Many readers have sent me emails asking me how to use PHP to generate watermarks. Today I will explain it to you. This PHP tutorial uses two functions to generate watermarks: watermark_text() and watermark_image(). You can integrate the examples in this tutorial into your WEB project, such as uploading copyright watermarks for images.
Text watermark
We use the function watermark_text() to generate text watermark, you must first specify the font source file, font size and font text , the specific code is as follows:
<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>
can be found here