yii2-upload-behavior

Yii2 file/image upload behavior for ActiveRecord

  • 所有者: yii-dream-team/yii2-upload-behavior
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

Yii2 file/image upload behavior for ActiveRecord

This package is the set of two similar behaviors. The first one allows you to keep the uploaded file as-is.
And the second one allows you to generate set of thumbnails for the uploaded image. Behaviors could be attached
multiple times for different attributes.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yii-dream-team/yii2-upload-behavior "*"

or add

"yii-dream-team/yii2-upload-behavior": "*"

to the require section of your composer.json.

FileUploadBehavior

This behavior allow you to add file uploading logic with ActiveRecord behavior.

Usage

Attach the behavior to your model class:

public function behaviors()
{
    return [
        [
            'class' => '\yiidreamteam\upload\FileUploadBehavior',
            'attribute' => 'fileUpload',
            'filePath' => '@webroot/uploads/.',
            'fileUrl' => '/uploads/.',
        ],
    ];
}

You can use Yii path aliases
like @app, @webroot, @web in your path template configuration.

You can also use additional placeholders in path templates.
Placeholder `` will be replaced with appropriate placeholder value.

Possible path/url placeholders:

  • `` - model class name
  • `` - value of the primary key
  • - the same as
  • - attribute value, for example
  • `` - id subdirectories structure (if model primary key is 12345, placeholder value will be 1/2/3/4/5/0/0/0/0/0
  • `` - original filename with extension
  • `` - original filename without extension
  • `` - original extension

Add validation rule:

public function rules()
{
    return [
        ['fileUpload', 'file'],   
    ];
}

Setup proper form enctype:

$form = \yii\bootstrap\ActiveForm::begin([
    'enableClientValidation' => false,
    'options' => [
        'enctype' => 'multipart/form-data',
    ],
]);

File should be uploading fine.

You can get uploaded file url using model call:

echo $model->getUploadedFileUrl('fileUpload');

ImageUploadBehavior

Image upload behavior extends file upload behavior with image thumbnails generation.
You can configure set of different thumbnail profiles to generate.

Usage

Attach the behavior to your model class:

public function behaviors()
{
    return [
        [
             'class' => '\yiidreamteam\upload\ImageUploadBehavior',
             'attribute' => 'imageUpload',
             'thumbs' => [
                 'thumb' => ['width' => 400, 'height' => 300],
             ],
             'filePath' => '@webroot/images/.',
             'fileUrl' => '/images/.',
             'thumbPath' => '@webroot/images/_.',
             'thumbUrl' => '/images/_.',
        ],
    ];
}

You can use Yii path aliases
like @app, @webroot, @web in your path template configuration.

You can also use additional placeholders in path templates.
Placeholder `` will be replaced with appropriate placeholder value.

Possible path/url placeholders:

  • `` - model class name
  • `` - value of the primary key
  • - the same as
  • - attribute value, for example
  • `` - id subdirectories structure (if model primary key is 12345, placeholder value will be 1/2/3/4/5/0/0/0/0/0
  • `` - original filename with extension
  • `` - original filename without extension
  • `` - original extension
  • `` - thumbnail profile name, use it in thumbnail path/url

Add validation rule:

public function rules()
{
    return [
        ['imageUpload', 'file', 'extensions' => 'jpeg, gif, png'],   
    ];
}

Setup proper form enctype:

$form = \yii\bootstrap\ActiveForm::begin([
    'enableClientValidation' => false,
    'options' => [
        'enctype' => 'multipart/form-data',
    ],
]);

File should be uploading fine.

You can get uploaded image url using model call:

echo $model->getImageFileUrl('imageUpload');

You can specify default image for models without uploaded image:

echo $model->getImageFileUrl('imageUpload', '/images/empty.jpg');

You can also get generated thumbnail image url:

echo $model->getThumbFileUrl('imageUpload', 'thumb');

You can specify default thumbnail image for models without uploaded image:

echo $model->getThumbFileUrl('imageUpload', 'thumb', '/images/thumb_empty.jpg');

Licence

MIT

主要指標

概覽
名稱與所有者yii-dream-team/yii2-upload-behavior
主編程語言PHP
編程語言PHP (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2014-08-01 09:17:46
推送於2022-09-30 13:08:54
最后一次提交2017-10-26 22:44:40
發布數7
最新版本名稱1.2.3 (發布於 )
第一版名稱1.0.0 (發布於 )
用户参与
星數77
關注者數13
派生數45
提交數55
已啟用問題?
問題數30
打開的問題數15
拉請求數3
打開的拉請求數3
關閉的拉請求數2
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?