Home >Backend Development >PHP Tutorial >Add text watermark to picture and picture watermark, add text watermark to picture_PHP tutorial
<?<span>php </span><span>/*</span><span>* * @desc 图片处理类 </span><span>*/</span> <span>class</span><span> Pic{ </span><span>private</span> <span>$info</span><span>; </span><span>private</span> <span>$res</span><span>; </span><span>public</span> <span>$thumb_pic</span><span>; </span><span>public</span> <span>function</span> __construct(<span>$picPath</span><span>){ </span><span>//</span><span>获取图片信息</span> <span>$this</span>->info = <span>getimagesize</span>(<span>$picPath</span><span>); </span><span>//</span><span>获取图片名</span> <span>$this</span>->info['type'] = image_type_to_extension(<span>$this</span>->info[2],<span>false</span><span>); </span><span>$funs</span> = 'imagecreatefrom'.<span>$this</span>->info['type'<span>]; </span><span>$this</span>->res = <span>$funs</span>(<span>$picPath</span><span>); } </span><span>//</span><span>缩略图</span> <span>public</span> <span>function</span> thumb(<span>$w</span>=100,<span>$h</span>=100<span>){ </span><span>//</span><span>创建图片资源</span> <span>$image_thumb</span> = imagecreatetruecolor(<span>$w</span>,<span>$h</span><span>); imagecopyresampled(</span><span>$image_thumb</span>,<span>$this</span>->res,0,0,0,0,<span>$w</span>,<span>$h</span>,<span>$this</span>->info[0],<span>$this</span>->info[1<span>]); imagedestroy(</span><span>$this</span>-><span>res); </span><span>$this</span>->res = <span>$image_thumb</span><span>; } </span><span>//</span><span>展示</span> <span>public</span> <span>function</span><span> showPic(){ </span><span>header</span>('Content-type:'.<span>$this</span>->info['type'<span>]); </span><span>$funs</span> = 'image'.<span>$this</span>->info['type'<span>]; </span><span>$funs</span>(<span>$this</span>-><span>res); } </span><span>//</span><span>保存</span> <span>public</span> <span>function</span> savePic(<span>$newname</span><span>){ </span><span>$funs</span> = 'image'.<span>$this</span>->info['type'<span>]; </span><span>$funs</span>(<span>$this</span>->res,<span>$newname</span>.".".<span>$this</span>->info['type'<span>]); } </span><span>//</span><span>销毁图片</span> <span>public</span> <span>function</span><span> __destruct(){ imagedestroy(</span><span>$this</span>-><span>res); } </span><span>/*</span><span>* * @desc 添加文字水印 * @param $content string 文字 * @param $fonturl string 字体路径 * @param $fontsize int 字体大小 * @param $fontcolor array 设置颜色和透明度 * @param $local array 水印坐标 * @param $fontangle int 字体旋转角度 </span><span>*/</span> <span>public</span> <span>function</span> fontMark(<span>$content</span>,<span>$fonturl</span>,<span>$fontsize</span>,<span>$fontcolor</span>,<span>$local</span>,<span>$fontangle</span><span>){ </span><span>$color</span> = imagecolorallocatealpha(<span>$this</span>->res,<span>$fontcolor</span>[0],<span>$fontcolor</span>[1],<span>$fontcolor</span>[2],<span>$fontcolor</span>[3<span>]); imagettftext(</span><span>$this</span>->res,<span>$fontsize</span>,<span>$fontangel</span>,<span>$local</span>['x'],<span>$local</span>['y'],<span>$color</span>,<span>$fonturl</span>,<span>$content</span><span>); } </span><span>/*</span><span>* * @desc 添加图片水印 * @param $markPic string 水印图片 * @param $local array 图片坐标 * @param $alpha array 设置颜色和透明度 </span><span>*/</span> <span>public</span> <span>function</span> picMark(<span>$markPic</span>,<span>$local</span>,<span>$alpha</span><span>){ </span><span>$markInfo</span> = <span>getimagesize</span>(<span>$markPic</span><span>); </span><span>$markType</span> = image_type_to_extension(<span>$markInfo</span>[2],<span>false</span><span>); </span><span>$markFun</span> = 'imagecreatefrom'.<span>$markType</span><span>; </span><span>$markWater</span> = <span>$markFun</span>(<span>$markPic</span><span>); imagecopymerge(</span><span>$this</span>->res,<span>$markWater</span>,<span>$local</span>['x'],<span>$local</span>['y'],0,0,<span>$markInfo</span>[0],<span>$markInfo</span>[1],<span>$alpha</span><span>); imagedestroy(</span><span>$markWater</span><span>); } } </span><span>$pic</span> = <span>new</span> Pic('yibo_pic28.jpg'<span>); </span><span>$pic</span>->picMark('logo3.png',<span>array</span>('x'=>50,'y'=>50),30<span>); </span><span>$pic</span>->showPic();