今天前台改版,于是要重新上传图片,发现以前上传的图片生成缩略图后背景都变成 黑色的了,很难看,于是追了下代码(@.ORG.Util.Image),发现生成缩略图的thumb方法中使用imagecreatetruecolor方法来生成一个图像资源,但此资源的背景永远都是黑色的,无论你怎么改,于是改为imagecreate就ok了。重新上传图片,生成缩略图,发现还是有个小问题,就是周边有白边,显得有些突兀,于是把代码里的生成透明图像的imagecreatetruecolor的顺序给替换了具体见下面代码。
/**
+----------------------------------------------------------
* 生成缩略图
+----------------------------------------------------------
* @static
* @access public
+----------------------------------------------------------
* @param string $image 原图
* @param string $type 图像格式
* @param string $thumbname 缩略图文件名
* @param string $maxWidth 宽度
* @param string $maxHeight 高度
* @param string $position 缩略图保存目录
* @param boolean $interlace 启用隔行扫描
+----------------------------------------------------------
* @return void
+----------------------------------------------------------
*/
static function thumb($image, $thumbname, $type = '', $maxWidth = 200, $maxHeight = 50, $interlace = true) {
// 获取原图信息
$info = Image::getImageInfo($image);
if ($info !== false) {
$srcWidth = $info['width'];
$srcHeight = $info['height'];
$type = empty($type) ? $info['type'] : $type;
$type = strtolower($type);
$interlace = $interlace ? 1 : 0;
unset($info);
$scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 计算缩放比例
if ($scale >= 1) {
// 超过原图大小不再缩略
$width = $srcWidth;
$height = $srcHeight;
} else {
// 缩略图尺寸
$width = (int) ($srcWidth * $scale);
$height = (int) ($srcHeight * $scale);
}
// 载入原图
$createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
$srcImg = $createFun($image);
ini_set("memory_limit", "50M");
//创建缩略图
#if($type!='gif' && function_exists('imagecreatetruecolor'))
# $thumbImg = imagecreatetruecolor($width, $height);
#else
$thumbImg = imagecreate($width, $height);
// 复制图片
if (function_exists("ImageCopyResampled"))
imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
else
imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
if ('gif' == $type || 'png' == $type) {
//imagealphablending($thumbImg, false);//取消默认的混色模式
//imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息
$background_color = imagecolorallocate($thumbImg, 0, 255, 0); // 指派一个绿色
imagecolortransparent($thumbImg, $background_color); // 设置为透明色,若注释掉该行则输出绿色的图
}
// 正方形填充
/* $length = abs($width - $height) / 2;
if ($width > $height) {
$size = $width;
$size_x = 0;
$size_y = $length;
}else {
$size = $height;
$size_x = $length;
$size_y = 0;
} */
$size_x = abs($maxWidth - $width) / 2;
$size_y = abs($maxHeight - $height) / 2;
// 创建新的画布
//$im = imagecreatetruecolor($size, $size);
#$im = imagecreatetruecolor($maxWidth, $maxHeight);
$im = imagecreate($maxWidth, $maxHeight);
// 矩形涂色
$white = imagecolorallocate($im, 255, 255, 255);
// imagefilledrectangle($im, 0, 0, $size, $size, $white);
imagefilledrectangle($im, 0, 0, $maxWidth, $maxHeight, $white);
// 拷贝图像到画布
imagecopy($im, $thumbImg, $size_x, $size_y, 0, 0, $width, $height);
if ('gif' == $type || 'png' == $type) {
$background_color = imagecolorallocate($im, 0, 255, 0); // 指派一个绿色
imagecolortransparent($im, $background_color); // 设置为透明色,若注释掉该行则输出绿色的图
}
$thumbImg = $im;
// 对jpeg图形设置隔行扫描
if ('jpg' == $type || 'jpeg' == $type)
imageinterlace($thumbImg, $interlace);
//$gray=ImageColorAllocate($thumbImg,255,0,0);
//ImageString($thumbImg,2,5,5,"ThinkPHP",$gray);
// 生成图片
$imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
$imageFun($thumbImg, $thumbname);
imagedestroy($thumbImg);
imagedestroy($srcImg);
return $thumbname;
}
return false;
}【update】今天在官网下载了3.1.3扩展包,里面就解决了上述问题,把代码贴下
static function thumb($image, $thumbname, $type='', $maxWidth=200, $maxHeight=50, $interlace=true) {
// 获取原图信息
$info = Image::getImageInfo($image);
if ($info !== false) {
$srcWidth = $info['width'];
$srcHeight = $info['height'];
$type = empty($type) ? $info['type'] : $type;
$type = strtolower($type);
$interlace = $interlace ? 1 : 0;
unset($info);
$scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 计算缩放比例
if ($scale >= 1) {
// 超过原图大小不再缩略
$width = $srcWidth;
$height = $srcHeight;
} else {
// 缩略图尺寸
$width = (int) ($srcWidth * $scale);
$height = (int) ($srcHeight * $scale);
}
// 载入原图
$createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
if(!function_exists($createFun)) {
return false;
}
$srcImg = $createFun($image);
//创建缩略图
if ($type != 'gif' && function_exists('imagecreatetruecolor'))
$thumbImg = imagecreatetruecolor($width, $height);
else
$thumbImg = imagecreate($width, $height);
//png和gif的透明处理 by luofei614
if('png'==$type){
imagealphablending($thumbImg, false);//取消默认的混色模式(为解决阴影为绿色的问题)
imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息(为解决阴影为绿色的问题)
}elseif('gif'==$type){
$trnprt_indx = imagecolortransparent($srcImg);
if ($trnprt_indx >= 0) {
//its transparent
$trnprt_color = imagecolorsforindex($srcImg , $trnprt_indx);
$trnprt_indx = imagecolorallocate($thumbImg, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
imagefill($thumbImg, 0, 0, $trnprt_indx);
imagecolortransparent($thumbImg, $trnprt_indx);
}
}
// 复制图片
if (function_exists("ImageCopyResampled"))
imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
else
imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
// 对jpeg图形设置隔行扫描
if ('jpg' == $type || 'jpeg' == $type)
imageinterlace($thumbImg, $interlace);
// 生成图片
$imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
$imageFun($thumbImg, $thumbname);
imagedestroy($thumbImg);
imagedestroy($srcImg);
return $thumbname;
}
return false;
}
今天开发公司项目的时候发现thinkphp的水印功能不支持png图片的原始透明,虽然支持png的透明度度处理但是去在图片原本的透明部分填充了白色,真的是令人蛋疼。 于是果断处理,发现thinkphp使用的是imagecopymerge函数,虽然imagecopymerge支持图片的透明处理但是却不保留png的原始透明度。于是果断改为先用imagecopy进行png图片拷贝然后用imagecopymerge,进行处理的方法,效果果然有效。
修改方法很简单,在image类里面添加一个imagecopymerge_alpha方法,随便找个位置加上下面的代码就可以了。
第二步用imagecopymerge_alpha替换原来的imagecopymerge处理,大约83行将
替换为
ok搞定,如果看不懂,或者觉得自己修改麻烦,那就直接复制下面的代码,替换Image.class.php内的所有代码