Maison >développement back-end >tutoriel php >PHP生成图片水印和文字水印_PHP教程
之前很多读者发邮件问我如何使用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>
可以在这里