coolanole / yii2-gallery-manager

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

安装数: 1,866

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 61

类型:yii2-extension

1.3.0 2019-06-06 13:50 UTC

This package is auto-updated.

Last update: 2024-09-07 01:18:05 UTC


README

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

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

画廊管理器截图(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

运行以下命令之一:

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

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

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

使用

准备

添加迁移以创建图像表

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

}

或者更好 - 将迁移复制到你的应用程序中(但请确保 删除命名空间 - 它应该在全局命名空间中)

添加上传和存储图像的配置

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

use coolanole\yii2\galleryManager\GalleryBehavior;

class Product extends \yii\db\ActiveRecord 
{
...
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);
                 },
             ]
         ]
    ];
}

有关图像转换的说明,请参阅 imagine 文档

在控制器中的某个位置添加 GalleryManagerAction。你也可以在这一步为该操作添加一些安全检查。

use coolanole\yii2\galleryManager\GalleryManagerAction;

class ProductController extends Controller
{
...
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,例如在编辑器中。

use coolanole\yii2\galleryManager\GalleryManager;

/* @var $this yii\web\View */
/* @var $model Product */
?>
...
<?php
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 属性