zoukankan      html  css  js  c++  java
  • Yii 框架生成缩略图

    控制器

    if($model->load(Yii::$app->request->post()))
            {

                //原图
                $model->img = UploadedFile::getInstance($model, 'img');
                $imgname=$model->img->name = time().$model->img->name;
        
                //$model->img->saveAs('./upload/image/' . $model->img->baseName . '.' . $model->img->extension);
                
                //设定上传目录
                $path=Yii::$app->basePath.'/web/upload/image/'.$imgname;
                //上传原图 图片
                $model->img->saveAs($path);  

                //获取上传原图文件的名称   
                $image=$model->img;
                $img=$image->name;
                //echo $img;die;            
                //利用扩展生成缩略图
                $im = null;
                //取图片类型
                $imagetype = strtolower($model->img->extension);
                if($imagetype == 'gif')
                    $im = imagecreatefromgif($path);
                else if ($imagetype == 'jpg')
                    $im = imagecreatefromjpeg($path);
                else if ($imagetype == 'png')
                    $im = imagecreatefrompng($path);
               
                //按时间生成新文件名
                $newName=$model->img->baseName.'_thumb_'.'.'.$model->img->extension;
                //设定缩略图的存放目录+名字
                $thumb_path=Yii::$app->basePath.'/web/upload/image/image_thumb/'.$newName;
                //生成缩略图
                $sql=$model->resizeImage($im,800,450,$thumb_path,$model->img->extension);
        }

    在model 模型层里添加 resizeImage 方法

     public static function resizeImage($im, $maxwidth, $maxheight, $name,$filetype) {
            $pic_width = imagesx ( $im );
            $pic_height = imagesy ( $im );

            if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
                if ($maxwidth && $pic_width > $maxwidth) {
                    $widthratio = $maxwidth / $pic_width;
                    $resizewidth_tag = true;
                }
                
                if ($maxheight && $pic_height > $maxheight) {
                    $heightratio = $maxheight / $pic_height;
                    $resizeheight_tag = true;
                }
                
                if ($resizewidth_tag && $resizeheight_tag) {
                    if ($widthratio < $heightratio)
                        $ratio = $widthratio;
                    else
                        $ratio = $heightratio;
                }
                
                if ($resizewidth_tag && ! $resizeheight_tag)
                    $ratio = $widthratio;
                if ($resizeheight_tag && ! $resizewidth_tag)
                    $ratio = $heightratio;
                
                $newwidth = $pic_width * $ratio;
                $newheight = $pic_height * $ratio;
                
                if (function_exists ( "imagecopyresampled" )) {
                    $newim = imagecreatetruecolor ( $newwidth, $newheight );
                    imagecopyresampled ( $newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height );
                } else {
                    $newim = imagecreate ( $newwidth, $newheight );
                    imagecopyresized ( $newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height );
                }
                
                imagejpeg ( $newim, $name );
                imagedestroy ( $newim );
            } else {
                //$name = $name . $filetype;//这句好像多余了
                imagejpeg ( $im, $name );
            }
        }

    很简单,,这样就可以了

  • 相关阅读:
    Linux下架构高可用性网络----HA+LB+lvs
    MacBook如何用Parallels Desktop安装windows7/8
    Win10如何彻底禁用小娜?彻底禁用小娜的方法
    安卓手机微信发不出去怎么办 微信不能发信息怎么办
    计算机名、主机名、用户账户名与NetBIOS名有什么区别
    安装corel x8提示你已安装了另外一个版本
    ssh整合问题总结--在添加商品模块实现图片(文件)的上传
    代理设计模式之静态代理与动态代理(超..)详解
    Java基础--反射机制的知识点梳理
    ssh整合问题总结--运行项目时报java.lang.StackOverflowError(堆栈溢出)异常
  • 原文地址:https://www.cnblogs.com/bluealine/p/5386282.html
Copyright © 2011-2022 走看看