PHP生成图片水印和文字水印_PHP教程

WBOY
发布: 2016-07-20 10:58:25
原创
801 人浏览过

之前很多读者发邮件问我如何使用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>
登录后复制

可以在这里

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445685.htmlTechArticle之前很多读者发邮件问我如何使用PHP生成水印,今天我就来给大家讲解一下。本篇PHP教程使用了两个函数来生成水印:watermark_text()和water...
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!