This article mainly introduces the method of watermarking and setting watermark position in ThinkPHP. It analyzes the related operation steps and specific implementation skills of thinkPHP printing and setting watermark in the form of examples. Friends in need can refer to the following
The example in this article describes the method of watermarking and setting the watermark position in ThinkPHP. I would like to share it with you for your reference. The details are as follows:
I recently used the watermarking function of Thinkphp and found that it can only be printed in the lower left corner. PHP watermarking is still very easy. The most important thing is to use
bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )
to copy a part of the src_im image whose coordinates start from src_x, src_y, with a width of src_w and a height of src_h to the dst_im image where the coordinates are dst_x and dst_y. position. The two images will be merged based on pct, which ranges from 0 to 100. When pct = 0, it actually does nothing. When it is 100, this function is exactly the same as imagecopy() for palette images. It implements alpha transparency for truecolor images.
Watermark demo picture:
I need to put the watermark in the middle of the picture, check the Thinkphp code. I found that the author actually wrote it to death, I can only make one modification
/** * 为图片添加水印 * @static public * @param string $source 原文件名 * @param string $water 水印图片 * @param string $$savename 添加水印后的图片名 * @param string $postion 水印的具体位置 leftbottom rightbottom lefttop righttop center <新增> * @param string $alpha 水印的透明度 * @return void */ static public function water($source, $water, $savename=null,$postion="center", $alpha=80) { //检查文件是否存在 if (!file_exists($source) || !file_exists($water)) return false; //图片信息 $sInfo = self::getImageInfo($source); $wInfo = self::getImageInfo($water); //如果图片小于水印图片,不生成图片 if ($sInfo["width"] < $wInfo["width"] || $sInfo['height'] < $wInfo['height']) return false; //建立图像 $sCreateFun = "imagecreatefrom" . $sInfo['type']; $sImage = $sCreateFun($source); $wCreateFun = "imagecreatefrom" . $wInfo['type']; $wImage = $wCreateFun($water); //设定图像的混色模式 imagealphablending($wImage, true); //图像位置,默认为右下角右对齐 $posArr = $this->WaterPostion($postion,$sInfo,$wInfo); //新增 //生成混合图像 imagecopymerge($sImage, $wImage, $posArr[0], $posArr[1], 0, 0, $wInfo['width'], $wInfo['height'], $alpha); //输出图像 $ImageFun = 'Image' . $sInfo['type']; //如果没有给出保存文件名,默认为原图像名 if (!$savename) { $savename = $source; @unlink($source); } //保存图像 $ImageFun($sImage, $savename); imagedestroy($sImage); } private function WaterPostion($postion,$sInfo,$wInfo) { $posY = $sInfo["height"] - $wInfo["height"]; $posX = $sInfo["width"] - $wInfo["width"]; switch($postion) { case "rightbottom": return array($posX,$posY); break; case "leftbottom": return array($wInfo["width"],$posY); break; case "lefttop": return array($wInfo["width"],$wInfo["height"]); break; case "righttop": return array($posX,$wInfo["height"]); break; case "center": return array($posX/2,$posY/2); break; } }
The above is the entire content of this article, I hope it will be helpful to everyone Learning will be helpful. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
How to use thinkphp to obtain the client IP
About the thinkphp framework's function of adding and displaying data Method
ThinkPHP template output display
##
The above is the detailed content of Analysis on ThinkPHP watermarking and setting watermark position. For more information, please follow other related articles on the PHP Chinese website!