您好,欢迎访问一九零五行业门户网

给图片添加水印(支持中文)并生成缩略图

缩略图|中文
// **************************************** //
// 功能:给图片添加水印(支持中文)并生成缩略图
// 参数: $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$dsty=($dstx$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);
//结束图形,释放内存空间
}
?>
其它类似信息

推荐信息