dvizh/yii2-gallery

模块允许使用小部件上传图片

安装次数: 4,115

依赖者: 5

建议者: 0

安全: 0

星标: 11

关注者: 2

分支: 11

公开问题: 5

类型:yii2-extension

dev-master 2018-02-05 13:08 UTC

This package is not auto-updated.

Last update: 2024-09-20 22:01:33 UTC


README

此模块旨在提供快速在管理后台加载图片的功能,包括添加标题、描述、替代文本,以及设置图片位置(值越大,图片在列表中的位置越靠前)。

安装

执行以下命令

php composer require dvizh/yii2-gallery "@dev"

或者将以下内容添加到 composer.json 文件中

"dvizh/yii2-gallery": "@dev",

然后执行

php composer update

迁移

php yii migrate/up --migrationPath=@vendor/dvizh/yii2-gallery/src/migrations

连接和配置

在应用程序配置文件中添加 gallery 模块

    'modules' => [
        'gallery' => [
            'class' => 'dvizh\gallery\Module',
            'imagesStorePath' => dirname(dirname(__DIR__)).'/frontend/web/images/store', //path to origin images
            'imagesCachePath' => dirname(dirname(__DIR__)).'/frontend/web/images/cache', //path to resized copies
            'graphicsLibrary' => 'GD',
            'placeHolderPath' => '@webroot/images/placeHolder.png',
            'adminRoles' => ['administrator', 'admin', 'superadmin'],
        ],
        //...
    ]

为需要附加上传图片的模型添加行为

    function behaviors()
    {
        return [
            'images' => [
                'class' => 'dvizh\gallery\behaviors\AttachImages',
                'mode' => 'gallery',
                'quality' => 60,
                'galleryId' => 'picture'
            ],
        ];
    }

*mode - 加载类型。gallery - 批量加载,single - 单个字段,如需压缩,请设置 quality (0 - 100),其中 0 - 最大压缩,100 - 最小压缩。galleryId - 画廊标识符,如果存在同名类冲突,请使用。

使用

也可以直接使用

$images = $model->getImages();
foreach($images as $img) {
    //retun url to full image
    echo $img->getUrl();

    //return url to proportionally resized image by width
    echo $img->getUrl('300x');

    //return url to proportionally resized image by height
    echo $img->getUrl('x300');

    //return url to resized and cropped (center) image by width and height
    echo $img->getUrl('200x300');

    //return alt text to image
    $img->alt

    //return title to image
    $img->title
    
    //return description image
    $img->description
}

小部件

图片通过小部件上传。在 CRUD 模型表单的 _form.php 中添加小部件。小部件需要以下参数:model => 模型对象,绑定图片,默认 null;previewSize => 上传图片预览尺寸,默认 '140x140';label => 小部件标签,默认 '图像';fileInputPluginLoading => 是否显示加载进度条,默认 true;fileInputPluginOptions => 小部件属性数组 kartik/file/fileInput,默认 [];

不要忘记 php<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?> 用于表单。

<?=\dvizh\gallery\widgets\Gallery::widget(
    [
        'model' => $model,
        'previewSize' => '50x50',
        'fileInputPluginLoading' => true,
        'fileInputPluginOptions' => []
    ]
); ?>