The example in this article describes the method of using the ThinkPHP watermark function to repair PNG transparent watermarks and increase the quality of JPEG images. Share it with everyone for your reference. The specific implementation method is as follows:
TP comes with a picture class and has the function of adding watermarks to pictures.
Completed here:
1. png watermark transparent
2. Quality adjustment after adding watermark (only in JPG format)
The code is as follows:
//Picture information
$sInfo = self::getImageInfo($source);
$wInfo = self::getImageInfo($water);
//If the image is smaller than the watermark image, no image will be generated
if ($sInfo["width"] < $wInfo["width"] || $sInfo['height'] < $wInfo['height'])
return false;
//Create image
$sCreateFun = "imagecreatefrom" . $sInfo['type'];
$sImage = $sCreateFun($source);
$wCreateFun = "imagecreatefrom" . $wInfo['type'];
$wImage = $wCreateFun($water);
//Set the color mixing mode of the image
imagealphablending($wImage, true);
//Image position, the default is the lower right corner right-aligned
$posY = $sInfo["height"] – $wInfo["height"];
$posX = $sInfo["width"] – $wInfo["width"];
/* In order to maintain the transparency of PNG, use imagecopy. Here is the modified one*/
imagecopy($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'], $wInfo['height']);
//Generate mixed images, this is systematic
// 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 the watermark quality. Here is the modified one:
if ($sInfo['type'] == "jpg" || $sInfo['type'] == "jpeg") {
imagejpeg($sImage, $savename, 90);//The third parameter is the quality, because only imagejpeg supports this parameter
} else {
$ImageFun($sImage, $savename);
}
//$ImageFun($sImage, $savename);//This is system
imagedestroy($sImage);
}
I hope this article will be helpful to everyone’s ThinkPHP framework programming.