apolle/yii2-gallery-manager

yii 的扩展,允许管理图片画廊

安装: 30

依赖: 0

建议者: 0

安全性: 0

星标: 1

关注者: 2

分支: 0

开放性问题: 0

类型:yii2-extension

dev-master 2017-08-15 00:13 UTC

This package is not auto-updated.

Last update: 2024-09-21 15:21:27 UTC


README

Yii2 端的 https://github.com/iswebjscom/yii2-gallery-manager.git

(前端部分基本没有变化,但后端几乎全部重写)

画廊管理器截图(yii 1.x 版本,新的一个具有 bootstrap 3 风格)

GalleryManager images list

更多截图:拖放上传编辑图片信息上传进度

特性

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

依赖项

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

安装

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

运行以下命令:

composer require apolle/yii2-gallery-manager

或将

apolle/yii2-gallery-manager

添加到您的 composer.json 文件的 require 部分。

用法

添加迁移以创建用于图片的表

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('@webroot') . '/images/product/gallery',
             'url' => Yii::getAlias('@web') . '/images/product/gallery',
             'versions' => [
                 'small' => function ($img) {
                     /** @var \Imagine\Image\ImageInterface $img */
                     return $img
                         ->copy()
                         ->thumbnail(new \Imagine\Image\Box(200, 200));
                 },
                 'medium' => function ($img) {
                     /** @var Imagine\Image\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 属性