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

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

Jun 13, 2016 pm 12:24 PM
image info param this type

利用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>);
Copier après la connexion

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> -&gt; info =<span style="color: #800080;">$info</span><span style="color: #000000;">;        </span><span style="color: #800080;">$this</span>-&gt;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> -&gt; 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>-&gt;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>-&gt;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> -&gt; info['mime'<span style="color: #000000;">]);        </span><span style="color: #800080;">$fun</span>='image' . <span style="color: #800080;">$this</span>-&gt;info['type'<span style="color: #000000;">];        </span><span style="color: #800080;">$fun</span>(<span style="color: #800080;">$this</span>-&gt;<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>-&gt;<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>-&gt;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>-&gt;show();
Copier après la connexion

 

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Article chaud

Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD
Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Musée à deux points: toutes les expositions et où les trouver
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌

Article chaud

Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD
Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Musée à deux points: toutes les expositions et où les trouver
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌

Tags d'article chaud

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Quelles sont les utilisations du mot-clé Type dans Go ? Quelles sont les utilisations du mot-clé Type dans Go ? Sep 06, 2023 am 09:58 AM

Quelles sont les utilisations du mot-clé Type dans Go ?

Résoudre l'erreur de montage du disque dur mobile Ubuntu : type de système de fichiers inconnu exfat Résoudre l'erreur de montage du disque dur mobile Ubuntu : type de système de fichiers inconnu exfat Jan 05, 2024 pm 01:18 PM

Résoudre l'erreur de montage du disque dur mobile Ubuntu : type de système de fichiers inconnu exfat

Comment utiliser Bing Image Creator gratuitement Comment utiliser Bing Image Creator gratuitement Feb 27, 2024 am 11:04 AM

Comment utiliser Bing Image Creator gratuitement

Comment supprimer des images des téléphones Xiaomi Comment supprimer des images des téléphones Xiaomi Mar 02, 2024 pm 05:34 PM

Comment supprimer des images des téléphones Xiaomi

Installation d'Imagemagic Tutoriel d'installation de Centos et Image Installation d'Imagemagic Tutoriel d'installation de Centos et Image Feb 12, 2024 pm 05:27 PM

Installation d'Imagemagic Tutoriel d'installation de Centos et Image

Commandes de type Linux Commandes de type Linux Mar 20, 2024 pm 05:06 PM

Commandes de type Linux

Quelles informations sont requises lors de l'enregistrement d'un compte Deepseek? Quelles informations sont requises lors de l'enregistrement d'un compte Deepseek? Mar 12, 2025 pm 02:33 PM

Quelles informations sont requises lors de l'enregistrement d'un compte Deepseek?

Comment utiliser cette méthode en Java Comment utiliser cette méthode en Java Apr 18, 2023 pm 01:58 PM

Comment utiliser cette méthode en Java

See all articles