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

WBOY
Lepaskan: 2016-07-20 10:58:25
asal
801 orang telah melayarinya

之前很多读者发邮件问我如何使用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>
Salin selepas log masuk

可以在这里

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445685.htmlTechArticle之前很多读者发邮件问我如何使用PHP生成水印,今天我就来给大家讲解一下。本篇PHP教程使用了两个函数来生成水印:watermark_text()和water...
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!