Search code examples
yii2

why did Yii2 imagine gives Class 'common\models\Box' not found error


I'm new to Yii framework & i'm learning this by doing a simple project. In this project i have used extension called yiisoft/yii2-imagine to reduce the resolution of the image but this gives me unknown error.

My code to resize the image :

use yii\imagine\Image;

Image::getImagine()
      ->open($thumbnail_path.'/'.$this->video_id.'.jpg')
      ->thumbnail(new Box(500,500))
      ->save();

Error

Class 'common\models\Box' not found

How i can fix this error ?


Solution

  • Looks like you've forgotten to specify what Box class should be used. By default the php is looking for Box class in your current namespace, so it's looking for common\models\Box. I guess you are trying to use Imagine's Imagine\Image\Box class so you need to add a use statement like this:

    use Imagine\Image\Box;
    

    The other option is to use the fully qualified class name in your code:

    use yii\imagine\Image;
    
    Image::getImagine()
          ->open($thumbnail_path.'/'.$this->video_id.'.jpg')
          ->thumbnail(new \Imagine\Image\Box(500,500))
          ->save();