zxbodya/yii2-gallery-manager

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

安装次数: 50,068

依赖项: 2

建议者: 0

安全: 0

星标: 93

关注者: 14

分支: 61

公开问题: 41

类型:yii2-extension

dev-master 2020-12-07 22:26 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:29:31 UTC


README

Yii2 端的 https://github.com/zxbodya/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 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 添加到您的模型中,并对其进行配置,为上传的文件创建文件夹。

use zxbodya\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 zxbodya\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 zxbodya\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 属性