karpoff/yii2-crop-image-upload

Yii 2 Crop image upload widget

Installs: 17 940

Dependents: 6

Suggesters: 0

Security: 0

Stars: 18

Watchers: 8

Forks: 22

Open Issues: 8

Language:JavaScript

Type:yii2-extension

0.3.0 2024-01-25 07:08 UTC

This package is not auto-updated.

Last update: 2024-11-14 11:05:13 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

This extension automatically uploads image and make crop.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist karpoff/yii2-crop-image-upload "*"

or add

"karpoff/yii2-crop-image-upload": "*"

to the require section of your composer.json file.

Usage

Upload image and create crop

Attach the behavior in your model:

use karpoff\icrop\CropImageUploadBehavior;

class Document extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['photo', 'file', 'extensions' => 'jpeg, gif, png', 'on' => ['insert', 'update']],
        ];
    }

    /**
     * @inheritdoc
     */
    function behaviors()
    {
        return [
            [
                'class' => CropImageUploadBehavior::className(),
                'attribute' => 'photo',
                'scenarios' => ['insert', 'update'],
                'path' => '@webroot/upload/docs',
                'url' => '@web/upload/docs',
				'ratio' => 1,
				'crop_field' => 'photo_crop',
				'cropped_field' => 'photo_cropped',
            ],
        ];
    }
}

Example view file:

<?php use karpoff\icrop\CropImageUpload; ?>

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
    <?= $form->field($model, 'photo')->widget(CropImageUpload::className()) ?>
    <div class="form-group">
        <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
    </div>
<?php ActiveForm::end(); ?>