【问题标题】:Thumb create in Yii2 gives an error在 Yii2 中创建拇指会报错
【发布时间】:2015-07-04 15:29:26
【问题描述】:

我想创建一个拇指,但收到此错误:

PHP 致命错误 – yii\base\ErrorException 找不到类“Imagine\Image\ManipulatorInterface”

我不知道从哪里可以得到这个?我使用了yii2-imagine,但没有找到任何ManipulatorInterface 类。

控制器文件:

<?php
namespace backend\controllers;

use Yii;
use app\models\Employee;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\imagine\Image; // this is where i used Image class

public function actionCreate()
{
    $model = new Employee();
    $model->added_date_time = date('Y-m-d H:i:s');
    if($model->load(Yii::$app->request->post())) {
        $model->file = UploadedFile::getInstance($model,'avatar');

        if(!empty($model->file)) {
            $imageName = Yii::$app->security->generateRandomString();
            $model->file->saveAs('uploads/'.$imageName.'.'.$model->file->extension);
            $model->avatar = $imageName.'.'.$model->file->extension;
            $file = 'uploads/'.$imageName.'.'.$model->file->extension;
            Image::thumbnail($file, 200, 200)->save('uploads/thumb/', ['quality' => 80]);
        }
        if($model->save()){
            $this->redirect(\Yii::$app->urlManager->createUrl('employee'));
        }
    }
    else {
        return $this->render('create', [
            'model' =>  $model
            ]);
    }
}
?>

图像类文件在\vendor\yiisoft\yii2\imagine文件夹中,ManipulatorInterface\vendor\yiisoft\yii2\imagine\Image文件夹中

Image.php

<?php
namespace yii\Imagine;
class Image extends BaseImage
{
}
?>

BaseImage.php

<?php

namespace yii\imagine;

use Yii;
use imagine\Image\Box;
use imagine\Image\Color;
use imagine\Image\ImageInterface;
use imagine\Image\ImagineInterface;
use imagine\Image\ManipulatorInterface;
use imagine\Image\Point;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\helpers\ArrayHelper;

class BaseImage
{
    public static function thumbnail($filename, $width, $height, $mode = 'outbound')
    {
        $box = new Box($width, $height);
        $img = static::getImagine()->open(Yii::getAlias($filename));

        if (($img->getSize()->getWidth() <= $box->getWidth() && $img->getSize()->getHeight() <= $box->getHeight()) || (!$box->getWidth() && !$box->getHeight())) {
            return $img->copy();
        }

        $img = $img->thumbnail($box, $mode);

        // create empty image to preserve aspect ratio of thumbnail
        $thumb = static::getImagine()->create($box, new Color('FFF', 100));

        // calculate points
        $size = $img->getSize();

        $startX = 0;
        $startY = 0;
        if ($size->getWidth() < $width) {
            $startX = ceil($width - $size->getWidth()) / 2;
        }
        if ($size->getHeight() < $height) {
            $startY = ceil($height - $size->getHeight()) / 2;
        }

        $thumb->paste($img, new Point($startX, $startY));

        return $thumb;
    }
?>

【问题讨论】:

  • 我会说你有一个命名空间问题,但没有代码很难帮助你。您可以在您的问题中粘贴相关代码吗?
  • 我应该发布什么?
  • 抛出此错误的 php 代码
  • 好的,我会发布更详细的代码
  • @FélixGagnon-Grenier 请查看我的更新代码。

标签: php yii2


【解决方案1】:

我也有同样的问题。我尝试 'composer global require "fxp/composer-asset-plugin:~1.0.0"' 之后我再次尝试 'composer.phar require --prefer-dist yiisoft/yii2-imagine' 并且它对我有用。希望对你也有帮助

【讨论】:

  • 你知道为什么要安装composer-asset-plugin吗?安装 composer 后出现 OP 错误,如何在官方文档中解释
【解决方案2】:

你可以试试这个:

1) 将想象复制给您的供应商

`pathA: "vendor/Imagine/Draw...."`

2) 在config.php中添加别名

'components' => [
],

 ....

'aliases'=>[
    '@Imagine'=>'@vendor/Imagine', //this should correspond to pathA above
 ],

 ....

'params' => [
 ],

3) 检查vendor/composer/autoload_psr4.php 像这样配置它

return array(
    ...

    'yii\\imagine\\' => array($vendorDir . '/yiisoft/yii2-imagine'),

    ...

);

4) 将 yii-imagine 复制到 yiisoft 中

pathB:"vendor/yiisoft/yii-imagine"

那么你就可以使用它了。

