php实现给图片指定位置加水印文字

时间:2018-12-04
需求:给图片指定位置加水印文字。使用的是 imagettftext,这个可以输出中文了,有个必须参数是要真实存在的字体文件,于是先下载个字体文件,下面是函数原型:
 
array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
 
demo代码和效果如下:
 
//1.配置图片路径  
$src = "1.jpg";  
//2.获取图片信息  
$info = getimagesize($src);  
//3.通过编号获取图像类型  
$type = image_type_to_extension($info[2],false);  
//4.在内存中创建和图像类型一样的图像  
$fun = "imagecreatefrom".$type;  
//5.图片复制到内存  
$image = $fun($src);  
/*操作图片*/  
//1.设置字体的路径  
$font = "simkai.ttf";  
//2.填写水印内容  
$content = "水印文字:some special words are supported.";  
//3.设置字体颜色和透明度  
$color = imagecolorallocatealpha($image, 255, 255, 255, 0);  
//4.写入文字 (图片资源,字体大小,旋转角度,坐标x,坐标y,颜色,字体文件,内容) 
imagettftext($image, 30, 0, 100, 60, $color, $font, $content);  
/*输出图片*/  
//浏览器输出  
header("Content-type:".$info['mime']);  
$fun = "image".$type;  
$fun($image);  
//保存图片  
$fun($image,'bg_res.'.$type);  
/*销毁图片*/  
imagedestroy($image);
 
上一条:PHP获取字符串长度之strlen和mb_strlen的区别 下一条:PHP实现模拟登录签到功能

相关文章

最新文章