Home > Backend Development > PHP Tutorial > ThinkPHP watermark function repairs PNG transparent watermarks and increases JPEG image quality with adjustable quality, _PHP tutorial

ThinkPHP watermark function repairs PNG transparent watermarks and increases JPEG image quality with adjustable quality, _PHP tutorial

WBOY
Release: 2016-07-13 10:15:06
Original
1267 people have browsed it

ThinkPHP watermark function can repair PNG transparent watermarks and increase JPEG image quality adjustment,

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:

Copy code The code is as follows:
/**
+————————————————————-
* Add watermark to pictures
+————————————————————-
* @static public
+————————————————————-
* @param string $source original file name
* @param string $water watermark image
* @param string $$savename Image name after adding watermark
* @param string $alpha watermark transparency
+————————————————————-
* @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 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/906679.htmlTechArticleThinkPHP watermark function realizes repairing PNG transparent watermark and increasing JPEG image quality adjustment. This article describes the implementation of ThinkPHP watermark function. Repair PNG transparent watermark and add adjustable JPEG image quality...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template