【讨论】:

    【解决方案3】:

    我已决定在yiisoft 文件夹下的extensions.php 中设置路径。

    只需执行 2 个步骤:

    1. 打开extensions.php

    2. 在文件底部添加

      'yiisoft/yii2-imagine' => 
      array (
        'name' => 'yiisoft/yii2-imagine',
        'version' => '~2.0.0',
        'alias' => 
        array (
          '@yii/imagine' => $vendorDir . '/yiisoft/yii2-imagine',
          '@Imagine/Image' => $vendorDir . '/yiisoft/yii2-imagine/Imagine/Image',
          '@Imagine/Gd' => $vendorDir . '/yiisoft/yii2-imagine/Imagine/Gd',
          '@Imagine/Draw' => $vendorDir . '/yiisoft/yii2-imagine/Imagine/Draw',
          '@Imagine/Effects' => $vendorDir . '/yiisoft/yii2-imagine/Imagine/Effects',
          '@Imagine/Exception' => $vendorDir . '/yiisoft/yii2-imagine/Imagine/Exception',
          '@Imagine/Filter' => $vendorDir . '/yiisoft/yii2-imagine/Imagine/Filter',
          '@Imagine/Gmagick' => $vendorDir . '/yiisoft/yii2-imagine/Imagine/Gmagick',
          '@Imagine/Imagick' => $vendorDir . '/yiisoft/yii2-imagine/Imagine/Imagick',
          '@Imagine/resources' => $vendorDir . '/yiisoft/yii2-imagine/Imagine/resources',
        ),
      ),
    

    所以我的extensions.php 看起来像

    <?php
    
    $vendorDir = dirname(__DIR__);
    
    return array (
      'yiisoft/yii2-swiftmailer' => 
      array (
        'name' => 'yiisoft/yii2-swiftmailer',
        'version' => '2.0.6.0',
        'alias' => 
        array (
          '@yii/swiftmailer' => $vendorDir . '/yiisoft/yii2-swiftmailer',
        ),
      ),
      'yiisoft/yii2-bootstrap' => 
      array (
        'name' => 'yiisoft/yii2-bootstrap',
        'version' => '2.0.6.0',
        'alias' => 
        array (
          '@yii/bootstrap' => $vendorDir . '/yiisoft/yii2-bootstrap',
        ),
      ),
      'yiisoft/yii2-debug' => 
      array (
        'name' => 'yiisoft/yii2-debug',
        'version' => '2.0.6.0',
        'alias' => 
        array (
          '@yii/debug' => $vendorDir . '/yiisoft/yii2-debug',
        ),
      ),
      'yiisoft/yii2-gii' => 
      array (
        'name' => 'yiisoft/yii2-gii',
        'version' => '2.0.5.0',
        'alias' => 
        array (
          '@yii/gii' => $vendorDir . '/yiisoft/yii2-gii',
        ),
      ),
      'yiisoft/yii2-faker' => 
      array (
        'name' => 'yiisoft/yii2-faker',
        'version' => '2.0.3.0',
        'alias' => 
        array (
          '@yii/faker' => $vendorDir . '/yiisoft/yii2-faker',
        ),
      ),
      'yiisoft/yii2-imagine' => 
      array (
        'name' => 'yiisoft/yii2-imagine',
        'version' => '~2.0.0',
        'alias' => 
        array (
          '@yii/imagine' => $vendorDir . '/yiisoft/yii2-imagine',
          '@Imagine/Image' => $vendorDir . '/yiisoft/yii2-imagine/Imagine/Image',
          '@Imagine/Gd' => $vendorDir . '/yiisoft/yii2-imagine/Imagine/Gd',
          '@Imagine/Draw' => $vendorDir . '/yiisoft/yii2-imagine/Imagine/Draw',
          '@Imagine/Effects' => $vendorDir . '/yiisoft/yii2-imagine/Imagine/Effects',
          '@Imagine/Exception' => $vendorDir . '/yiisoft/yii2-imagine/Imagine/Exception',
          '@Imagine/Filter' => $vendorDir . '/yiisoft/yii2-imagine/Imagine/Filter',
          '@Imagine/Gmagick' => $vendorDir . '/yiisoft/yii2-imagine/Imagine/Gmagick',
          '@Imagine/Imagick' => $vendorDir . '/yiisoft/yii2-imagine/Imagine/Imagick',
          '@Imagine/resources' => $vendorDir . '/yiisoft/yii2-imagine/Imagine/resources',
        ),
      ),
    );
    

    【讨论】:

      猜你喜欢
      • 2011-02-06
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-02
      • 2014-11-06
      相关资源
      最近更新 更多