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.