我们在截图的时候,如果给定的尺寸比例与原图比例不等,那么情况就是缩略出来的图片总有一边是不理想的, 可能高度不够,也可能宽度不够,最近项目用到图片缩放,发现了这个问题,于是重写了框架里的图片缩放方法,大家可能都知道电脑设置壁纸模式的时候有一项为“填充”,这种模式保证了你的屏幕不会出现空白,图片会自动调整到填满屏幕大小,那么下面这个方法加入了这一种模式,保证你需要的尺寸一定会有图,而不是出现黑边或者尺寸不理想等情况。
启用填充模式需要设置:$scalemode=false;
以下方法只是框架图片类中的一部分:
/** * 生成缩略图 * @author joychao * @static * @param string $image?原图 * @param string $type图像格式 * @param string $maxwidth? 目标最大宽度 * @param string $maxheight?目标最大高度 * @param string $prefix目标后缀 * @param boolean $scalemode 按比例切割模式 * @param boolean $saveimg? 生成后是否保存文件 * @param boolean $interlace 启用隔行扫描 * @return void */? static function thumbnail($image,$type='',$maxwidth=200,$maxheight=50,$prefix='_thumb',$scalemode=false,$saveimg=true,$interlace=true)? {? // 获取原图信息? $info? = self::getinfo($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);? // 载入原图? $createfun = 'imagecreatefrom'.($type=='jpg'?'jpeg':$type);? $srcimg = $createfun($image);? if($scalemode){//按比例切图? $scale = min($maxwidth/$srcwidth, $maxheight/$srcheight); // 计算缩放比例? if($scale>=1) {? // 超过原图大小不再缩略? $width =? $srcwidth;? $height? =? $srcheight;? }else{? // 缩略图尺寸? $width? = (int)($srcwidth*$scale);? $height = (int)($srcheight*$scale);? }? }else{//不按比例切图? $newscale=$maxwidth/$maxheight;? $srcscale=$srcwidth/$srcheight;? if($srcscale>=$newscale){? $height=$maxheight;? $width=intval($height*($srcwidth/$srcheight));? }else{? $width=$maxwidth;? $height=intval($width/($srcwidth/$srcheight));? }? $thumbimgfirst = imagecreatetruecolor($width, $height);? imagecopyresampled($thumbimgfirst, $srcimg, 0, 0, 0, 0, $width, $height, $srcwidth,$srcheight);? if($srcscale>=$newscale){? //原图起点? $startx=intval(($width-$maxwidth)*0.5);? $starty=0;? $width=$maxwidth;? }else{? //原图起点? $startx=0;? $starty=intval(($height-$maxheight)*0.5);? $height=$maxheight;? }? }? //创建缩略图? if($type!='gif' && function_exists('imagecreatetruecolor')){? $thumbimg = imagecreatetruecolor($width, $height);? }else{? $thumbimg = imagecreate($width, $height);? }? // 复制图片? if(!$scalemode){? imagecopymerge($thumbimg, $thumbimgfirst, 0, 0, $startx, $starty, $width, $height, 100);? }else{? 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);? $thumbname=preg_replace('/(\.\w+)$/', $prefix.'\\1', $image);? //abs_thumb.jpg? if($saveimg)//是否需要保存文件? $imagefun($thumbimg,$thumbname);? else? $imagefun($thumbimg);? imagedestroy($thumbimg);? imagedestroy($srcimg);? return $thumbname;? }? return false;? }? /** * 获取图片信息 * @static * @param string $path 图片路径 * @return array */? static function getinfo($path){? $arr=getimagesize($path);? $info['width']=$arr[0];? $info['height']=$arr[1];? $info['mime']=$arr['mime'];? return $info;? }
如果在使用过程中遇到什么问题请留言或评论。
原文地址:[分享]一个强大的图片缩放类,支持填充缩放模式, 感谢原作者分享。
