


A super powerful class for adding watermarks to images_PHP Tutorial
I had nothing to do in the afternoon, so I wrote a class for adding watermarks to pictures. This class implements adding text watermarks to pictures, adding picture watermarks, and also implements the transparency function for all netizens to learn and communicate
/**
* Add watermark category, support text, picture watermark and transparency setting, watermark picture background is transparent.
* @author litx date:2011-12-05 at 3 pm at Micron Qvod R&D Center
*/
class WaterMask
{
/**
* Watermark type
* @var int $waterType 0 is text watermark; 1 is picture watermark
*/
Private $waterType = 1;
/**
* Watermark position type
* @var int $pos Default is 9 (lower right corner)
*/
Private $pos = 9;
/**
* Watermark transparency
* @var int $transparent Watermark transparency (the smaller the value, the more transparent)
*/
private $transparent = 20;
/**
* If it is a text watermark, you need to add the watermark text
* @var string $waterStr Default value (Li Tiexiong’s personal collection)
*/
private $waterStr = 'Personal portfolio';
/**
* Text font size
* @var int $fontSize Font size
*/
private $fontSize = 14;
/**
* Watermark text color (RGB)
* @var array $fontColor Watermark text color (RGB)
*/
Private $fontColor = array (255, 255, 255);
/**
* Font file
* @var unknown_type
*/
Private $fontFile = 'AHGBold.ttf';
/**
* Watermark image
* @var string $waterImg
*/
Private $waterImg = 'logo.png';
/**
* Pictures that need to be watermarked
* @var string $srcImg
*/
Private $srcImg = '';
/**
* Image handle
* @var string $im
*/
private $im = '';
/**
* Watermark image handle
* @var string $water_im
*/
private $water_im = '';
/**
* Picture information
* @var array $srcImg_info
*/
Private $srcImg_info = '';
/**
* Watermark image information
* @var array $waterImg_info
*/
Private $waterImg_info = '';
/**
* Watermark text width
* @var int $str_w
*/
private $str_w = '';
/**
* Watermark text height
* @var int $str_h
*/
private $str_h = '';
/**
* Watermark X coordinates
* @var int $x
*/
private $x = '';
/**
* Watermark y coordinates
* @var int $y
*/
private $y = '';
/**
* Constructor, initialize the source image by passing in the source image that needs to be watermarked
* @param string $img Source image that needs to be watermarked
*/
Public function __construct ($img)
{
If(file_exists($img)){//The source file exists
$this -> srcImg = $img;
}else{//The source file does not exist
echo 'The source file'.$img.' does not exist, please check whether the file path is correct';
exit();
}
}
/**
* Get the information of the picture that needs to be watermarked, and load the picture
*/
Public function imginfo ()
$this -> srcImg_info = getimagesize($this -> srcImg);
var_dump($this -> srcImg_info);exit();
switch ($this -> srcImg_info[2]) {
case 3 ://png
$this -> im = imagecreatefrompng($this -> srcImg);
break 1;
case 2: // jpeg/jpg
$this -> im = imagecreatefromjpeg($this -> srcImg);
break 1;
case 1: //gif
$this -> im = imagecreatefromgif($this -> srcImg);
break 1;
default
Echo 'source picture file'. $ This -& gt; srcimg. 'Format is incorrect. At present, this function only supports PNG, JPEG, GIF picture watermark function';exit();
}
}
/**
* Get the information of the watermark image and load the image
*/
Private function waterimginfo ()
{
$this -> waterImg_info = getimagesize($this -> waterImg);
switch ($this -> waterImg_info[2]) {
case 3:
$ This -& gt; water_im = ImageCreateFrompng ($ this -& gt; waterimg);
break 1;
case 2:
$this -> water_im = imagecreatefromjpeg($this -> waterImg);
break 1;
case 1 :
$this -> water_im = imagecreatefromgif($this -> waterImg);
break 1;
default
Echo 'source picture file'. $ This -& gt; srcimg. 'Format is incorrect. At present, this function only supports PNG, JPEG, GIF picture watermark function';exit();
}
}
/**
* Watermark position algorithm
*/
private function waterpos ()
{
switch ($this -> pos) {
case 0: //Random position
$this -> x = rand(0, $this -> srcImg_info[0] - $this -> waterImg_info[0]);$this -> y = rand(0, $this -> srcImg_info[1] - $this -> waterImg_info[1]);
break 1;
Case 1: // Upper left
$this -> x = 20;
$this -> y = 20;
break 1;
case 2: //upper middle
$this -> x = ($this -> srcImg_info[0] - $this -> waterImg_info[0]) / 2;
$this -> y = 20;
Break 1;
Case 3: //Top right
$this -> x = $this -> srcImg_info[0] - $this -> waterImg_info[0];
$this -> y = 20;
break 1;
case 4: //center left
$this -> x = 20;
$this -> y = ($this -> srcImg_info[1] - $this -> waterImg_info[1]) / 2;
break 1;
case 5: //中中
$this -> x = ($this -> srcImg_info[0] - $this -> waterImg_info[0]) / 2;
$this -> y = ($this -> srcImg_info[1] - $this -> waterImg_info[1]) / 2;
break 1;
case 6 : //中右
$this -> x = $this -> srcImg_info[0] - $this -> waterImg_info[0] - 20;
$this -> y = ($this -> srcImg_info[1] - $this -> waterImg_info[1]) / 2;
break 1;
case 7 : //下左
$this -> x = 20;
$this -> y = $this -> srcImg_info[1] - $this -> waterImg_info[1] - 20;
break 1;
case 8 : //下中 www.2cto.com
$this -> x = ($this -> srcImg_info[0] - $this -> waterImg_info[0]) / 2;
$this -> y = $this -> srcImg_info[1] - $this -> waterImg_info[1] - 20;
break 1;
case 9 : //下右
$this -> x = $this -> srcImg_info[0] - $this -> waterImg_info[0] - 20;
$this -> y = $this -> srcImg_info[1] - $this -> waterImg_info[1] - 20;
break 1;
default : //下右
$this -> x = $this -> srcImg_info[0] - $this -> waterImg_info[0] - 20;
$this -> y = $this -> srcImg_info[1] - $this -> waterImg_info[1] - 20;
break 1;
}
}
/**
* Add image watermark
*/
private function waterimg ()
{
if ($this -> srcImg_info[0] waterImg_info[0] || $this -> srcImg_info[1] waterImg_info[1]) {
echo '图片尺寸太小,无法加水印,请上传一张大图片';
exit();
}
//计算水印位置
$this->waterpos();
$cut = imagecreatetruecolor($this -> waterImg_info[0], $this -> waterImg_info[1]);
imagecopy($cut, $this -> im, 0, 0, $this -> x, $this -> y, $this -> waterImg_info[0],
$this -> waterImg_info[1]);
$pct = $this -> transparent;
imagecopy($cut, $this -> water_im, 0, 0, 0, 0, $this -> waterImg_info[0],
$this -> waterImg_info[1]);
//将图片与水印图片合成
imagecopymerge($this -> im, $cut, $this -> x, $this -> y, 0, 0, $this -> waterImg_info[0], $this -> waterImg_info[1], $pct);
}
/**
* Add text watermark
*/
private function waterstr ()
{
$rect = imagettfbbox($this -> fontSize, 0, $this -> fontFile, $this -> waterStr);
$w = abs($rect[2] - $rect[6]);
$h = abs($rect[3] - $rect[7]);
$fontHeight = $this -> fontSize;
$this -> water_im = imagecreatetruecolor($w, $h);
imagealphablending($this -> water_im, false);
imagesavealpha($this -> water_im, true);
$white_alpha = imagecolorallocatealpha($this -> water_im, 255, 255, 255, 127);
imagefill($this -> water_im, 0, 0, $white_alpha);
$color = imagecolorallocate($this -> water_im, $this -> fontColor[0], $this -> fontColor[1],
$this -> fontColor[2]);
imagettftext($this -> water_im, $this -> fontSize, 0, 0, $this -> fontSize, $color,
$this -> fontFile, $this -> waterStr);
$this -> waterImg_info = array (
0 => $w, 1 => $h
);
$this->waterimg();
}
/**
* * Watermark image output
*/
public function output ()
{
$this->imginfo();
if ($this -> waterType == 0) {
$this->waterstr();
} else {
$this->waterimginfo();
$this->waterimg();
}
switch ($this -> srcImg_info[2]) {
case 3 :
imagepng($this -> im, $this -> srcImg);
break 1;
case 2 :
imagejpeg($this -> im, $this -> srcImg);
break 1;
case 1 :
imagegif($this -> im, $this -> srcImg);
break 1;
default :
die('Failed to add watermark!');
break;
}
//Subsequent destruction processing after image synthesis
imagedestroy($this -> im);
imagedestroy($this -> water_im);
}
}
Usage example:
//instantiate object
$obj = new WaterMask('img/10451.jpg');
//Type: 0 is text watermark, 1 is picture watermark$obj->waterType = 0;
//Watermark transparency, the smaller the value, the higher the transparency
$obj->transparent = 15;
//Watermark text//$obj->waterStr = 'Happy Birthday';
//Watermark image
//$obj->waterImg = '';//Watermark image
//Text font size
$obj->fontSize = 14;
//Watermark text color (RGB)
$obj->fontColor = array(255,255,100);
//Font file$obj->fontFile = 'STCAIYUN.ttf';
//The output watermark image file overwrites the input image file
$obj->output();
The usage is relatively simple and very practical.
Author ltx851201
http://www.bkjia.com/PHPjc/478502.htmlwww.bkjia.com
true

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools