The ThinkPHP watermark function is used to repair PNG transparent watermarks and increase the JPEG image quality.

Source: Internet
Author: User
Tags imagecopy imagejpeg
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.