yii2 缩略图功能的实现

yii2 缩略图功能
这是缩略图的代码,我这里并没有直接去作为一个插件去使用,而是作为一个models 来使用

<?php

namespace app\models; // 这里的命名空间根据自己的配置,自己来修改

use Yii;

/**
 * This is the model class for table "information".
 *
 * @property integer $id
 * @property string $name
 * @property string $icon
 * @property string $bigImage
 * @property integer $mbuyId
 * @property string $smallTitle
 * @property string $content
 * @property integer $browseNum
 * @property integer $praiseNum
 * @property integer $commentNum
 * @property integer $status
 * @property integer $recommend
 * @property integer $recitems
 * @property string $createtime
 * @property string $remarks
 * @property string $username
 */
class SmallImage extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    private $src;
    private $imageinfo;
    private $image;
    public  $percent = 0.1;
    public function __construct($src){

        $this->src = $src;

    }
    public static function tableName()
    {
        return 'information';
    }

    /**
     * @inheritdoc
     */

    /**
     * @inheritdoc
     */

    public function openImage(){
        list($width, $height, $type, $attr) = getimagesize($this->src);
        $this->imageinfo = array(

            'width'=>$width,
            'height'=>$height,
            'type'=>image_type_to_extension($type,false),
            'attr'=>$attr
        );
        $fun = 'imagecreatefrom'.$this->imageinfo['type'];

        $this->image = $fun($this->src);
    }
    /**
    操作图片
     */
    public function thumpImage(){

        $new_width = $this->imageinfo['width'] * $this->percent;
        $new_height = $this->imageinfo['height'] * $this->percent;
        $image_thump = imagecreatetruecolor($new_width,$new_height);
        //将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度
        imagecopyresampled($image_thump,$this->image,0,0,0,0,$new_width,$new_height,$this->imageinfo['width'],$this->imageinfo['height']);
        imagedestroy($this->image);

         $this->image =   $image_thump;



    }
    /**
    输出图片
     */
    public function showImage(){

        header('Content-Type: image/'.$this->imageinfo['type']);
        $funcs = "image".$this->imageinfo['type'];
        $funcs($this->image);

    }
    /**
    保存图片到硬盘
     */
    public function saveImage($sname){

        $funcs = "image".$this->imageinfo['type'];
        $funcs($this->image,$sname.$this->imageinfo['type']);

    }
    /**
    销毁图片
     */
    public function __destruct(){

        imagedestroy($this->image);
    }
}

把上面代码放入models 之后,然后就可以在控制器里面写代码了
这里是我的控制器的代码
$image = new SmallImage($src); //实例化图片models
$image->percent = 0.5625; // 这里是宽高比,根据自己需要来调整
$image->openImage(); //打开图片
$image->thumpImage();//生成缩略图
$name = time();
$sname = 'uploads/small/'.$name.'.'; //这是我自己写的路径
$mname = Yii::$app->request->hostInfo.Yii::$app->request->baseUrl.'/'.'uploads/small/'.$name.'.'.    //这里是写的一个路径,为了缩略图的
                            'jpeg';
$image->saveImage($sname); //保存缩略图到上述的路径下
 $model->icon=$mname; //这里是yii2的保存

喜欢的可以收藏下,我会不定时更新一些实用的东西

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值