A formal website, when uploading pictures, often needs to add its own website’s LOGO watermark to the pictures. So how to implement this step? First, let us understand
to create graphics by judging the file type, then copy it to the original graphics, fill in and create a rectangle, ready to be written to imagestring() or in the original Determine the watermark type in the image program that has been set: one is a string, and the other is to add a graphic object on top. The following is a reprint of the PHP image with watermark!
Parameter description:
$max_file_size: upload file size limit, unit BYTE
$destination_folder: upload file path
$watermark: whether to attach a watermark (1 means adding watermark, others (to add no watermark);
Instructions for adding watermark to PHP images:
1. Remove the ; sign in front of the "extension=php_gd2.dll" line in the PHP.INI file, because we To use the GD library;
2. Change extension_dir = to the directory where your php_gd2.dll is located;
Code example for watermarking PHP images:
<ol class="dp-xml"> <li class="alt"><span><span>//上传文件类型列表 </span></span></li> <li> <span>$</span><span class="attribute">uptypes</span><span>=</span><span class="attribute-value">array</span><span>( </span> </li> <li class="alt"><span>'image/jpg', </span></li> <li><span>'image/jpeg', </span></li> <li class="alt"><span>'image/png', </span></li> <li><span>'image/pjpeg', </span></li> <li class="alt"><span>'image/gif', </span></li> <li><span>'image/bmp', </span></li> <li class="alt"><span>'image/x-png' </span></li> <li><span>); </span></li> <li class="alt"> <span>$</span><span class="attribute">max_file_size</span><span>=</span><span class="attribute-value">2000000</span><span>; //上传文件大小限制, 单位BYTE </span> </li> <li> <span>$</span><span class="attribute">destination_folder</span><span>=</span><span class="attribute-value">"uploadimg/"</span><span>; //上传文件路径 </span> </li> <li class="alt"> <span>$</span><span class="attribute">watermark</span><span>=</span><span class="attribute-value">1</span><span>; //是否附加水印(1为加水印,其他为不加水印); </span> </li> <li> <span>$</span><span class="attribute">watertype</span><span>=</span><span class="attribute-value">1</span><span>; //水印类型(1为文字,2为图片) </span> </li> <li class="alt"> <span>$</span><span class="attribute">waterposition</span><span>=</span><span class="attribute-value">1</span><span>; //水印位置(1为左下角,2为右下角 </span> </li> <li><span>,3为左上角,4为右上角,5为居中); </span></li> <li class="alt"> <span>$</span><span class="attribute">waterstring</span><span>="</span><span class="tag"><</span><span> </span><span class="tag-name">A</span><span> href="</span><span class="attribute-value">http</span><span>://www.xplore.cn/"</span><span class="tag">></span><span> </span> </li> <li> <span>http://www.xplore.cn/</span><span class="tag"><</span><span> /A</span><span class="tag">></span><span>"; //水印字符串 </span> </li> <li class="alt"> <span>$</span><span class="attribute">waterimg</span><span>=</span><span class="attribute-value">"xplore.gif"</span><span>; //水印图片 </span> </li> <li> <span>$</span><span class="attribute">imgpreview</span><span>=</span><span class="attribute-value">1</span><span>; //是否生成预览图(1为生成,其他为不生成); </span> </li> <li class="alt"> <span>$</span><span class="attribute">imgpreviewsize</span><span>=</span><span class="attribute-value">1</span><span>/2; //缩略图比例 </span> </li> <li> <span class="tag">?></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><span> </span><span class="tag-name">html</span><span class="tag">></span><span> </span> </li> <li> <span class="tag"><</span><span> </span><span class="tag-name">head</span><span class="tag">></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><span> </span><span class="tag-name">title</span><span class="tag">></span><span>ZwelL图片上传程序</span><span class="tag"></</span><span class="tag-name">title</span><span class="tag">></span><span> </span> </li> <li> <span class="tag"><</span><span> </span><span class="tag-name">style</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/css"</span><span class="tag">></span><span> </span> </li> <li class="alt"> <span><!-- </span></li><li><span>body </span></li><li class="alt"><span>{ </span></li><li><span>font-size: 9pt; </span></li><li class="alt"><span>} </span></li><li><span>input </span></li><li class="alt"><span>{ </span></li><li><span>background-color: #66CCFF; </span></li><li class="alt"><span>border: 1px inset #CCCCCC; </span></li><li><span>} </span></li><li class="alt"><span>--</span><span class="tag">></span><span> </span> </li> <li> <span class="tag"><</span><span> /style</span><span class="tag">></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><span> /head</span><span class="tag">></span><span> </span> </li> <li> <span class="tag"><</span><span> </span><span class="tag-name">body</span><span class="tag">></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><span> </span><span class="tag-name">form</span><span> </span><span class="attribute">enctype</span><span>=</span><span class="attribute-value">"multipart/form-data"</span><span> </span></li><li><span class="attribute">method</span><span>=</span><span class="attribute-value">"post"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"upform"</span><span class="tag">></span><span> </span> </li> <li class="alt"><span>上传文件: </span></li> <li> <span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"upfile"</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"file"</span><span class="tag">></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"submit"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"上传"</span><span class="tag">></span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span class="tag">></span><span> </span> </li> <li> <span>允许上传的文件类型为:</span><span class="tag"><?</span><span>=implode(', ',$uptypes)</span><span class="tag">?></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><span> /form</span><span class="tag">></span><span> </span> </li> <li> <span class="tag"><</span><span> ?php </span></li><li class="alt"><span>if ($_SERVER['REQUEST_METHOD'] == 'POST') </span></li><li><span>{ </span></li><li class="alt"><span>if (!is_uploaded_file($_FILES["upfile"] </span></li><li><span>[tmp_name])) </span></li><li class="alt"><span>//是否存在文件 </span></li><li><span>{ </span></li><li class="alt"><span>echo "图片不存在!"; </span></li><li><span>exit; </span></li><li class="alt"><span>} </span></li><li><span>$</span><span class="attribute">file</span><span> = $_FILES["upfile"]; </span></li><li class="alt"><span>if($max_file_size </span><span class="tag"><</span><span> $file["size"]) </span></li><li><span>//检查文件大小 </span></li><li class="alt"><span>{ </span></li><li><span>echo "文件太大!"; </span></li><li class="alt"><span>exit; </span></li><li><span>} </span></li><li class="alt"><span>if(!in_array($file["type"], $uptypes)) </span></li><li><span>//检查文件类型 </span></li><li class="alt"><span>{ </span></li><li><span>echo "文件类型不符!".$file["type"]; </span></li><li class="alt"><span>exit; </span></li><li><span>} </span></li><li class="alt"><span>if(!file_exists($destination_folder)) </span></li><li><span>{ </span></li><li class="alt"><span>mkdir($destination_folder); </span></li><li><span>} </span></li><li class="alt"><span>$</span><span class="attribute">filename</span><span>=$file["tmp_name"]; </span></li><li><span>$</span><span class="attribute">image_size</span><span> = </span><span class="attribute-value">getimagesize</span><span>($filename); </span></li><li class="alt"><span>$</span><span class="attribute">pinfo</span><span>=</span><span class="attribute-value">pathinfo</span><span>($file["name"]); </span></li><li><span>$</span><span class="attribute">ftype</span><span>=$pinfo['extension']; </span></li><li class="alt"><span>$</span><span class="attribute">destination</span><span> = $destination_folder. </span></li><li><span>time().".".$ftype; </span></li><li class="alt"><span>if (file_exists($destination) && </span></li><li><span> $overwrite != true) </span></li><li class="alt"><span>{ </span></li><li><span>echo "同名文件已经存在了"; </span></li><li class="alt"><span>exit; </span></li><li><span>} </span></li><li class="alt"><span>if(!move_uploaded_file ($filename, </span></li><li><span> $destination)) </span></li><li class="alt"><span>{ </span></li><li><span>echo "移动文件出错"; </span></li><li class="alt"><span>exit; </span></li><li><span>} </span></li><li class="alt"><span>$</span><span class="attribute">pinfo</span><span>=</span><span class="attribute-value">pathinfo</span><span>($destination); </span></li><li><span>$</span><span class="attribute">fname</span><span>=$pinfo[basename]; </span></li><li class="alt"><span>echo " </span><span class="tag"><</span><span class="tag-name">font</span><span> </span><span class="attribute">color</span><span>=</span><span class="attribute-value">red</span><span class="tag">></span><span>已经成功上传 </span> </li> <li> <span class="tag"><</span><span> /font</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">br</span><span class="tag">></span><span>文件名: </span> </li> <li class="alt"> <span class="tag"><</span><span> </span><span class="tag-name">font</span><span> </span><span class="attribute">color</span><span>=</span><span class="attribute-value">blue</span><span class="tag">></span><span>".$destination_folder. </span> </li> <li> <span>$fname."</span><span class="tag"><</span><span> /font</span><span class="tag">></span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span class="tag">></span><span>"; </span> </li> <li class="alt"><span>echo " 宽度:".$image_size[0]; </span></li> <li><span>echo " 长度:".$image_size[1]; </span></li> <li class="alt"> <span>echo "</span><span class="tag"><</span><span class="tag-name">br</span><span class="tag">></span><span> 大小:".$file["size"]." bytes"; </span> </li> <li> <span>if($</span><span class="attribute">watermark</span><span>==1) </span> </li> <li class="alt"><span>{ </span></li> <li> <span>$</span><span class="attribute">iinfo</span><span>=</span><span class="attribute-value">getimagesize</span><span>($destination,$iinfo); </span> </li> <li class="alt"> <span>$</span><span class="attribute">nimage</span><span>=</span><span class="attribute-value">imagecreatetruecolor</span><span>($image_size[0] </span> </li> <li><span>,$image_size[1]); </span></li> <li class="alt"> <span>$</span><span class="attribute">white</span><span>=</span><span class="attribute-value">imagecolorallocate</span><span>($nimage,255,255,255); </span> </li> <li> <span>$</span><span class="attribute">black</span><span>=</span><span class="attribute-value">imagecolorallocate</span><span>($nimage,0,0,0); </span> </li> <li class="alt"> <span>$</span><span class="attribute">red</span><span>=</span><span class="attribute-value">imagecolorallocate</span><span>($nimage,255,0,0); </span> </li> <li><span>imagefill($nimage,0,0,$white); </span></li> <li class="alt"><span>switch ($iinfo[2]) </span></li> <li><span>{ </span></li> <li class="alt"><span>case 1: </span></li> <li> <span>$</span><span class="attribute">simage</span><span> =</span><span class="attribute-value">imagecreatefromgif</span><span>($destination); </span> </li> <li class="alt"><span>break; </span></li> <li><span>case 2: </span></li> <li class="alt"> <span>$</span><span class="attribute">simage</span><span> =</span><span class="attribute-value">imagecreatefromjpeg</span><span>($destination); </span> </li> <li><span>break; </span></li> <li class="alt"><span>case 3: </span></li> <li> <span>$</span><span class="attribute">simage</span><span> =</span><span class="attribute-value">imagecreatefrompng</span><span>($destination); </span> </li> <li class="alt"><span>break; </span></li> <li><span>case 6: </span></li> <li class="alt"> <span>$</span><span class="attribute">simage</span><span> =</span><span class="attribute-value">imagecreatefromwbmp</span><span>($destination); </span> </li> <li><span>break; </span></li> <li class="alt"><span>default: </span></li> <li><span>die("不支持的文件类型"); </span></li> <li class="alt"><span>exit; </span></li> <li><span>} </span></li> <li class="alt"><span>imagecopy($nimage,$simage,0,0,0,0, </span></li> <li><span>$image_size[0],$image_size[1]); </span></li> <li class="alt"><span>imagefilledrectangle($nimage,1, </span></li> <li><span>$image_size[1]-15,80,$image_size[1],$white); </span></li> <li class="alt"><span>switch($watertype) </span></li> <li><span>{ </span></li> <li class="alt"><span>case 1: //加水印字符串 </span></li> <li><span>imagestring($nimage,2,3,$image_size[1]-15, </span></li> <li class="alt"><span>$waterstring,$black); </span></li> <li><span>break; </span></li> <li class="alt"><span>case 2: //加水印图片 </span></li> <li> <span>$</span><span class="attribute">simage1</span><span> =</span><span class="attribute-value">imagecreatefromgif</span><span>("xplore.gif"); </span> </li> <li class="alt"><span>imagecopy($nimage,$simage1,0,0,0,0,85,15); </span></li> <li><span>imagedestroy($simage1); </span></li> <li class="alt"><span>break; </span></li> <li><span>} </span></li> <li class="alt"><span>switch ($iinfo[2]) </span></li> <li><span>{ </span></li> <li class="alt"><span>case 1: </span></li> <li><span>//imagegif($nimage, $destination); </span></li> <li class="alt"><span>imagejpeg($nimage, $destination); </span></li> <li><span>break; </span></li> <li class="alt"><span>case 2: </span></li> <li><span>imagejpeg($nimage, $destination); </span></li> <li class="alt"><span>break; </span></li> <li><span>case 3: </span></li> <li class="alt"><span>imagepng($nimage, $destination); </span></li> <li><span>break; </span></li> <li class="alt"><span>case 6: </span></li> <li><span>imagewbmp($nimage, $destination); </span></li> <li class="alt"><span>//imagejpeg($nimage, $destination); </span></li> <li><span>break; </span></li> <li class="alt"><span>} </span></li> <li><span>//覆盖原上传文件 </span></li> <li class="alt"><span>imagedestroy($nimage); </span></li> <li><span>imagedestroy($simage); </span></li> <li class="alt"><span>} </span></li> <li> <span>if($</span><span class="attribute">imgpreview</span><span>==1) </span> </li> <li class="alt"><span>{ </span></li> <li> <span>echo "</span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span class="tag">></span><span>图片预览:</span><span class="tag"><</span><span class="tag-name">br</span><span class="tag">></span><span>"; </span> </li> <li class="alt"> <span>echo "</span><span class="tag"><</span><span> </span><span class="tag-name">ccid_file</span><span> </span><span class="attribute">values</span><span>=</span><span class="attribute-value">""</span><span> </span><span class="attribute">width</span><span>=". </span></li><li><span>($image_size[0]*$imgpreviewsize)." </span></li><li class="alt"><span class="attribute">height</span><span>=</span><span class="attribute-value">".($image_size[1]*$imgpreviewsize);"</span><span> </span></li><li><span>echo " </span><span class="attribute">alt</span><span>="图片预览:r文件名:". </span></li><li class="alt"><span>$destination."r上传时间:" </span><span class="tag">/></span><span>"; </span> </li> <li><span>} </span></li> <li class="alt"><span>} </span></li> <li> <span class="tag">?></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><span> /body</span><span class="tag">></span><span> </span> </li> <li> <span class="tag"><</span><span> /html</span><span class="tag">></span><span> </span> </li> </ol>
The above paragraph The code example is the specific implementation method of adding watermark to PHP images.