stkevich/yii2-cropper

YII2 Image Cropper

Installs: 206

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 1

Open Issues: 0

Type:yii2-extension

dev-master 2018-10-24 16:17 UTC

This package is auto-updated.

Last update: 2019-08-24 19:33:49 UTC


README

YII2 Image Cropper. Extension based on imgAreaSelect jQuery plugin. More details about it on author site http://odyniec.net/projects/imgareaselect/usage.html

Installation

The preferred way to install this extension is through composer.

Run

composer require --prefer-dist stkevich/yii2-cropper "*"

or add

"stkevich/yii2-cropper": "*"

to the require section of your composer.json file.

Usage front

Once the extension is installed, simply use it in your code by:

use \stkevich\cropper\Cropper;
...
$form->field($modelName, 'fieldName')->widget(Cropper::className(), []);

You can add some options to widget:

$form->field($modelName, 'fieldName')->widget(Cropper::className(), 
    [
        'aspectRatio' => '1', //A string of the form "width:height" which represents the aspect ratio to maintain. Default = 1.
        'maxHeight' => '200', //Maximum selection height (in pixels). Default = 9999.
        'maxWidth' => '200', //Maximum selection width (in pixels). Default = 9999.
        'minHeight' => '200', //Minimum selection height (in pixels). Default = 0.
        'minWidth' => '200', //Minimum selection width (in pixels). Default = 0.
        'resizable' => 'true', //If set to true, the plugin is completely removed. Default = true.
    ]
);

Usage in back

public function behaviors()
    {
        return [
            'imageCropper' => [
                'class' => ImageBehavior::class,
                'attributeName' => 'image', // Attribute to work in behavior
                'removeOldImage' => true, // Remove old image when record is update
                'path' => '/upload/product/', // Path to save final image
                'savingImages' => [
                    'thumbnail' => [
                        'fieldName' => 'image',
                        'method' => function ($img) {
                            /** @var ImageInterface $img */
                            $cropInfo = CropFactory::getInfo('image');
                            return $img
                                ->copy()
                                ->crop(
                                    new Point($cropInfo->getX(), $cropInfo->getY()),
                                    new Box($cropInfo->getWidth(), $cropInfo->getHeight())
                                )->resize(new Box(235, 300));
                        }
                    ],
                    'big' => [
                        'fieldName' => 'imageLarge',
                        'method' => function ($img) {
                            /** @var ImageInterface $img */
                            $dstSize = $img->getSize();
                            $maxWidth = 1200;
                            if ($dstSize->getWidth() > $maxWidth) {
                                $dstSize = $dstSize->widen($maxWidth);
                            }
                            return $img
                                ->copy()
                                ->resize($dstSize);
                        }
                    ]
                ],
                'imageFileName' => function($extension, $version) {
                    return $version . '_' . substr(md5(time()), 0, 10) . '.' . $extension;
                }
            ],
    }