为什么 Yii2 想象会给出 Class 'common\models\Box' not found 错误

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

我是 Yii 框架的新手,我正在通过做一个简单的项目来学习它。在这个项目中,我使用了名为 yiisoft/yii2-imagine 的扩展来降低图像的分辨率,但这给了我未知的错误。

我调整图片大小的代码:

use yii\imagine\Image;

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

错误

Class 'common\models\Box' not found

我该如何解决这个错误?

您似乎忘记指定应该使用哪个框 class。默认情况下,php 正在您当前的命名空间中查找 Box class,因此它正在查找 common\models\Box。 我猜你正在尝试使用 Imagine 的 Imagine\Image\Box class 所以你需要添加这样的 use 语句:

use Imagine\Image\Box;

另一种选择是在您的代码中使用完全限定的 class 名称:

use yii\imagine\Image;

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