thinkphp watermark function to fix PNG transparent watermark and increase JPEG image quality Adjustable _php instance

Source: Internet
Author: User
Tags imagecopy imagejpeg save file transparent watermark

This article illustrates the method of thinkphp watermark to fix PNG transparent watermark and increase the quality of JPEG image. Share to everyone for your reference. The implementation methods are as follows:

TP comes with the picture class, has the function which adds the watermark to the picture.
Here the perfect:
1. PNG Watermark Transparent
2. Quality adjustment after watermark (jpg format only)
The code is as follows:

Copy Code code as follows:
/**
+ ———————————————————-
* Add a watermark to a picture
+ ———————————————————-
* @static Public
+ ———————————————————-
* @param string $source The original file name
* @param string $water watermark Picture
* @param string $ $savename The name of the picture after the watermark is added
* @param string $alpha The transparency of the watermark
+ ———————————————————-
* @return String
+ ———————————————————-
* @throws thinkexecption
+ ———————————————————-
*/
static public Function water ($source, $water, $savename =null, $alpha =80) {
Check to see if a file exists
if (!file_exists ($source) | | |!file_exists ($water))
return false;

Picture information
$sInfo = Self::getimageinfo ($source);
$wInfo = Self::getimageinfo ($water);

If the picture is smaller than the watermark picture, no picture is generated
if ($sInfo ["width"] < $wInfo ["width"] | | $sInfo [' height '] < $wInfo [' height ']]
return false;

Creating images
$sCreateFun = "Imagecreatefrom". $sInfo [' type '];
$sImage = $sCreateFun ($source);
$wCreateFun = "Imagecreatefrom". $wInfo [' type '];
$wImage = $wCreateFun ($water);

Set the blending mode of the image
Imagealphablending ($wImage, true);

Image position, default to right of bottom right corner
$posY = $sInfo ["Height"]– $wInfo ["height"];
$posX = $sInfo ["width"]– $wInfo ["width"];

* * To keep PNG transparent effect using imagecopy here for modified * *
Imagecopy ($sImage, $wImage, $posX, $posY, 0, 0, $wInfo [' width '], $wInfo [' height ']);
Generates a mixed image, which 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 given, the default is the original image name
if (! $savename) {
$savename = $source;
@unlink ($source);
}
Save the image, if it is JPG, set a launch quality here for the modified:
if ($sInfo [' type '] = = "JPG" | | $sInfo [' type '] = = "jpeg") {
Imagejpeg ($sImage, $savename, 90);//3rd parameter even if the mass size, because only imagejpeg support this parameter
} else {
$ImageFun ($sImage, $savename);
}
$ImageFun ($sImage, $savename);//This is the system.
Imagedestroy ($sImage);
}

I hope this article will be helpful to everyone's thinkphp framework program design.

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.