首頁 >後端開發 >php教程 >圖片添加文字浮水印 和圖片浮水印

圖片添加文字浮水印 和圖片浮水印

WBOY
WBOY原創
2016-08-08 09:28:361343瀏覽

<?<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();

以上就介紹了圖片添加文字浮水印 和圖片浮水印,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn