PHP给图片加水印

前端之家收集整理的这篇文章主要介绍了PHP给图片加水印前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<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;"&gt;$srcinfo</span> = @<span style="color: #008080;"&gt;getimagesize</span>(<span style="color: #800080;"&gt;$srcImg</span><span style="color: #000000;"&gt;);
</span><span style="color: #0000ff;"&gt;if</span>(!<span style="color: #800080;"&gt;$srcinfo</span><span style="color: #000000;"&gt;){
    </span><span style="color: #0000ff;"&gt;return</span> -1<span style="color: #000000;"&gt;;
}
</span><span style="color: #800080;"&gt;$waterinfo</span> = @<span style="color: #008080;"&gt;getimagesize</span>(<span style="color: #800080;"&gt;$waterImg</span><span style="color: #000000;"&gt;);
</span><span style="color: #0000ff;"&gt;if</span>(!<span style="color: #800080;"&gt;$waterinfo</span><span style="color: #000000;"&gt;){
    </span><span style="color: #0000ff;"&gt;return</span> -2<span style="color: #000000;"&gt;;
}
</span><span style="color: #800080;"&gt;$srcImgObj</span> = img_create_from_ext(<span style="color: #800080;"&gt;$srcImg</span><span style="color: #000000;"&gt;);
</span><span style="color: #0000ff;"&gt;if</span>(!<span style="color: #800080;"&gt;$srcImgObj</span><span style="color: #000000;"&gt;){
    </span><span style="color: #0000ff;"&gt;return</span> -3<span style="color: #000000;"&gt;;
}
</span><span style="color: #800080;"&gt;$waterImgObj</span> = img_create_from_ext(<span style="color: #800080;"&gt;$waterImg</span><span style="color: #000000;"&gt;);
</span><span style="color: #0000ff;"&gt;if</span>(!<span style="color: #800080;"&gt;$waterImgObj</span><span style="color: #000000;"&gt;){
    </span><span style="color: #0000ff;"&gt;return</span> -4<span style="color: #000000;"&gt;;
}
</span><span style="color: #0000ff;"&gt;switch</span> (<span style="color: #800080;"&gt;$position</span><span style="color: #000000;"&gt;) {
    </span><span style="color: #0000ff;"&gt;case</span> 1:
        <span style="color: #800080;"&gt;$x</span>=<span style="color: #800080;"&gt;$y</span>=0<span style="color: #000000;"&gt;;
        </span><span style="color: #0000ff;"&gt;break</span><span style="color: #000000;"&gt;;
    </span><span style="color: #0000ff;"&gt;case</span> 2:
        <span style="color: #800080;"&gt;$x</span>=<span style="color: #800080;"&gt;$srcinfo</span>[0] - <span style="color: #800080;"&gt;$waterinfo</span>[0<span style="color: #000000;"&gt;];
        </span><span style="color: #800080;"&gt;$y</span>=0<span style="color: #000000;"&gt;;
        </span><span style="color: #0000ff;"&gt;break</span><span style="color: #000000;"&gt;;
    </span><span style="color: #0000ff;"&gt;case</span> 3:
        <span style="color: #800080;"&gt;$x</span>=(<span style="color: #800080;"&gt;$srcinfo</span>[0] - <span style="color: #800080;"&gt;$waterinfo</span>[0])/2<span style="color: #000000;"&gt;;
        </span><span style="color: #800080;"&gt;$y</span>=(<span style="color: #800080;"&gt;$srcinfo</span>[1] - <span style="color: #800080;"&gt;$waterinfo</span>[1])/2<span style="color: #000000;"&gt;;
        </span><span style="color: #0000ff;"&gt;break</span><span style="color: #000000;"&gt;;
    </span><span style="color: #0000ff;"&gt;case</span> 4:
        <span style="color: #800080;"&gt;$x</span>=0<span style="color: #000000;"&gt;;
        </span><span style="color: #800080;"&gt;$y</span>=<span style="color: #800080;"&gt;$srcinfo</span>[1] - <span style="color: #800080;"&gt;$waterinfo</span>[1<span style="color: #000000;"&gt;];
        </span><span style="color: #0000ff;"&gt;break</span><span style="color: #000000;"&gt;;
    </span><span style="color: #0000ff;"&gt;case</span> 5:
        <span style="color: #800080;"&gt;$x</span>=<span style="color: #800080;"&gt;$srcinfo</span>[0] - <span style="color: #800080;"&gt;$waterinfo</span>[0<span style="color: #000000;"&gt;];
        </span><span style="color: #800080;"&gt;$y</span>=<span style="color: #800080;"&gt;$srcinfo</span>[1] - <span style="color: #800080;"&gt;$waterinfo</span>[1<span style="color: #000000;"&gt;];
        </span><span style="color: #0000ff;"&gt;break</span><span style="color: #000000;"&gt;;
}
</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 合并<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>+水印</span>
imagecopymerge(<span style="color: #800080;"&gt;$srcImgObj</span>,<span style="color: #800080;"&gt;$waterImgObj</span>,<span style="color: #800080;"&gt;$x</span>,<span style="color: #800080;"&gt;$y</span>,<span style="color: #800080;"&gt;$waterinfo</span>[0],<span style="color: #800080;"&gt;$waterinfo</span>[1],<span style="color: #800080;"&gt;$opacity</span><span style="color: #000000;"&gt;);

</span><span style="color: #0000ff;"&gt;switch</span> (<span style="color: #800080;"&gt;$srcinfo</span>[2<span style="color: #000000;"&gt;]) {
    </span><span style="color: #0000ff;"&gt;case</span> 1:<span style="color: #000000;"&gt;
        imagegif(</span><span style="color: #800080;"&gt;$srcImgObj</span>,<span style="color: #800080;"&gt;$savefile</span><span style="color: #000000;"&gt;);
        </span><span style="color: #0000ff;"&gt;break</span><span style="color: #000000;"&gt;;
    </span><span style="color: #0000ff;"&gt;case</span> 2:<span style="color: #000000;"&gt;
        imagejpeg(</span><span style="color: #800080;"&gt;$srcImgObj</span>,<span style="color: #800080;"&gt;$savefile</span><span style="color: #000000;"&gt;);
        </span><span style="color: #0000ff;"&gt;break</span><span style="color: #000000;"&gt;;
    </span><span style="color: #0000ff;"&gt;case</span> 3:<span style="color: #000000;"&gt;
        imagepng(</span><span style="color: #800080;"&gt;$srcImgObj</span>,<span style="color: #800080;"&gt;$savefile</span><span style="color: #000000;"&gt;);
        </span><span style="color: #0000ff;"&gt;break</span><span style="color: #000000;"&gt;;
    </span><span style="color: #0000ff;"&gt;default</span>: <span style="color: #0000ff;"&gt;return</span> -5<span style="color: #000000;"&gt;;
}
imagedestroy(</span><span style="color: #800080;"&gt;$srcImgObj</span><span style="color: #000000;"&gt;);
imagedestroy(</span><span style="color: #800080;"&gt;$waterImgObj</span><span style="color: #000000;"&gt;);
</span><span style="color: #0000ff;"&gt;return</span> <span style="color: #800080;"&gt;$savefile</span><span style="color: #000000;"&gt;;

}

?>

 

原文链接:https://www.f2er.com/php/238703.html

猜你在找的PHP相关文章