Heim > Backend-Entwicklung > PHP-Tutorial > 利用php给图片添加文字水印-面向对象与面临过程俩种方法的实现

利用php给图片添加文字水印-面向对象与面临过程俩种方法的实现

WBOY
Freigeben: 2016-06-13 12:24:03
Original
967 Leute haben es durchsucht

利用php给图片添加文字水印--面向对象与面向过程俩种方法的实现

1: 面向过程的编写方法

<span style="color: #008000;">//</span><span style="color: #008000;">指定图片路径</span><span style="color: #800080;">$src</span> = '001.png'<span style="color: #000000;">;</span><span style="color: #008000;">//</span><span style="color: #008000;">获取图片信息</span><span style="color: #800080;">$info</span> = <span style="color: #008080;">getimagesize</span>(<span style="color: #800080;">$src</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">获取图片扩展名</span><span style="color: #800080;">$type</span> = image_type_to_extension(<span style="color: #800080;">$info</span>[2],<span style="color: #0000ff;">false</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">动态的把图片导入内存中</span><span style="color: #800080;">$fun</span> =  "imagecreatefrom{<span style="color: #800080;">$type</span>}"<span style="color: #000000;">;</span><span style="color: #800080;">$image</span> = <span style="color: #800080;">$fun</span>('001.png'<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">指定字体颜色</span><span style="color: #800080;">$col</span> = imagecolorallocatealpha(<span style="color: #800080;">$image</span>,255,255,255,50<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">指定字体内容</span><span style="color: #800080;">$content</span> = 'helloworld'<span style="color: #000000;">;</span><span style="color: #008000;">//</span><span style="color: #008000;">给图片添加文字</span>imagestring(<span style="color: #800080;">$image</span>,5,20,30,<span style="color: #800080;">$content</span>,<span style="color: #800080;">$col</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">指定输入类型</span><span style="color: #008080;">header</span>('Content-type:'.<span style="color: #800080;">$info</span>['mime'<span style="color: #000000;">]);</span><span style="color: #008000;">//</span><span style="color: #008000;">动态的输出图片到浏览器中</span><span style="color: #800080;">$func</span> = "image{<span style="color: #800080;">$type</span>}"<span style="color: #000000;">;</span><span style="color: #800080;">$func</span>(<span style="color: #800080;">$image</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;">销毁图片</span>imagedestroy(<span style="color: #800080;">$image</span>);
Nach dem Login kopieren

2:面向对象的实现方法

<span style="color: #0000ff;">class</span><span style="color: #000000;"> Image_class {    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$image</span><span style="color: #000000;">;    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$info</span><span style="color: #000000;">;    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * @param $src:图片路径     * 加载图片到内存中     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$src</span><span style="color: #000000;">){        </span><span style="color: #800080;">$info</span> = <span style="color: #008080;">getimagesize</span>(<span style="color: #800080;">$src</span><span style="color: #000000;">);        </span><span style="color: #800080;">$type</span> = image_type_to_extension(<span style="color: #800080;">$info</span>[2],<span style="color: #0000ff;">false</span><span style="color: #000000;">);        </span><span style="color: #800080;">$this</span> -> info =<span style="color: #800080;">$info</span><span style="color: #000000;">;        </span><span style="color: #800080;">$this</span>->info['type'] = <span style="color: #800080;">$type</span><span style="color: #000000;">;        </span><span style="color: #800080;">$fun</span> = "imagecreatefrom" .<span style="color: #800080;">$type</span><span style="color: #000000;">;        </span><span style="color: #800080;">$this</span> -> image = <span style="color: #800080;">$fun</span>(<span style="color: #800080;">$src</span><span style="color: #000000;">);    }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * @param $fontsize: 字体大小     * @param $x: 字体在图片中的x位置     * @param $y: 字体在图片中的y位置     * @param $color: 字体的颜色是一个包含rgba的数组     * @param $text: 想要添加的内容     * 操作内存中的图片,给图片添加文字水印     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> fontMark(<span style="color: #800080;">$fontsize</span>,<span style="color: #800080;">$x</span>,<span style="color: #800080;">$y</span>,<span style="color: #800080;">$color</span>,<span style="color: #800080;">$text</span><span style="color: #000000;">){        </span><span style="color: #800080;">$col</span> = imagecolorallocatealpha(<span style="color: #800080;">$this</span>->image,<span style="color: #800080;">$color</span>[0],<span style="color: #800080;">$color</span>[1],<span style="color: #800080;">$color</span>[2],<span style="color: #800080;">$color</span>[3<span style="color: #000000;">]);        imagestring(</span><span style="color: #800080;">$this</span>->image,<span style="color: #800080;">$fontsize</span>,<span style="color: #800080;">$x</span>,<span style="color: #800080;">$y</span>,<span style="color: #800080;">$text</span>,<span style="color: #800080;">$col</span><span style="color: #000000;">);    }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">     * 输出图片到浏览器中     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> show(){       </span><span style="color: #008080;">header</span>('content-type:' . <span style="color: #800080;">$this</span> -> info['mime'<span style="color: #000000;">]);        </span><span style="color: #800080;">$fun</span>='image' . <span style="color: #800080;">$this</span>->info['type'<span style="color: #000000;">];        </span><span style="color: #800080;">$fun</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">image);    }    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*     * 销毁图片     </span><span style="color: #008000;">*/</span>    <span style="color: #0000ff;">function</span><span style="color: #000000;"> __destruct(){        imagedestroy(</span><span style="color: #800080;">$this</span>-><span style="color: #000000;">image);    }}</span><span style="color: #008000;">//</span><span style="color: #008000;">对类的调用</span><span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">new</span> Image_class('001.png'<span style="color: #000000;">);</span><span style="color: #800080;">$obj</span>->fontMark(20,20,30,<span style="color: #0000ff;">array</span>(255,255,255,60),'hello'<span style="color: #000000;">);</span><span style="color: #800080;">$obj</span>->show();
Nach dem Login kopieren

 

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage