解决thinkphp图片上传生成缩略图背景为黑色的问题
今天前台改版,于是要重新上传图片,发现以前上传的图片生成缩略图后背景都变成 黑色的了,很难看,于是追了下代码(@.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;
}声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: php过滤字符串中重复的字符(包含中文)
- 下一篇:没有了
