Home  >  Article  >  Backend Development  >  Adding watermark to php pictures and adding watermark to uploaded pictures php class_PHP tutorial

Adding watermark to php pictures and adding watermark to uploaded pictures php class_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:38:291052browse

When a regular website needs to upload pictures, it often needs to add its own website's LOGO watermark to the pictures. So how to implement this step? First, let us understand the principle of watermarking PHP images.
Create a graphic by determining the file type, then copy it to the originally created graphic, fill in and create a rectangle, ready to be written to imagestring() or to determine the watermark type in the original image program: First, string , the other is to add a graphics 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 not add 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 need to use it GD library;
2. Change extension_dir = to the directory where your php_gd2.dll is located;
3. http://www.jb51.net/list/list_15_1.htm
Code for watermarking PHP images Example:

Copy code The code is as follows:

//Upload file type list
$uptypes=array(
'image/jpg',
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/bmp',
'image/x-png'
);
$max_file_size=2000000; //Upload File size limit, unit BYTE
$destination_folder="uploadimg/"; //Upload file path
$watermark=1; //Whether to attach a watermark (1 means adding watermark, others means not adding watermark);
$watertype=1; //Watermark type (1 is text, 2 is picture)
$waterposition=1; //Watermark position (1 is the lower left corner, 2 is the lower right corner
, 3 is the upper left corner, 4 is the upper right corner, 5 is in the center);
$waterstring="
http://www.xplore.cn/< ;/A>"; //Watermark string
$waterimg="xplore.gif"; //Watermark image
$imgpreview=1; //Whether to generate a preview image (1 means generated, others will not be generated) );
$imgpreviewsize=1/2; //Thumbnail ratio
?>


