最近做项目 遇到个问题 就是用thinkphp的缩略图类 生成缩略图 比如我要生成230,230的 但是实际上Thinkphp提供的缩略图处理类是等比例截取的 不是按大小严格截取的,所以如果原图比例和你的缩略图比例不一致 可能会出现宽度或者高度略小的情况。
我想要的效果是 一定要缩略图的高度和宽度相等 少掉的部分 用白色背景填充 于是开始分析 他们的上传类
ORG.Net.UploadFile
UploadFile 这里类 里面 添加了一个属性 是否设置固定比例的 缩略图
//是否使用固定缩略图
public $is_fixed = false;
然后 找到生成缩略图的部分 179行 最后加上 $this->is_fixed
Image::thumb($filename,$thumbname,'',$thumbWidth[$i],$thumbHeight[$i],true,$this->is_fixed);
打开 缩略图类ORG.Util.Image
找到生成缩略图方法 修改为 thumb方法 我修改的部分都添加上了注释
/**
+----------------------------------------------------------
* 生成缩略图
+----------------------------------------------------------
* @static
* @access public
+----------------------------------------------------------
* @param string $image 原图
* @param string $type 图像格式
* @param string $thumbname 缩略图文件名
* @param string $maxWidth 宽度
* @param string $maxHeight 高度
* @param string $position 缩略图保存目录
* @param boolean $interlace 启用隔行扫描
* @param boolean $is_fixed 是否生成固定比例的缩略图
+----------------------------------------------------------
* @return void
+----------------------------------------------------------
*/
static function thumb($image, $thumbname, $type='', $maxWidth=200, $maxHeight=50, $interlace=true,$fixed = false) {
// 获取原图信息
$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);
//创建缩略图
if ($type != 'gif' && function_exists('imagecreatetruecolor'))
{
$thumbImg = imagecreatetruecolor($width, $height);
}else{
$thumbImg = imagecreate($width, $height);
}
/* 开始判断是否生成固定高宽的缩略图
* 作者:过往云烟
* 日期:2012-06-25
*/
if($fixed){
//创建个 固定的缩略图盒子
$thumbImg = imagecreatetruecolor($maxWidth,$maxHeight);
//盒子 背景色
$padColor = imagecolorallocate($thumbImg,255,255,255);
//盒子填充 box
imagefilledrectangle($thumbImg,0,0,$maxWidth,$maxHeight,$padColor);
//$imgWidth == $srcWidth $srcHeight
//$width = $maxWidth $height = $maxHeight
if($srcWidth >= $srcHeight)
{
$thumbWidth = $maxWidth;
$thumbHeight = ($maxWidth / $srcWidth) * $srcHeight;
}
else
{
$thumbWidth = ($maxHeight / $srcHeight) * $srcWidth;
$thumbHeight = $maxHeight;
}
if(function_exists('ImageCopyResampled')){
//生成固定比例的缩略图
imagecopyresampled($thumbImg, $srcImg, ($maxWidth-$thumbWidth)/2, ($maxHeight-$thumbHeight)/2, 0, 0, $thumbWidth, $thumbHeight, $srcWidth, $srcHeight);
}else{
imagecopyresized($thumbImg, $srcImg, ($maxWidth-$thumbWidth)/2, ($maxHeight-$thumbHeight)/2, 0, 0, $thumbWidth, $thumbHeight, $srcWidth, $srcHeight);
}
}else{
//如果不要固定生成 就还是按照他们以前的搞
// 复制图片 如果有 imagecopyresampled 函数的话 就用
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) {
$background_color = imagecolorallocate($thumbImg, 0, 255, 0); // 指派一个绿色
imagecolortransparent($thumbImg, $background_color); // 设置为透明色,若注释掉该行则输出绿色的图
}
// 对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;
}
这样就可以 了 最后在调用的时候 添加上这一句话就行了
$upload = new UploadFile();// 实例化上传类
$upload->is_fixed = true; //固定高宽
最终效果
至此 thinkphp 就支持等比例缩放 和固定缩放了 搞了半天搞出来了 顺便记录在日志里
过往云烟 2012/06/25