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.