ZwelL image uploader< /title> <br><style type="text/css"> <br><!-- <BR>body <BR>{ <BR>font-size: 9pt; <BR>} <BR> input <BR>{ <BR>background-color: #66CCFF; <BR>border: 1px inset #CCCCCC; <BR>} <BR>--> <br></style> <br></ head> <br><body> <br><form enctype="multipart/form-data" <BR>method="post" name="upform"> <br>Upload file: <br>< input name="upfile" type="file"> <br><input type="submit" value="Upload"><br> <br>The file types allowed to be uploaded are:<?=implode (', ',$uptypes)?> <br></form> <br><?php <BR>if ($_SERVER['REQUEST_METHOD'] == 'POST') <BR>{ <BR>if (!is_uploaded_file($_FILES["upfile"] <BR>[tmp_name])) <BR>//Whether the file exists<BR>{ <BR>echo "The picture does not exist!"; <BR>exit; <BR>} <BR>$file = $_FILES["upfile"]; <BR>if($max_file_size <$file["size"]) <BR>//Check file size<BR>{ <BR>echo "The file is too big!"; <BR>exit; <BR>} <BR>if(!in_array($file["type"], $uptypes)) <BR>//Check the file type<BR>{ <BR>echo "File type does not match!".$file["type"]; <BR>exit; <BR>} <BR>if(!file_exists($destination_folder)) <BR>{ <BR>mkdir($destination_folder) ; <BR>} <BR>$filename=$file["tmp_name"]; <BR>$image_size = getimagesize($filename); <BR>$pinfo=pathinfo($file["name"]); <BR>$ftype=$pinfo['extension']; <BR>$destination = $destination_folder. <BR>time().".".$ftype; <BR>if (file_exists($destination) && <BR>$ overwrite != true) <BR>{ <BR>echo "The file with the same name already exists"; <BR>exit; <BR>} <BR>if(!move_uploaded_file ($filename, <BR>$destination)) <BR>{ <BR>echo "Error moving file"; <BR>exit; <BR>} <BR>$pinfo=pathinfo($destination); <BR>$fname=$pinfo[basename]; <BR>echo " <font color=red>Uploaded successfully<br></font><br>File name: <br><font color=blue>".$destination_folder. <br>$fname."</ font><br>"; <br>echo " width:".$image_size[0]; <br>echo " length:".$image_size[1]; <br>echo "<br> size:" .$file["size"]." bytes"; <br>if($watermark==1) <br>{ <br>$iinfo=getimagesize($destination,$iinfo); <br>$nimage=imagecreatetruecolor ($image_size[0] <br>,$image_size[1]); <br>$white=imagecolorallocate($nimage,255,255,255); <br>$black=imagecolorallocate($nimage,0,0,0); <br>$red=imagecolorallocate($nimage,255,0,0); <br>imagefill($nimage,0,0,$white); <br>switch ($iinfo[2]) <br>{ <br>case 1: <br>$simage =imagecreatefromgif($destination); <br>break; <br>case 2: <br>$simage =imagecreatefromjpeg($destination); <br>break; <br>case 3: <br>$simage =imagecreatefrompng($destination); <br>break; <br>case 6: <br>$simage =imagecreatefromwbmp($destination); <br>break; <br>default: <br>die( "Unsupported file type"); <br>exit; <br>} <br>imagecopy($nimage,$simage,0,0,0,0, <br>$image_size[0],$image_size[1 ]); <br>imagefilledrectangle($nimage,1, <br>$image_size[1]-15,80,$image_size[1],$white); <br>switch($watertype) <br>{ <br>case 1: //Add watermark string<br>imagestring($nimage,2,3,$image_size[1]-15, <br>$waterstring,$black); <br>break; <br>case 2 : //Watermark image<br>$simage1 =imagecreatefromgif("xplore.gif"); <br>imagecopy($nimage,$simage1,0,0,0,0,85,15); <br>imagedestroy( $simage1); <br>break; <br>} <br>switch ($iinfo[2]) <br>{ <br>case 1: <br>//imagegif($nimage, $destination); <br>imagejpeg($nimage, $destination); <br>break; <br>case 2: <br>imagejpeg($nimage, $destination); <br>break; <br>case 3: <br>imagepng($ nimage, $destination); <br>break; <br>case 6: <br>imagewbmp($nimage, $destination); <br>//imagejpeg($nimage, $destination); <br>break; <br>} <br>//Overwrite the original uploaded file <br>imagedestroy($nimage); <br>imagedestroy($simage); <br>}<br>if($imgpreview==1) <br>{ <br>echo "<br>Image preview:<br>"; <br>echo "<ccid_file values="" width=". <BR>($image_size[0]*$imgpreviewsize)." <BR>height=".($image_size[1]*$imgpreviewsize);" <BR>echo " alt="Image preview:rFile name:". <BR>$destination."rUpload time:" />"; <br>} <br>} <br>?> <br></body> <br></html> <br> </div> <br><br>You can also refer to the PHP class for adding watermarks to pictures <p align="left"></p> <div style="display:none;"> <span id="url" itemprop="url">http://www.bkjia.com/PHPjc/321772.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http: //www.bkjia.com/PHPjc/321772.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">A regular website often needs to add its own website’s LOGO watermark to the image when uploading pictures. So how to implement this step? First let us understand PHP images...</span> </div> <div class="art_confoot"></div></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>Statement:</span><div>The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn</div></div></div><div class="nphpSytBox"><span>Previous article:<a class="dBlack" title="Implementation code for php radio radio button to obtain and maintain values_PHP Tutorial" href="http://m.php.cn/faq/309926.html">Implementation code for php radio radio button to obtain and maintain values_PHP Tutorial</a></span><span>Next article:<a class="dBlack" title="Implementation code for php radio radio button to obtain and maintain values_PHP Tutorial" href="http://m.php.cn/faq/309928.html">Implementation code for php radio radio button to obtain and maintain values_PHP Tutorial</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>Related articles</h2><em><a href="http://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ul class="nphpXgwzList"><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/2.html" title="All expression symbols in regular expressions (summary)" class="aBlack">All expression symbols in regular expressions (summary)</a><div class="clear"></div></li></ul></div></div><div class="nphpFoot"><div class="nphpFootBg"><ul class="nphpFootMenu"><li><a href="http://m.php.cn/"><b class="icon1"></b><p>Home</p></a></li><li><a href="http://m.php.cn/course.html"><b class="icon2"></b><p>Course</p></a></li><li><a href="http://m.php.cn/wenda.html"><b class="icon4"></b><p>Q&A</p></a></li><li><a href="http://m.php.cn/login"><b class="icon5"></b><p>My</p></a></li><div class="clear"></div></ul></div></div><div class="nphpYouBox" style="display: none;"><div class="nphpYouBg"><div class="nphpYouTitle"><span onclick="$('.nphpYouBox').hide()"></span><a href="http://m.php.cn/"></a><div class="clear"></div></div><ul class="nphpYouList"><li><a href="http://m.php.cn/"><b class="icon1"></b><span>Home</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/course.html"><b class="icon2"></b><span>Course</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/article.html"><b class="icon3"></b><span>Article</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/wenda.html"><b class="icon4"></b><span>Q&A</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/dic.html"><b class="icon6"></b><span>Dictionary</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/course/type/99.html"><b class="icon7"></b><span>Manual</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/xiazai/"><b class="icon8"></b><span>Download</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/faq/zt" title="Topic"><b class="icon12"></b><span>Topic</span><div class="clear"></div></a></li><div class="clear"></div></ul></div></div><div class="nphpDing" style="display: none;"><div class="nphpDinglogo"><a href="http://m.php.cn/"></a></div><div class="nphpNavIn1"><div class="swiper-container nphpNavSwiper1"><div class="swiper-wrapper"><div class="swiper-slide"><a href="http://m.php.cn/" >Home</a></div><div class="swiper-slide"><a href="http://m.php.cn/article.html" class="hover">Article</a></div><div class="swiper-slide"><a href="http://m.php.cn/wenda.html" >Q&A</a></div><div class="swiper-slide"><a href="http://m.php.cn/course.html" >Course</a></div><div class="swiper-slide"><a href="http://m.php.cn/faq/zt" >Topic</a></div><div class="swiper-slide"><a href="http://m.php.cn/xiazai" >Download</a></div><div class="swiper-slide"><a href="http://m.php.cn/game" >Game</a></div><div class="swiper-slide"><a href="http://m.php.cn/dic.html" >Dictionary</a></div><div class="clear"></div></div></div><div class="langadivs" ><a href="javascript:;" class="bg4 bglanguage"></a><div class="langadiv" ><a onclick="javascript:setlang('zh-cn');" class="language course-right-orders chooselan " href="javascript:;"><span>简体中文</span><span>(ZH-CN)</span></a><a onclick="javascript:;" class="language course-right-orders chooselan chooselanguage" href="javascript:;"><span>English</span><span>(EN)</span></a><a onclick="javascript:setlang('zh-tw');" class="language course-right-orders chooselan " href="javascript:;"><span>繁体中文</span><span>(ZH-TW)</span></a><a onclick="javascript:setlang('ja');" class="language course-right-orders chooselan " href="javascript:;"><span>日本語</span><span>(JA)</span></a><a onclick="javascript:setlang('ko');" class="language course-right-orders chooselan " href="javascript:;"><span>한국어</span><span>(KO)</span></a><a onclick="javascript:setlang('ms');" class="language course-right-orders chooselan " href="javascript:;"><span>Melayu</span><span>(MS)</span></a><a onclick="javascript:setlang('fr');" class="language course-right-orders chooselan " href="javascript:;"><span>Français</span><span>(FR)</span></a><a onclick="javascript:setlang('de');" class="language course-right-orders chooselan " href="javascript:;"><span>Deutsch</span><span>(DE)</span></a></div></div><script> var swiper = new Swiper('.nphpNavSwiper1', { slidesPerView : 'auto', observer: true,//修改swiper自己或子元素时,自动初始化swiper observeParents: true,//修改swiper的父元素时,自动初始化swiper }); </script></div></div><!--顶部导航 end--><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body></html>