Home > php教程 > php手册 > body text

PHP生成图片水印和文字水印

WBOY
Release: 2016-06-13 11:15:03
Original
1787 people have browsed it

之前很多读者发邮件问我如何使用PHP生成水印,今天我就来给大家讲解一下。本篇PHP教程使用了两个函数来生成水印:watermark_text()和watermark_image()。你可以将本篇教程的示例整合到你的WEB项目中,比如上传图片的版权水印。

文本水印

我们使用函数watermark_text()来生成文本水印,你必须先指定字体源文件、字体大小和字体文本,具体代码如下:

<ol class="dp-c">
<li class="alt"><span><span class="vars">$font_path</span><span> = </span><span class="string">"GILSANUB.TTF"</span><span>; </span><span class="comment">// Font file </span><span> </span></span></li>
<li>
<span class="vars">$font_size</span><span> = 30; </span><span class="comment">// in pixcels </span><span> </span>
</li>
<li class="alt">
<span class="vars">$water_mark_text_2</span><span> = </span><span class="string">"phpfuns"</span><span>; </span><span class="comment">// Watermark Text </span><span> </span>
</li>
<li>
<span class="keyword">function</span><span> watermark_text(</span><span class="vars">$oldimage_name</span><span>, </span><span class="vars">$new_image_name</span><span>)  </span>
</li>
<li class="alt"><span>{  </span></li>
<li>
<span class="keyword">global</span><span> </span><span class="vars">$font_path</span><span>, </span><span class="vars">$font_size</span><span>, </span><span class="vars">$water_mark_text_2</span><span>;  </span>
</li>
<li class="alt">
<span>list(</span><span class="vars">$owidth</span><span>,</span><span class="vars">$oheight</span><span>) = </span><span class="func">getimagesize</span><span>(</span><span class="vars">$oldimage_name</span><span>);  </span>
</li>
<li>
<span class="vars">$width</span><span> = </span><span class="vars">$height</span><span> = 300;  </span>
</li>
<li class="alt">
<span class="vars">$image</span><span> = imagecreatetruecolor(</span><span class="vars">$width</span><span>, </span><span class="vars">$height</span><span>);  </span>
</li>
<li>
<span class="vars">$image_src</span><span> = imagecreatefromjpeg(</span><span class="vars">$oldimage_name</span><span>);  </span>
</li>
<li class="alt">
<span>imagecopyresampled(</span><span class="vars">$image</span><span>, </span><span class="vars">$image_src</span><span>, 0, 0, 0, 0, </span><span class="vars">$width</span><span>, </span><span class="vars">$height</span><span>, </span><span class="vars">$owidth</span><span>, </span><span class="vars">$oheight</span><span>);  </span>
</li>
<li>
<span class="vars">$blue</span><span> = imagecolorallocate(</span><span class="vars">$image</span><span>, 79, 166, 185);  </span>
</li>
<li class="alt">
<span>imagettftext(</span><span class="vars">$image</span><span>, </span><span class="vars">$font_size</span><span>, 0, 68, 190, </span><span class="vars">$blue</span><span>, </span><span class="vars">$font_path</span><span>, </span><span class="vars">$water_mark_text_2</span><span>);  </span>
</li>
<li>
<span>imagejpeg(</span><span class="vars">$image</span><span>, </span><span class="vars">$new_image_name</span><span>, 100);  </span>
</li>
<li class="alt">
<span>imagedestroy(</span><span class="vars">$image</span><span>);  </span>
</li>
<li>
<span>unlink(</span><span class="vars">$oldimage_name</span><span>);  </span>
</li>
<li class="alt">
<span class="keyword">return</span><span> true;  </span>
</li>
<li><span>}  </span></li>
</ol>
Copy after login

可以在这里

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!