Giter Site home page Giter Site logo

ilhomjonkurbanov / yii2-upload-behavior Goto Github PK

View Code? Open in Web Editor NEW

This project forked from akiraz2/yii2-upload-behavior

0.0 0.0 0.0 43 KB

Yii2 file/image upload behavior for ActiveRecord

Home Page: http://yiidreamteam.com/yii2/upload-behavior

License: MIT License

PHP 100.00%

yii2-upload-behavior's Introduction

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/[[pk]].[[extension]]',
            'fileUrl' => '/uploads/[[pk]].[[extension]]',
        ],
    ];
}

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 [[foobar]] will be replaced with appropriate placeholder value.

Possible path/url placeholders:

  • [[model]] - model class name
  • [[pk]] - value of the primary key
  • [[id]] - the same as [[pk]]
  • [[attribute_name]] - attribute value, for example [[attribute_ownerId]]
  • [[id_path]] - id subdirectories structure (if model primary key is 12345, placeholder value will be 1/2/3/4/5/0/0/0/0/0
  • [[basename]] - original filename with extension
  • [[filename]] - original filename without extension
  • [[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/[[pk]].[[extension]]',
             'fileUrl' => '/images/[[pk]].[[extension]]',
             'thumbPath' => '@webroot/images/[[profile]]_[[pk]].[[extension]]',
             'thumbUrl' => '/images/[[profile]]_[[pk]].[[extension]]',
        ],
    ];
}

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 [[foobar]] will be replaced with appropriate placeholder value.

Possible path/url placeholders:

  • [[model]] - model class name
  • [[pk]] - value of the primary key
  • [[id]] - the same as [[pk]]
  • [[attribute_name]] - attribute value, for example [[attribute_ownerId]]
  • [[id_path]] - id subdirectories structure (if model primary key is 12345, placeholder value will be 1/2/3/4/5/0/0/0/0/0
  • [[basename]] - original filename with extension
  • [[filename]] - original filename without extension
  • [[extension]] - original extension
  • [[profile]] - 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

Links

yii2-upload-behavior's People

Contributors

metalagman avatar biosin avatar rsol avatar elisdn avatar akiraz2 avatar ekzobrain avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.