thinkphp watermark function to fix PNG transparent watermark and increase JPEG picture quality adjustable _php instance

Source: Internet
Author: User
Tags imagecopy imagejpeg transparent watermark
In this paper, the thinkphp watermark function is introduced to repair PNG transparent watermark and to increase the quality of JPEG image can be adjusted. Share to everyone for your reference. The implementation method is as follows:

TP comes with the picture class, has the function of adding watermark to the picture.
Here are the perfect:
1. PNG watermark Transparency
2. Watermark quality adjustment (jpg format only)
The code is as follows:

Copy the 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 picture name 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 if the 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, the picture is not 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);

To set the color blending mode of an image
Imagealphablending ($wImage, true);

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

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

It is hoped that this article will be helpful to everyone's thinkphp framework 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.