PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 12, 2022

[FIXED] How to resize image by imagine extension in yii2

 January 12, 2022     yii, yii-extensions, yii2, yii2-advanced-app     No comments   

Issue

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);
        }

Solution

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);
    }


Answered By - ck_arjun
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing