bupy7/yii2-gallery-manager

此包已被废弃,不再维护。未建议替代包。

这是分支 https://github.com/zxbodya/yii2-gallery-manager。扩展的改进和升级版本。

安装: 20

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 61

开放问题: 0

类型:yii2-extension

dev-master 2015-04-15 17:36 UTC

This package is not auto-updated.

Last update: 2020-01-24 15:47:00 UTC


README

Yii2 端的 https://github.com/zxbodya/yii-gallery-manager

(前端部分基本未变,但后端几乎完全重写)

警告:预览版本

功能

  1. AJAX 图像上传
  2. 可选为每张图像添加名称和描述
  3. 在画廊中排列图像的可能性
  4. 为每张图像生成不同配置的几个版本的能力
  5. 拖放

依赖

  1. Yii2
  2. Twitter bootstrap 资产(版本 3)
  3. Imagine 库
  4. JQuery UI(包含在 Yii 中)

安装

安装此扩展的首选方式是通过 composer

运行以下命令:

php composer.phar require --prefer-dist zxbodya/yii2-gallery-manager "*@dev"

或者将以下内容添加到您的 composer.json 文件的 require 部分:

"zxbodya/yii2-gallery-manager": "*@dev"

用法

将迁移添加到创建图像表的表

class m150318_154933_gallery_ext
    extends zxbodya\yii2\galleryManager\migrations\m140930_003227_gallery_manager
{

}

或者更好的是 - 将迁移复制到您的应用程序中。

将 GalleryBehavior 添加到您的模型中,并进行配置,创建上传文件的文件夹。

public function behaviors()
{
    return [
         'galleryBehavior' => [
             'class' => GalleryBehavior::className(),
             'type' => 'product',
             'extension' => 'jpg',
             'directory' => Yii::getAlias('@contentRoot') . '/images/product/gallery',
             'url' => Yii::getAlias('@web') . '/images/product/gallery',
             'versions' => [
                 'small' => function ($img) {
                     /** @var ImageInterface $img */
                     return $img
                         ->copy()
                         ->thumbnail(new Box(200, 200));
                 },
                 'medium' => function ($img) {
                     /** @var ImageInterface $img */
                     $dstSize = $img->getSize();
                     $maxWidth = 800;
                     if ($dstSize->getWidth() > $maxWidth) {
                         $dstSize = $dstSize->widen($maxWidth);
                     }
                     return $img
                         ->copy()
                         ->resize($dstSize);
                 },
             ]
         ]
    ];
}

在您的应用程序的控制器中添加 GalleryManagerAction。在这一步,您还可以为此操作添加一些安全检查。

public function actions()
{
    return [
       'galleryApi' => [
           'class' => GalleryManagerAction::className(),
           // mappings between type names and model classes (should be the same as in behaviour)
           'types => [
               'product' => Product::className()
           ]
       ],
    ];
}

在您的应用程序的某个地方添加 ImageAttachmentWidget,例如在编辑表单中。

if ($model->isNewRecord) {
    echo 'Can not upload images for new record';
} else {
    echo GalleryManager::widget(
        [
            'model' => $model,
            'behaviorName' => 'galleryBehavior',
            'apiRoute' => 'product/galleryApi'
        ]
    );
}

完成!

现在,您可以使用以下方式使用画廊中的上传图像:

foreach($model->getBehavior('galleryBehavior')->getImages() as $image) {
    echo Html::img($image->getUrl('medium'));
}

选项

使用非默认的画廊图像表名(默认为 {{%gallery_image}}

  1. 添加创建所需表的迁移
  2. 在行为配置中更改 tableName 属性