Tigrov / yii2-photo-widget

Upload photo widget for Yii2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

yii2-photo-widget

Upload photo widget for Yii2.

drawing

Latest Stable Version

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist tigrov/yii2-photo-widget

or add

"tigrov/yii2-photo-widget": "~1.0"

to the require section of your composer.json file.

Usage

It is better to use the extension with yii2-upload-behavior

Once the extension is installed, you can use it as follow:

Create a model with a photo attribute

class Model extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'upload' => [
                 'class' => '\tigrov\uploadBehavior\UploadBehavior',
                 'path' => '@runtime/upload',
                 'attributes' => ['photo'],
                 // 'saveCallback' => ['\tigrov\photoWidget\PhotoWidgetHelper', 'crop'],
                 // or if you need to crop and resize
                 'saveCallback' => function ($model, $attribute, $file, $filename) {
                     $width = 200;
                     $height = 200;
                     \tigrov\photoWidget\PhotoWidgetHelper::crop($model, $attribute, $file, $filename, $width, $height);
                 }
            ],
        ];
    }
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['photo'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png,jpg,jpeg'],
        ];
    }
}

Create an action in a controller

class FormController extends \yii\web\Controller
{
    public function actionUpload()
    {
        $model = new Model();
        if ($model->load(\Yii::$app->request->post()) && $model->save()) {
            \Yii::$app->session->setFlash('success', 'Model is saved.');
            return $this->refresh();
        }

        return $this->render('form', [
            'model' => $model,
        ]);
    }
}

Create a form with the file attribute

<?php $form = ActiveForm::begin(); ?>
    <?= $form->field($model, 'photo')->widget('\tigrov\photoWidget\PhotoWidget') ?>
    <?= Html::submitButton('Submit') ?>
<?php $form::end(); ?>

After submitting the photo it will be saved to specified path.

License

MIT

About

Upload photo widget for Yii2

License:MIT License


Languages

Language:PHP 80.5%Language:JavaScript 17.7%Language:CSS 1.8%