zoukankan      html  css  js  c++  java
  • 一个强大的图片缩放类,支持填充缩放模式

    我们在截图的时候,如果给定的尺寸比例与原图比例不等,那么情况就是缩略出来的图片总有一边是不理想的, 可能高度不够,也可能宽度不够,最近项目用到图片缩放,发现了这个问题,于是重写了框架里的图片缩放方法,大家可能都知道电脑设置壁纸模式的时候有一项为“填充”,这种模式保证了你的屏幕不会出现空白,图片会自动调整到填满屏幕大小,那么下面这个方法加入了这一种模式,保证你需要的尺寸一定会有图,而不是出现黑边或者尺寸不理想等情况。

    启用填充模式需要设置:$scaleMode=false;

    以下方法只是框架图片类中的一部分:

             /**
             * 生成缩略图
             * @author Joychao<Joy#joychao.cc>
             * @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;
            }
  • 相关阅读:
    安装 kubenetes-dashboard
    使用kubeadm在Centos7上部署kubernetes1.21.1
    批量重命名
    is_valid重写。返回数据格式统一
    django-序列化参数为空报错问题
    单文件日志测试
    文本、目录 空间清理问题
    数仓数据
    滑动验证码(无原图片处理)
    鸟哥私房菜基础篇
  • 原文地址:https://www.cnblogs.com/gaohj/p/3165298.html
Copyright © 2011-2022 走看看