ThinkPHP水印功能实现修复PNG透明水印并增加JPEG图片质量可调整,_PHP教程

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

ThinkPHP水印功能实现修复PNG透明水印并增加JPEG图片质量可调整,

本文实例讲述了ThinkPHP水印功能实现修复PNG透明水印并增加JPEG图片质量可调整的方法。分享给大家供大家参考。具体实现方法如下:

TP自带有图片类,有给图片加水印的功能。
这里完善了:
1. png水印透明
2. 加水印后质量调整(只限于JPG格式)
代码如下:

复制代码 代码如下:
/**
+———————————————————-
* 为图片添加水印
+———————————————————-
* @static public
+———————————————————-
* @param string $source 原文件名
* @param string $water 水印图片
* @param string $$savename 添加水印后的图片名
* @param string $alpha 水印的透明度
+———————————————————-
* @return string
+———————————————————-
* @throws ThinkExecption
+———————————————————-
*/
static public function water($source, $water, $savename=null, $alpha=80) {
//检查文件是否存在
if (!file_exists($source) || !file_exists($water))
return false;

//图片信息
$sInfo = self::getImageInfo($source);
$wInfo = self::getImageInfo($water);

//如果图片小于水印图片,不生成图片
if ($sInfo["width"] return false;

//建立图像
$sCreateFun = "imagecreatefrom" . $sInfo['type'];
$sImage = $sCreateFun($source);
$wCreateFun = "imagecreatefrom" . $wInfo['type'];
$wImage = $wCreateFun($water);

//设定图像的混色模式
imagealphablending($wImage, true);

//图像位置,默认为右下角右对齐
$posY = $sInfo["height"] – $wInfo["height"];
$posX = $sInfo["width"] – $wInfo["width"];

/* 为了保持PNG的透明效果 使用imagecopy 此处为修改过的*/
imagecopy($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'], $wInfo['height']);
//生成混合图像,这是系统的
// imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'],$wInfo['height'], $alpha);
//输出图像
$ImageFun = 'Image' . $sInfo['type'];
//如果没有给出保存文件名,默认为原图像名
if (!$savename) {
$savename = $source;
@unlink($source);
}
//保存图像,如果是jpg,则设置一下水印质量 此处为修改过的:
if ($sInfo['type'] == "jpg" || $sInfo['type'] == "jpeg") {
imagejpeg($sImage, $savename, 90);//第3个参数即使质量大小,因为只有imagejpeg支持这个参数
} else {
$ImageFun($sImage, $savename);
}
//$ImageFun($sImage, $savename);//这是系统的
imagedestroy($sImage);
}

希望本文所述对大家的ThinkPHP框架程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/906679.htmlTechArticleThinkPHP水印功能实现修复PNG透明水印并增加JPEG图片质量可调整, 本文实例讲述了ThinkPHP水印功能实现修复PNG透明水印并增加JPEG图片质量可调...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!