This article mainly introduces the ThinkPHP watermark function to fix PNG transparent watermarks and increase the JPEG image quality adjustable, including modifying the multi-layer watermark setting code, the modified part is described in more detail in the annotations, which is of great practical value, for more information about how to use the ThinkPHP watermark function to fix PNG transparent watermarks and increase the adjustable JPEG image quality, see the example below. 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:
The 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.