1.给图像加水印 应先创建图像 可以使用的函数有以下函数
imagecreatefromgd 从 GD 文件或 URL 新建一图像 语法 resource imagecreatefromgd ( string $filename
)
imagecreatefromgd2 从 GD2 文件或 URL 新建一图像 语法 resource imagecreatefromgd2 ( string $filename
)
imagecreatefromgif 由文件或 URL 创建一个新图象 语法 resource imagecreatefromgif ( string $filename
)
imagecreatefromjpeg 由文件或 URL 创建一个新图象 语法 resource imagecreatefromjpeg ( string $filename
)
imagecreatefrompng 由文件或 URL 创建一个新图象 语法 resource imagecreatefrompng ( string $filename
)
!!!但是有个综合以上功能的函数
imagecreatefromstring — 从字符串中的图像流新建一图像 resource imagecreatefromstring ( string $image
)
imagecreatefromstring() 返回一个图像标识符,其表达了从给定字符串得来的图像。图像格式将自动检测,只要 PHP 支持:JPEG,PNG,GIF,WBMP 和 GD2
$img = imagecreatefromstring($str);
if($img === false){
die('图像创建失败');
}
2第二步加载图片并将文件写入字符串中 使用file_get_contents函数
file_get_contents — 将整个文件读入一个字符串
$imgfile = "ss.jpg";
header('content-type:text/html;charset=utf8');
//将图片文件读取出来,存入到一个变量中
$str = file_get_contents($imgfile);
3.最后在图片上加水印 即在图片上输入字符串
使用file_exits 检查文件目录是否存在
存在:true
不存在 :false
//加水印(即在图像上输出字符串)
$font = "STCAIYUN.TTF";
if(!file_exists($font)){
die('字体库文件不存在');
}
设置颜色使用imagecolorocate 并且设置输入的文本$text $text = "xxx";
$color = imagecolorallocate($img,255,255,255);
最后使用imagettftext 输出文本
imagettftext($img, 30, 0, 50, 50, $color, $font, $text);