7

I use the bellow function to resize images after upload to show on my post. but it works just for images larger than 500px 300px. when I upload image smaller than this size, my website images row breaks down.

use yii\imagine\Image;    
public function upload() {
            $this->pictureFile->saveAs('../files/upload/' . $this->pictureFile->baseName . '.' . $this->pictureFile->extension);

            Image::thumbnail('../files/upload/' . $this->pictureFile, 500, 300)
                    ->save('../files/upload/thumbnail-500x300/' . $this->pictureFile->baseName . '.' . $this->pictureFile->extension, 
                            ['quality' => 70]);
            unlink('../files/upload/' . $this->pictureFile->baseName . '.'  . $this->pictureFile->extension);
        }
Mohammad Aghayari
  • 1,010
  • 3
  • 15
  • 38

4 Answers4

4

Instead of Image::thumbnail, try the following

$imagine = Image::getImagine();
$image = $imagine->open('../files/upload/' . $this->pictureFile);
$image->resize(new Box(500, 300))->save('../files/upload/thumbnail-500x300/' . $this->pictureFile->baseName . '.' . $this->pictureFile->extension, ['quality' => 70]);

Haven't tested it but since yii's Image is just a wrapper over Imagine library, this should work with minor changes (if at all needed).

And yes, you need to use Imagine\Image\Box; in your file before using the code above.

yetanotherse
  • 500
  • 3
  • 16
3

Use resize method as below

 use yii\imagine\Image;  
 use Imagine\Image\Box;  

 public function upload() {
        $this->pictureFile->saveAs('../files/upload/' . $this->pictureFile->baseName . '.' . $this->pictureFile->extension);

        Image::thumbnail('../files/upload/' . $this->pictureFile, 500, 300)
                ->resize(new Box(500,300))
                ->save('../files/upload/thumbnail-500x300/' . $this->pictureFile->baseName . '.' . $this->pictureFile->extension, 
                        ['quality' => 70]);
        unlink('../files/upload/' . $this->pictureFile->baseName . '.'  . $this->pictureFile->extension);
    }
ck_arjun
  • 1,367
  • 1
  • 11
  • 19
1
    Yii::setAlias('newsfolder', dirname(dirname(__DIR__)) . '/frontend/web/extraimages/');

    $model->img = UploadedFile::getInstance($model,'img');
    if (!empty($model->img)){
        $model->img->saveAs( Yii::getAlias('@newsfolder/').$filename.'.'.$model->img->extension );
        $model->img =  $filename.'.'.$model->img->extension;
        $imagine = Image::getImagine();
        $image = $imagine->open(Yii::getAlias('@newsfolder/'.$model->img));
        $image->resize(new Box(500, 300))->save(Yii::getAlias('@newsfolder/'.$model->img, ['quality' => 70]));
    }
Dmitry
  • 242
  • 1
  • 9
  • 20
Davron Achilov
  • 536
  • 4
  • 14
0
$imagine = Image::getImagine();
        $imagine = $imagine->open($openPath);
        $sizes = getimagesize ( $openPath ); 
        /*                   
           [0] => 604
           [1] => 244
           [2] => 3
           [3] => width="604" height="244"
           [bits] => 8
           [mime] => image/png
        ) */
        $width = 200;
        $height = round($sizes[1]*$width/$sizes[0]);        
        $imagine = $imagine->resize(new Box($width, $height))->save($savePath, ['quality' => 60]); 
pedro casas
  • 147
  • 5