Yii:任何以压缩形式保存图像的方法? [英] Yii: any way to save the images in compressed form?

查看:379
本文介绍了Yii:任何以压缩形式保存图像的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大尺寸的图像,我想在保存到数据库之前压缩它们。这是我的控制器,有没有办法在没有任何扩展的情况下做到这一点?

I have images having huge size, I want to compress them before they save in database. Here is my controller, is there any way to do this without any extension?

public function actionCreate()
{
    $model = new Business;
    if (isset($_POST['Business'])) {
        $rnd = rand(0, 9999);

        $model->attributes = $_POST['Business'];

        $uploadedFile = CUploadedFile::getInstance($model, 'image');
        $fileName = "{$rnd}-{$uploadedFile}";

        $model->image = $fileName;
        if ($model->save()) {
            $uploadedFile->saveAs(Yii::app()->basePath . '/../img/' . $fileName);
            $this->redirect(array('view', 'id' => $model->id));
        }
    }

    $this->render('create', array(
        'model' => $model,
    ));
}


推荐答案

1)您需要下载< a href =https://github.com/zhdanovartur/yii-easyimage =nofollow>这个扩展并将其连接到配置文件中。
2)上传原始文件后,您只需调用方法 resize()。这是作品。我检查了一下。在你的情况下,它将是这样的:

1) You need download this extension and connect it in config like in documentation. 2) After upload original file you need just call method resize(). It is works. I checked it. In your case it will be something like this:

public function actionCreate()
{
    $model = new Business;
    if (isset($_POST['Business'])) {
        $rnd = rand(0, 9999);

        $model->attributes = $_POST['Business'];

        $uploadedFile = CUploadedFile::getInstance($model, 'image');
        $fileName = "{$rnd}-{$uploadedFile}";

        $model->image = $fileName;
        if ($model->validate()) {
           $path = Yii::app()->basePath . '/../img/' . $fileName;

           $uploadedFile->saveAs($path);

           $image = new EasyImage($path);
           $thumbPath = Yii::app()->basePath . '/../thumb/' . $fileName;

           $image->resize(100, 100);
           $image->save($thumbPath);

           $model->save();
           $this->redirect(array('view', 'id' => $model->id));
       }
   }

   $this->render('create', array(
       'model' => $model,
   ));
} 

这篇关于Yii:任何以压缩形式保存图像的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