How to watermark and set the watermark position in ThinkPHP (example analysis)

墨辰丷
Release: 2023-03-28 20:12:02
Original
1529 people have browsed it

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

I recently used the watermarking function of Thinkphp and found that it can only be printed in the lower left corner. PHP watermarking function is still very easy. The most important thing is to use

Copy code The code is as follows:

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 )

Start the coordinates in the src_im image from src_x, src_y, the width is src_w, and the height Copy a part of src_h to the positions of coordinates dst_x and dst_y in the dst_im image. 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, and I could 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[&#39;height&#39;] < $wInfo[&#39;height&#39;]) return false; //建立图像 $sCreateFun = "imagecreatefrom" . $sInfo[&#39;type&#39;]; $sImage = $sCreateFun($source); $wCreateFun = "imagecreatefrom" . $wInfo[&#39;type&#39;]; $wImage = $wCreateFun($water); //设定图像的混色模式 imagealphablending($wImage, true); //图像位置,默认为右下角右对齐 $posArr = $this->WaterPostion($postion,$sInfo,$wInfo); //新增
  //生成混合图像
  imagecopymerge($sImage, $wImage, $posArr[0], $posArr[1], 0, 0, $wInfo[&#39;width&#39;], $wInfo[&#39;height&#39;], $alpha);
  //输出图像
  $ImageFun = &#39;Image&#39; . $sInfo[&#39;type&#39;];
 //如果没有给出保存文件名,默认为原图像名
 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;
  }
}
Copy after login

Summary: The above is the entire content of this article , I hope it can be helpful to everyone’s study.

Related recommendations:

PHP implements magic methods and about independent instances and connected instances

PHP returns a JSON to the front end Object

phpMethods to implement file upload, download and deletion

The above is the detailed content of How to watermark and set the watermark position in ThinkPHP (example analysis). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template