日期:2013-05-29  浏览次数:20397 次

<?

// **************************************** //
// 功能:给图片添加水印(支持中文)并生成缩略图
// 参数: $srcFile 图片文件名
// $dstFile 另存图片文件名
// $markwords 水印文字内容
// $markimage 水印图片地址
// $dstW 图片保存宽度
// $dstH 图片保存高度
// $rate 图片保存品质
// **************************************** //
function makethumb($srcFile,$dstFile,$dstW,$dstH,$rate=100,$markwords=null,$markimage=null)
{
$data = GetImageSize($srcFile);
switch($data[2])
{
case 1:
$im=@ImageCreateFromGIF($srcFile);
break;
case 2:
$im=@ImageCreateFromJPEG($srcFile);
break;
case 3:
$im=@ImageCreateFromPNG($srcFile);
break;
}
if(!$im) return False;
$srcW=ImageSX($im);
$srcH=ImageSY($im);
$dstX=0;
$dstY=0;
if ($srcW*$dstH>$srcH*$dstW)
{
$fdstH = round($srcH*$dstW/$srcW);
$dstY = floor(($dstH-$fdstH)/2);
$fdstW = $dstW;
}
else
{
$fdstW = round($srcW*$dstH/$srcH);
$dstX = floor(($dstW-$fdstW)/2);
$fdstH = $dstH;
}
$ni=ImageCreateTrueColor($dstW,$dstH);
$dstX=($dstX<0)?0:$dstX;
$dstY=($dstX<0)?0:$dstY;
$dstX=($dstX>($dstW/2))?floor($dstW/2):$dstX;
$dstY=($dstY>($dstH/2))?floor($dstH/s):$dstY;
$white = ImageColorAllocate($ni,255,255,255);
$black = ImageColorAllocate($ni,0,0,0);
imagefilledrectangle($ni,0,0,$dstW,$dstH,$white);// 填充背景色
ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH);

// 生成水印
if($markwords!=null)
{
$markwords=iconv("gb2312","UTF-8",$markwords);
//转换文字编码
ImageTTFText($ni,9,0,10,15,$white,"simhei.ttf",$markwords);
//ImageTTFText(int im,int size,int angle,int x,int y,int col,string fontfile,string text):
//本函数将 TTF (TrueType Fonts) 字型文字写入图片。
//参数: size 为字形的尺寸;
// angle 为字型的角度,顺时针计算,0 度为水平(由左到右),90 度则为由下到上的文字;
// x,y 二参数为文字的坐标值 (原点为左上角);
// col 为字的颜色;
// fontfile 为字型文件名称;
// text 是字符串内容。
}
elseif($markimage!=null)
{
$wimage_data = GetImageSize($markimage);
switch($wimage_data[2])
{
case 1:
$wimage=@ImageCreateFromGIF($markimage);
break;
case 2:
$wimage=@ImageCreateFromJPEG($markimage);
break;
case 3:
$wimage=@ImageCreateFromPNG($markimage);
break;
}
imagecopy($ni,$wimage,0,0,0,0,88,31);
imagedestroy($wimage);
}

ImageJpeg($ni,$dstFile,$rate);
imagedestroy($im);
imagedestroy($ni);
//结束图形,释放内存空间
}
?>