如何在上传时调整图像大小yii2

时间:2017-12-29 14:31:58

标签: yii yii2

这是我的帖子控制器功能。我总是上传少于200x200的图片,这些图片会存储在' 文件夹中。上传图片后,ID号将更改为4546464.png。但我希望在上传后将图片尺寸更改为60x60,并在将尺寸和质量更改为60x60x30后将其存储。此代码上传很好但不会改变大小和质量。

public function actionCreate() {
    $model = new Monitor();

    if ($model->load(Yii::$app->request->post())) {
        if ($model->payment_processor) {
            $model->payment_processor = implode(',', $model>payment_processor);
        }

        $model->file = UploadedFile::getInstance($model, 'file');

        if ($model->file != '') {
            $model->image = time() . '.' . $model->file->extension;
        }

        $model->update_at = date('Y-m-d h:i:s');
        $model->save();

        if ($model->file != '') {             
            $model->file->saveAs('upload/' . $model->image);               
        }
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', ['model' => $model]);
    }
}

2 个答案:

答案 0 :(得分:0)

,您必须先在服务器中上传图片,然后将其大小调整为您想要的尺寸,另一种方法是在您之前

 $model->file->saveAs('upload/' . $model->image);

调整大小,但我建议您保存原始文件,然后将其作为副本调整大小。

这里是从中心调整大小和裁剪图像的功能:

public static function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 100){
    $quality = 10;
    $imgsize = getimagesize($source_file);
    $width = $imgsize[0];
    $height = $imgsize[1];
    $mime = $imgsize['mime'];

    switch($mime){
        case 'image/gif':
            $image_create = "imagecreatefromgif";
            $image = "imagegif";
            break;

        case 'image/png':
            $image_create = "imagecreatefrompng";
            $image = "imagepng";
            $quality = 9;
            break;

        case 'image/jpeg':
            $image_create = "imagecreatefromjpeg";
            $image = "imagejpeg";
            $quality = 100;
            break;



        default:
            return false;
            break;
    }

    $dst_img = imagecreatetruecolor($max_width, $max_height);
    $src_img = $image_create($source_file);

    $width_new = $height * $max_width / $max_height;
    $height_new = $width * $max_height / $max_width;
    //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa
    if($width_new > $width){
        //cut point by height
        $h_point = (($height - $height_new) / 2);
        //copy image
        imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);
    }else{
        //cut point by width
        $w_point = (($width - $width_new) / 2);
        imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height);
    }

    $image($dst_img, $dst_dir, $quality);

    if($dst_img)imagedestroy($dst_img);
    if($src_img)imagedestroy($src_img);
}

尽可能轻松地使用此功能:

yourModel::resize_crop_image(150,150, $path, 'upload/'.$name_you_want_or_random_string.'.jpg',$q);

$ path是上传的原始文件的路径。 您必须在Web应用程序目录中创建上传文件夹,或将目标更改为您想要的位置。

答案 1 :(得分:0)

我正在使用imageprocessor扩展程序,我对它非常满意。您只需要向图像处理器组件添加配置,并且在保存时您必须调用组件的save方法。它将根据您配置的尺寸创建新图像。它有一个很好的文档。试一试。