/*
* 功能:给出一张图片的路径,向图片添加水印。
*/
function imageInnerText($src,$text,$font,$next="_bak",$text_size=12,$c="000000"){
$path=pathinfo($n,PATHINFO_DIRNAME);// 获取目录名
$name=pathinfo($n,PATHINFO_FILENAME);// 获取文件名
$ext=pathinfo($n,PATHINFO_EXTENSION);// 获取扩展名
//$text=iconv("gbk","utf-8",$text);
$size=getimagesize($src);// 获取图片的宽、高,以数组的形式存储起来
$x=$size[0]-(mb_strlen($text,"gbk")*$text_size);// $size[0],获得源图片的宽,$x是水印的x坐标
$y=$size[1]-$text_size;// $size[1],获得源图片的高,$y是水印的y坐标
$c1="0x".substr($c,0,2);
$c2="0x".substr($c,2,2);
$c3="0x".substr($c,4,2);
if($ext=="jpg"){
$old=imagecreatefromjpeg($src);//由文件或URL创建一个新图象
$color=imagecolorallocate($old,$c1,$c2,$c3);
imagettftext($old,$text_size,0,$x,$y,$color,$font,$text);
imagejpeg($old,$path.'/'.$name.$next.'.'.$ext);
}else if($ext=="png"){
$old=imagecreatefrompng($src);
$color=imagecolorallocate($old,$c1,$c2,$c3);
imagettftext($old,$text_size,0,$x,$y,$color,$font,$text);
imagepng($old,$path."/".$name.$next.".".$ext);
}else if($ext=="gif"){
$old=imagecreatefromgif($src);
$color=imagecolorallocate($old,$c1,$c2,$c3);
imagettftext($old,$text_size,0,$x,$y,$color,$font,$text);
imagegif($old,$path."/".$name.$next.".".$ext);
}
}
imageInnerText("E:/2bak.jpg","学习php","a.ttf","_bak",20,"ffff00");