Skip to content

germanoricardi/yii2-imagecrop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Latest Stable Version Total Downloads Latest Unstable Version License

yii2-imagecrop

Crop images with this class is very simple!

Resources

  • Centralized autocrop;
  • Customized cutting area with json;
  • Watermark;

Installation

The preferred way to install this extension is through composer.

Install

Either run

$ php composer.phar require germanoricardi/yii2-imagecrop "*"

or add

"germanoricardi/yii2-imagecrop": "*"

to the require section of your composer.json file.

**Part of this class code was implemented by a document made available by fengyuanchen.

How to use

Add the following code in a controller, the image that will be used for cutting should already be on your server, that is, the code should be run after the file upload. The original image is always preserved.

use germanoricardi\imagecrop\ImageCrop;

public function actionCrop(){
    $imageCrop = new ImageCrop();
    $imageCrop->imageSourcePath	= '@webroot/medias/';
    $imageCrop->imagesSizes		= [
        ['path' => '@webroot/medias/thumbs/', 'width' => '200', 'height' => '200'],
        ['path' => '@webroot/medias/large/', 'width' => '800', 'height' => '600', 'watermark' => '@webroot/watermark.png']
    ];
    $imageCrop->imageFileName	= 'image-to-crop.jpg';
    $imageCrop->init();
}

AJAX

If you are working with a jQuery plugin for crop image parameters can be passed to the class via AJAX as follows:

    $imageCrop = new ImageCrop();
    $imageCrop->imageSourcePath	= '@webroot/medias/';
    $imageCrop->imagesSizes		= [
        ['path' => '@webroot/medias/thumbs/', 'width' => '200', 'height' => '200'],
        ['path' => '@webroot/medias/large/', 'width' => '800', 'height' => '600', 'watermark' => '@webroot/watermark.png']
    ];
    $imageCrop->imageFileName	= 'image-to-crop.jpg';
    
    // AJAX
    $imageCrop->data            = '{"x":220,"y":26,"width":1168,"height":1168,"rotate":0,"scaleX":1,"scaleY":1}';
    
    $imageCrop->init();

License

License