


How to watermark and set the watermark position in ThinkPHP (example analysis)
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['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; } }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
