相关关键词
关于我们
最新文章
百度编辑器增加水印功能
百度编辑器不用介绍,最近写一个网站时用到给图片加水印,去官方看添加水印的功能, 以及生成缩略图,网上找了很多代码,有些能用有些不能用,有些写得太复杂!。
其实很简单,如下方法操作即可;
找到php/imageUp.php
$info = $up->getFileInfo();//在这一行下面添加如下代码
setWater($info["url"],"../../images/sy1_5.png",9,"img");
//总结:不管是生成缩略图还是生成水印,都只需要在图片成功上传后得到图片的服务器路径,用下面两个方法处理即可。
//seWater(原图,水印,位置0-9,字体,图片水印或文字水印)注:这里只支持图片,文字的代码已经删除。适用性不大
//说明:水印是加在原文件上,不会生成新的文件
//setWater("images/11.jpg","images/sy.png",0,"img");
function setWater($imgSrc,$markImg,$markPos,$markType)
{
$srcInfo = @getimagesize($imgSrc);
$srcImg_w = $srcInfo[0];
$srcImg_h = $srcInfo[1];
switch ($srcInfo[2])
{
case 1:
$srcim =imagecreatefromgif($imgSrc);
break;
case 2:
$srcim =imagecreatefromjpeg($imgSrc);
break;
case 3:
$srcim =imagecreatefrompng($imgSrc);
break;
default:
die("不支持的图片文件类型");
exit;
}
if(!strcmp($markType,"img"))
{
if(!file_exists($markImg) || empty($markImg))
{
return;
}
$markImgInfo = @getimagesize($markImg);
$markImg_w = $markImgInfo[0];
$markImg_h = $markImgInfo[1];
if($srcImg_w < $markImg_w || $srcImg_h < $markImg_h)
{
return;
}
switch ($markImgInfo[2])
{
case 1:
$markim =imagecreatefromgif($markImg);
break;
case 2:
$markim =imagecreatefromjpeg($markImg);
break;
case 3:
$markim =imagecreatefrompng($markImg);
break;
default:
die("不支持的水印图片文件类型");
exit;
}
$logow = $markImg_w;
$logoh = $markImg_h;
}
if($markPos == 0)
{
$markPos = rand(1, 9);
}
switch($markPos)
{
case 1:
$x = +5;
$y = +5;
break;
case 2:
$x = ($srcImg_w - $logow) / 2;
$y = +5;
break;
case 3:
$x = $srcImg_w - $logow - 5;
$y = +15;
break;
case 4:
$x = +5;
$y = ($srcImg_h - $logoh) / 2;
break;
case 5:
$x = ($srcImg_w - $logow) / 2;
$y = ($srcImg_h - $logoh) / 2;
break;
case 6:
$x = $srcImg_w - $logow - 5;
$y = ($srcImg_h - $logoh) / 2;
break;
case 7:
$x = +5;
$y = $srcImg_h - $logoh - 5;
break;
case 8:
$x = ($srcImg_w - $logow) / 2;
$y = $srcImg_h - $logoh - 5;
break;
case 9:
$x = $srcImg_w - $logow - 5;
$y = $srcImg_h - $logoh -5;
break;
default:
die("此位置不支持");
exit;
}
$dst_img = @imagecreatetruecolor($srcImg_w, $srcImg_h);
imagecopy ( $dst_img, $srcim, 0, 0, 0, 0, $srcImg_w, $srcImg_h);
if(!strcmp($markType,"img"))
{
imagecopy($dst_img, $markim, $x, $y, 0, 0, $logow, $logoh);
imagedestroy($markim);
}
// if(!strcmp($markType,"text"))
//{
// $rgb = explode(',', $TextColor);
// $color = imagecolorallocate($dst_img, $rgb[0], $rgb[1], $rgb[2]);
// imagettftext($dst_img, $fontSize, 0, $x, $y, $color, $fontType,$markText);
//}
switch ($srcInfo[2])
{
case 1:
imagegif($dst_img, $imgSrc);
break;
case 2:
imagejpeg($dst_img, $imgSrc);
break;
case 3:
imagepng($dst_img, $imgSrc);
break;
default:
die("不支持的水印图片文件类型");
exit;
}
imagedestroy($dst_img);
imagedestroy($srcim);
}
//生成缩略图
//makeThumbnail(源图片,缩略图,缩略图宽,缩略图高)
//makeThumbnail("images/222.jpg","images/2221.jpg",100,100);
function makeThumbnail($srcImgPath,$targetImgPath,$targetW,$targetH)
{
//echo $targetImgPath;
$imgSize = GetImageSize($srcImgPath);
$imgType = $imgSize[2];
//@ 使函数不向页面输出错误信息
switch ($imgType)
{
case 1:
$srcImg = @ImageCreateFromGIF($srcImgPath);
break;
case 2:
$srcImg = @ImageCreateFromJpeg($srcImgPath);
break;
case 3:
$srcImg = @ImageCreateFromPNG($srcImgPath);
break;
}
//取源图象的宽高
$srcW = ImageSX($srcImg);
$srcH = ImageSY($srcImg);
if($srcW>$targetW || $srcH>$targetH)
{
$targetX = 0;
$targetY = 0;
if ($srcW > $srcH)
{
$finaW=$targetW;
$finalH=round($srcH*$finaW/$srcW);
$targetY=floor(($targetH-$finalH)/2);
}
else
{
$finalH=$targetH;
$finaW=round($srcW*$finalH/$srcH);
$targetX=floor(($targetW-$finaW)/2);
}
//function_exists 检查函数是否已定义
//ImageCreateTrueColor 本函数需要GD2.0.1或更高版本
if(function_exists("ImageCreateTrueColor"))
{
$targetImg=ImageCreateTrueColor($targetW,$targetH);
}
else
{
$targetImg=ImageCreate($targetW,$targetH);
}
$targetX=($targetX<0)?0:$targetX;
$targetY=($targetX<0)?0:$targetY;
$targetX=($targetX>($targetW/2))?floor($targetW/2):$targetX;
$targetY=($targetY>($targetH/2))?floor($targetH/2):$targetY;
//背景白色
$white = ImageColorAllocate($targetImg, 255,255,255);
ImageFilledRectangle($targetImg,0,0,$targetW,$targetH,$white);
/*
PHP的GD扩展提供了两个函数来缩放图象:
ImageCopyResized 在所有GD版本中有效,其缩放图象的算法比较粗糙,可能会导致图象边缘的锯齿。
ImageCopyResampled 需要GD2.0.1或更高版本,其像素插值算法得到的图象边缘比较平滑,
该函数的速度比ImageCopyResized慢。
*/
if(function_exists("ImageCopyResampled"))
{
ImageCopyResampled($targetImg,$srcImg,$targetX,$targetY,0,0,$finaW,$finalH,$srcW,$srcH);
}
else
{
ImageCopyResized($targetImg,$srcImg,$targetX,$targetY,0,0,$finaW,$finalH,$srcW,$srcH);
}
switch ($imgType) {
case 1:
ImageGIF($targetImg,$targetImgPath);
break;
case 2:
ImageJpeg($targetImg,$targetImgPath);
break;
case 3:
ImagePNG($targetImg,$targetImgPath);
break;
}
ImageDestroy($srcImg);
ImageDestroy($targetImg);
}
else //不超出指定宽高则直接复制
{
copy($srcImgPath,$targetImgPath);
ImageDestroy($srcImg);
}
}
function smallpic($srcImgPath)
{
$img_arr=explode(".",$srcImgPath);
$targetImgPath="..".$img_arr[2]."sm.".$img_arr[3];
return $targetImgPath;
}