andrian
andrian

Reputation: 533

resize upload image yii

How to resize an upload image with 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'));
            }
    }

I have an image with 3000x4000 pixels, when uploading the image directly resize to 768x1024 pixels. I'm trying with $model->resize(768, 1024); but error with behaviors do not have a method or closure named "resize".

Upvotes: 2

Views: 8819

Answers (1)

Telvin Nguyen
Telvin Nguyen

Reputation: 3559

You have to use extension instead. You should have found many resources to achieve your purpose. Here are some examples

image-resize-on-the-fly

upload-and-resize-image-using-yii

Look at a proportion of code

    $image = Yii::app()->image->load($name);    
    $image->resize(768, 1024);    
    $image->save();

Upvotes: 4

Related Questions