deadly299/yii2-gallery

模块允许通过小部件上传图片

安装: 71

依赖者: 0

建议者: 0

安全: 0

星级: 1

观察者: 2

分支: 11

类型:yii2-extension

dev-master 2021-01-05 05:48 UTC

This package is auto-updated.

Last update: 2024-09-05 14:01:25 UTC


README

此模块被创建,以便在管理后台快速上传图片,添加标题,描述,替代文本,以及设置位置(值越大,图片在列表中的位置越靠前)。

安装

执行以下命令

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

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

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

然后执行

php composer update

迁移

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

连接和设置

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

    'modules' => [
        'gallery' => [
            'class' => 'deadly299\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' => 'deadly299\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']]); ?> 为表单。

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

<?= \deadly299\gallery\widgets\FrontendGallery::widget([
    'model' => $model,
    'countInput' => 2,
    'classBlockRight' => 'col-sm-12',
    'previewWidth' => '200px',
    'previewHeight' => '150px',
    'informationBlock' => false,
    'otherInputsSettings' => [
        'you-photo' => ['label' => 'Ваше фото', 'required' => true],
        'our-work' => ['label' => 'Наша работа', 'required' => true],
    ],
]) ?>