在Yii2中创建Thumb会出错

时间:2015-07-04 15:29:27

标签: php yii2

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

  

PHP致命错误 - yii \ base \ ErrorException   Class' 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;
    }
?>

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

你可以试试这个:

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"

然后你可以使用它。

答案 2 :(得分:-2)

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

只需按照以下两个步骤操作:

  1. 打开extensions.php

  2. 在文件底部添加

  3.   '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',
        ),
      ),
    );