<div class="cnblogs_code">
PHP <span style="color: #008000;">/<span style="color: #008000;">
图片加水印
@param $srcImg 原图
@param $waterImg 水印图片
@param $savepath 保存路径
@param $savename 保存名字
@param $position 水印位置
1:左上 2:右上 3:居中 4:左下 5:右下
@param $opacity 透明度
0:全透明 100:完全不透明
@return 成功 -- 加水印后的新图片地址
-
失败 -- -1:源文件不存在,-2:水印不存在,-3源文件图片对象建立失败,-4:水印文件图像对象建立失败,-5:加水印后的新图片保存失败
- 获取源文件路径、宽高等信息,得出保存后文件保存路径、水印放置位置->建立源文件和水印图片对象->合并图片对象(imagecopymerge)->销毁图片对象
<span style="color: #008000;">*/
<span style="color: #0000ff;">function img_create_from_ext(<span style="color: #800080;">$imgfile<span style="color: #000000;">){
<span style="color: #800080;">$info = <span style="color: #008080;">getimagesize(<span style="color: #800080;">$imgfile<span style="color: #000000;">);
<span style="color: #800080;">$im = <span style="color: #0000ff;">null<span style="color: #000000;">;
<span style="color: #0000ff;">switch (<span style="color: #800080;">$info[2<span style="color: #000000;">]) {
<span style="color: #0000ff;">case 1:
<span style="color: #800080;">$im = imagecreatefromgif(<span style="color: #800080;">$imgfile<span style="color: #000000;">);
<span style="color: #0000ff;">break<span style="color: #000000;">;
<span style="color: #0000ff;">case 2:
<span style="color: #800080;">$im = imagecreatefromjpeg(<span style="color: #800080;">$imgfile<span style="color: #000000;">);
<span style="color: #0000ff;">break<span style="color: #000000;">;
<span style="color: #0000ff;">case 3:
<span style="color: #800080;">$im = imagecreatefrompng(<span style="color: #800080;">$imgfile<span style="color: #000000;">);
<span style="color: #0000ff;">break<span style="color: #000000;">;
}
<span style="color: #0000ff;">return <span style="color: #800080;">$im<span style="color: #000000;">;
}
<span style="color: #0000ff;">function img_water_mark(<span style="color: #800080;">$srcImg,<span style="color: #800080;">$waterImg,<span style="color: #800080;">$savepath=<span style="color: #0000ff;">null,<span style="color: #800080;">$savename=<span style="color: #0000ff;">null,<span style="color: #800080;">$position=5,<span style="color: #800080;">$opacity=50<span style="color: #000000;">){
<span style="color: #800080;">$temp = <span style="color: #008080;">pathinfo(<span style="color: #800080;">$srcImg<span style="color: #000000;">);
<span style="color: #800080;">$name = <span style="color: #800080;">$temp['basename'<span style="color: #000000;">];
<span style="color: #800080;">$path = <span style="color: #800080;">$temp['dirname'<span style="color: #000000;">];
<span style="color: #800080;">$exte = <span style="color: #800080;">$temp['extension'<span style="color: #000000;">];
<span style="color: #800080;">$savename = <span style="color: #800080;">$savename ? <span style="color: #800080;">$savename : <span style="color: #800080;">$name<span style="color: #000000;">;
<span style="color: #800080;">$savepath = <span style="color: #800080;">$savepath ? <span style="color: #800080;">$savepath : <span style="color: #800080;">$path<span style="color: #000000;">;
<span style="color: #800080;">$savefile = <span style="color: #800080;">$savepath.'/'.<span style="color: #800080;">$savename<span style="color: #000000;">;
</span><span style="color: #800080;">$srcinfo</span> = @<span style="color: #008080;">getimagesize</span>(<span style="color: #800080;">$srcImg</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$srcinfo</span><span style="color: #000000;">){
</span><span style="color: #0000ff;">return</span> -1<span style="color: #000000;">;
}
</span><span style="color: #800080;">$waterinfo</span> = @<span style="color: #008080;">getimagesize</span>(<span style="color: #800080;">$waterImg</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$waterinfo</span><span style="color: #000000;">){
</span><span style="color: #0000ff;">return</span> -2<span style="color: #000000;">;
}
</span><span style="color: #800080;">$srcImgObj</span> = img_create_from_ext(<span style="color: #800080;">$srcImg</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$srcImgObj</span><span style="color: #000000;">){
</span><span style="color: #0000ff;">return</span> -3<span style="color: #000000;">;
}
</span><span style="color: #800080;">$waterImgObj</span> = img_create_from_ext(<span style="color: #800080;">$waterImg</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$waterImgObj</span><span style="color: #000000;">){
</span><span style="color: #0000ff;">return</span> -4<span style="color: #000000;">;
}
</span><span style="color: #0000ff;">switch</span> (<span style="color: #800080;">$position</span><span style="color: #000000;">) {
</span><span style="color: #0000ff;">case</span> 1:
<span style="color: #800080;">$x</span>=<span style="color: #800080;">$y</span>=0<span style="color: #000000;">;
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">case</span> 2:
<span style="color: #800080;">$x</span>=<span style="color: #800080;">$srcinfo</span>[0] - <span style="color: #800080;">$waterinfo</span>[0<span style="color: #000000;">];
</span><span style="color: #800080;">$y</span>=0<span style="color: #000000;">;
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">case</span> 3:
<span style="color: #800080;">$x</span>=(<span style="color: #800080;">$srcinfo</span>[0] - <span style="color: #800080;">$waterinfo</span>[0])/2<span style="color: #000000;">;
</span><span style="color: #800080;">$y</span>=(<span style="color: #800080;">$srcinfo</span>[1] - <span style="color: #800080;">$waterinfo</span>[1])/2<span style="color: #000000;">;
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">case</span> 4:
<span style="color: #800080;">$x</span>=0<span style="color: #000000;">;
</span><span style="color: #800080;">$y</span>=<span style="color: #800080;">$srcinfo</span>[1] - <span style="color: #800080;">$waterinfo</span>[1<span style="color: #000000;">];
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">case</span> 5:
<span style="color: #800080;">$x</span>=<span style="color: #800080;">$srcinfo</span>[0] - <span style="color: #800080;">$waterinfo</span>[0<span style="color: #000000;">];
</span><span style="color: #800080;">$y</span>=<span style="color: #800080;">$srcinfo</span>[1] - <span style="color: #800080;">$waterinfo</span>[1<span style="color: #000000;">];
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
}
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 合并<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>+水印</span>
imagecopymerge(<span style="color: #800080;">$srcImgObj</span>,<span style="color: #800080;">$waterImgObj</span>,<span style="color: #800080;">$x</span>,<span style="color: #800080;">$y</span>,<span style="color: #800080;">$waterinfo</span>[0],<span style="color: #800080;">$waterinfo</span>[1],<span style="color: #800080;">$opacity</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">switch</span> (<span style="color: #800080;">$srcinfo</span>[2<span style="color: #000000;">]) {
</span><span style="color: #0000ff;">case</span> 1:<span style="color: #000000;">
imagegif(</span><span style="color: #800080;">$srcImgObj</span>,<span style="color: #800080;">$savefile</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">case</span> 2:<span style="color: #000000;">
imagejpeg(</span><span style="color: #800080;">$srcImgObj</span>,<span style="color: #800080;">$savefile</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">case</span> 3:<span style="color: #000000;">
imagepng(</span><span style="color: #800080;">$srcImgObj</span>,<span style="color: #800080;">$savefile</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">default</span>: <span style="color: #0000ff;">return</span> -5<span style="color: #000000;">;
}
imagedestroy(</span><span style="color: #800080;">$srcImgObj</span><span style="color: #000000;">);
imagedestroy(</span><span style="color: #800080;">$waterImgObj</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$savefile</span><span style="color: #000000;">;
}
?>