文本水印

文本水印为图片添加文字,主要使用GD库的imagefttext方法,和需要的字体文件,结果如下:



实现代码如下所示:

复制代码代码如下所示:
dst_path美元= 'dst .jpg;

创建图片的一个实例
DST = imagecreatefromstring(file_get_contents美元(美元dst_path));

标记文本
$字体= '。 /宋体。TTC '; / /字体
黑imagecolorallocate美元(美元DST,0x00,0x00,0x00); / /字体颜色
imagefttext(13, 0, 20美元,20美元的DST,黑色字体快乐编程美元,);

输出画面
列表(dst_w美元美元美元,dst_h,dst_type)= getimagesize($ dst_path);
开关($ dst_type){
案例1
头('content-type:图像/ GIF);
imagegif($ DST);
打破;
案例2
头('content-type:图像/ JPEG);
imagejpeg($ DST);
打破;
案例3
头('content-type:图像/格式);
imagepng($ DST);
打破;
违约:
打破;
}

销毁一图像($ DST);


图片水印

图像水印是到另一个图片添加图片,主要使用GD库的imagecopy和拷贝并合并图像的一部分,结果如下:



实现代码如下所示:

复制代码代码如下所示:
dst_path美元= 'dst .jpg;
src_path美元= 'src .jpg;

创建图片的一个实例
DST = imagecreatefromstring(file_get_contents美元(美元dst_path));
SRC = imagecreatefromstring(file_get_contents美元(美元src_path));

获取水印图像的宽度和高度
列表(src_w美元,美元src_h)= getimagesize($ src_path);

将水印图像复制到目标图像中,最后50个参数在这里设置透明度,以实现半透明效果。
拷贝并合并图像的一部分($ DST,SRC美元,10, 10, 0,0,src_w美元,src_h美元,50);
/ /如果水印图像本身的透明的颜色,然后用imagecopy方法
/ / imagecopy($ DST,SRC美元,10, 10, 0,0,src_w美元,美元src_h);

输出画面
列表(dst_w美元美元美元,dst_h,dst_type)= getimagesize($ dst_path);
开关($ dst_type){
案例1
头('content-type:图像/ GIF);
imagegif($ DST);
打破;
案例2
头('content-type:图像/ JPEG);
imagejpeg($ DST);
打破;
案例3
头('content-type:图像/格式);
imagepng($ DST);
打破;
违约:
打破;
}

销毁一图像($ DST);
销毁一图像($ SRC);