Home Backend Development PHP Tutorial How to watermark and set the watermark position in ThinkPHP (example analysis)

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

Jun 01, 2018 am 09:34 AM
php thinkphp Location

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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 Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

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.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

See all articles