mmedojevicbg/yii2-smart-image-field

Image upload field for Yii2 forms

dev-master 2016-02-12 14:54 UTC

This package is auto-updated.

Last update: 2024-04-10 20:01:22 UTC


README

This is replacement of classic file upload HTML form element. Advantages of this field are:

  • You don't have to handle file upload yourself. It is encapsulated in ImageUploadAction.
  • ImageUploadAction resolves upload name clashes by default.
  • If existing model is edited, image field is pre-populated.
  • Uploaded image won't be lost if form has validation errors after submit.
  • There is a delete image link besides upload button.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist mmedojevicbg/yii2-smart-image-field "dev-master"

or add

"mmedojevicbg/yii2-smart-image-field": "dev-master"

to the require section of your composer.json file.

Usage

  1. Attach ImageUploadAction to designated controller
class SiteController extends BaseController
{
    function actions()
    {
        return [
            'upload' => [
                'class' => 'mmedojevicbg\SmartImageField\ImageUploadAction',
                'uploadsPath' => \Yii::getAlias('@webroot') . '/uploads',
                'uploadsUrl' => '/uploads'
            ]
        ];
    }
}
  1. Utilize SmartImageField inside ActiveForm
echo SmartImageField::widget(['model' => $model,
                              'attribute' => 'profile_image',
                              'uploadsHandler' => '/site/upload']);