ignatenkovnikita/yii2-imagemanager

Yii2 图像管理器

v1.2 2019-04-19 21:02 UTC

This package is auto-updated.

Last update: 2024-09-05 22:37:31 UTC


README

Yii2 图像管理器

Latest Stable Version Total Downloads Latest Unstable Version License

安装

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

运行以下命令之一

composer require --prefer-dist ignatenkovnikita/yii2-imagemanager "*"

或添加

"ignatenkovnikita/yii2-imagemanager": "*"

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

应用迁移

php yii migrate --migrationPath=vendor/ignatenkovnikita/yii2-imagemanager/migrations/

使用方法

在产品预览和附件中的示例使用

在模型 Product 中添加

public $attachments;
public $attachment;

const NAME_ATTACHMENTS = 'product.attachments';
const NAME_ATTACHMENT = 'product.attachment';
    
public function rules()
{
    return ArrayHelper::merge(parent::rules(), [
        [['attachments', 'attachment'], 'safe'],
    ]);
}    


public function behaviors()
{
    return ArrayHelper::merge(parent::behaviors(), [
        [
            'class' => \ignatenkovnikita\imagemanager\behaviors\UploadBehavior::className(),
            'attribute' => 'attachments',
            'multiple' => true,
            'tag' => self::NAME_ATTACHMENTS,
            'pathAttribute' => 'path',
            'uploadRelation' => 'productAttachments',
            'baseUrlAttribute' => 'base_url',
            'orderAttribute' => 'order',
            'typeAttribute' => 'type',
            'sizeAttribute' => 'size',
            'nameAttribute' => 'name',
        ],
        [
            'class' => \ignatenkovnikita\imagemanager\behaviors\UploadBehavior::className(),
            'attribute' => 'attachment',
            'multiple' => false,
            'tag' => self::NAME_ATTACHMENT,
            'uploadRelation' => 'productAttachment',
            'pathAttribute' => 'path',
            'baseUrlAttribute' => 'base_url',
            'orderAttribute' => 'order',
            'typeAttribute' => 'type',
            'sizeAttribute' => 'size',
            'nameAttribute' => 'name',
        ],
    ]);
}


/**
 * @return \yii\db\ActiveQuery
 * @throws \Exception
 */
public function getProductAttachments()
{
    return $this->hasMany(ImageManager::class, ['owner_id' => 'id'])->andWhere(['tag' => self::NAME_ATTACHMENTS]);
}

/**
 * @return \yii\db\ActiveQuery
 * @throws \Exception
 */
public function getProductAttachment()
{
    return $this->hasOne(ImageManager::class, ['owner_id' => 'id'])->andWhere(['tag' => self::NAME_ATTACHMENT]);
}

在视图中添加小部件

<?php echo $form->field($model, 'attachment')->widget(
    Upload::className(),
    [
        'url' => ['/file-storage/upload'],
        'maxFileSize' => 5000000, // 5 MiB
    ]);
?>

<?php echo $form->field($model, 'attachments')->widget(
    Upload::className(),
    [
        'url' => ['/file-storage/upload'],
        'sortable' => true,
        'maxFileSize' => 10000000, // 10 MiB
        'maxNumberOfFiles' => 10
    ]);
?>