The ThinkPHP watermark function repairs PNG transparent watermarks and increases the JPEG image quality to be adjusted,
This document describes how to use the ThinkPHP watermark function to fix PNG transparent watermarks and increase the adjustable JPEG image quality. Share it with you for your reference. The specific implementation method is as follows:
TP comes with an image class and can be used to add watermarks to an image.
Here we have improved:
1. png watermark transparency
2. Post-watermark quality adjustment (only in JPG format)
The Code is as follows:
Copy codeThe Code is as follows :/**
+ --------------------
* Add a watermark to an image
+ --------------------
* @ Static public
+ --------------------
* @ Param string $ source original file name
* @ Param string $ water watermark image
* @ Param string $ savename watermark image name
* @ Param string $ transparency of the alpha watermark
+ --------------------
* @ Return string
+ --------------------
* @ Throws ThinkExecption
+ --------------------
*/
Static public function water ($ source, $ water, $ savename = null, $ alpha = 80 ){
// Check whether the file exists
If (! File_exists ($ source) |! File_exists ($ water ))
Return false;
// Image Information
$ SInfo = self: getImageInfo ($ source );
$ WInfo = self: getImageInfo ($ water );
// If the image is smaller than the watermark image, no image is generated.
If ($ sInfo ["width"] <$ wInfo ["width"] | $ sInfo ['height'] <$ wInfo ['height'])
Return false;
// Create an image
$ SCreateFun = "imagecreatefrom". $ sInfo ['type'];
$ SImage = $ sCreateFun ($ source );
$ WCreateFun = "imagecreatefrom". $ wInfo ['type'];
$ WImage = $ wCreateFun ($ water );
// Set the mixed color mode of the image
Imagealphablending ($ wImage, true );
// Image position, right-aligned by default
$ PosY = $ sInfo ["height"]-$ wInfo ["height"];
$ PosX = $ sInfo ["width"]-$ wInfo ["width"];
/* Use imagecopy to ensure the transparency of PNG. This is the modified image */
Imagecopy ($ sImage, $ wImage, $ posX, $ posY, 0, 0, $ wInfo ['width'], $ wInfo ['height']);
// Generate a hybrid image. This is the system's
// Imagecopymerge ($ sImage, $ wImage, $ posX, $ posY, 0, 0, $ wInfo ['width'], $ wInfo ['height'], $ alpha );
// Output image
$ ImageFun = 'image'. $ sInfo ['type'];
// If no save file name is provided, the original image name is used by default.
If (! $ Savename ){
$ Savename = $ source;
@ Unlink ($ source );
}
// Save the image. If it is jpg, set the watermark quality here to the modified one:
If ($ sInfo ['type'] = "jpg" | $ sInfo ['type'] = "jpeg "){
Imagejpeg ($ sImage, $ savename, 90); // 3rd parameters, even if the quality is large, because only imagejpeg supports this parameter
} Else {
$ ImageFun ($ sImage, $ savename );
}
// $ ImageFun ($ sImage, $ savename); // This is the system's
Imagedestroy ($ sImage );
}
I hope this article will help you with ThinkPHP framework programming.