PHP图像处理 imagestring添加图片水印

<?php
header("Content-Type: text/html;charset=utf-8");
//指定图片路径
$src = '001.png';
//获取图片信息
$info = getimagesize($src);
//获取图片扩展名
$type = image_type_to_extension($info[2],false);
//动态的把图片导入内存中
$fun =  "imagecreatefrom{$type}";
$image = $fun('001.png');
//指定字体颜色
$col = imagecolorallocatealpha($image,0,0,0,0); //R,G,B,透明度


//指定字体内容
$content = 'zhangsan';
//给图片添加文字
imagestring($image,5,190,255,$content,$col);

//指定字体内容
$content = '123456789';
//给图片添加文字
imagestring($image,5,190,285,$content,$col);

//指定字体内容
$content = '98.6';
//给图片添加文字
imagestring($image,5,190,320,$content,$col);

//指定输入类型
header('Content-type:'.$info['mime']);
//动态的输出图片到浏览器中
$func = "image{$type}";
$func($image);
//销毁图片
imagedestroy($image);
?>

这里我们使用了imagestring方法来添加文字,但是imagestring并不支持中文字符,添加中文可以使用imagettftext来添加。

效果图:

PHP图像处理 imagestring添加图片水印