2

如何使用 yii 调整上传图片的大小?

    if(isset($_POST['ArsipSurat']))
    {
        $rnd = rand(0,9999); 
        $model->attributes=$_POST['ArsipSurat'];
        $uploadedFile=CUploadedFile::getInstance($model,'image');
        $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
        $model->image = $fileName;
        if(isset($uploadedFile)){

            $model->save();

            //$this->redirect(array('view','id'=>$model->no_surat));
            $uploadedFile->saveAs(Yii::app()->basePath.'../../images/arsip/'.$fileName); 
            //$this->redirect(array('admin'));
            $this->redirect(array('view','id'=>$model->no_surat));  
        }
            else if(empty($uploadedFile))  // check if uploaded file is set or not
            {
                $model->save();
                $this->redirect(array('view','id'=>$model->no_surat));                  
            }   
            else{
                //$this->render('create',array('model'=>$model));
                $this->redirect(array('admin'));
            }
    }

我有一张 3000x4000 像素的图片,上传图片时直接调整为 768x1024 像素。我正在尝试$model->resize(768, 1024);但错误behaviors do not have a method or closure named "resize".

4

1 回答 1

4

您必须改用扩展名。您应该已经找到了许多资源来实现您的目的。这里有些例子

即时调整图像大小

使用 yii 上传和调整图像大小

看一段代码

    $image = Yii::app()->image->load($name);    
    $image->resize(768, 1024);    
    $image->save();
于 2013-07-31T06:14:26.133 回答