Add image cropping functionality to your Yii application using the Jcrop jQuery cropping tool.
This extension adds a simple way to optionally use start, cancel, and apply buttons. Also provides a PHP class to handle a crop request sent via AJAX.
This extension is not yet finished, while it is completely usable as-is we will be improving it as our needs dictate. Your contributions are welcome.
Yii 1.1.8 or above (due to Jquery version), GD support.
Extract to your extensions folder.
Allows the user to select an area of an existing image using the Jcrop widget. The extension also creates the hidden text fields to record the cropping coordinates.
Optionally, you may specify to also create buttons for controlling the widget (start, cancel & apply). If you do not use these buttons you will also need to create the javascript for sending out the AJAX request.
$this->widget('ext.jcrop.EJcrop', array( // // Image URL 'url' => $this->createAbsoluteUrl('/path/to/full/image.jpg'), // // ALT text for the image 'alt' => 'Crop This Image', // // options for the IMG element 'htmlOptions' => array('id' => 'imageId'), // // Jcrop options (see Jcrop documentation) 'options' => array( 'minSize' => array(50, 50), 'aspectRatio' => 1, 'onRelease' => "js:function() {ejcrop_cancelCrop(this);}", ), // if this array is empty, buttons will not be added 'buttons' => array( 'start' => array( 'label' => Yii::t('promoter', 'Adjust thumbnail cropping'), 'htmlOptions' => array( 'class' => 'myClass', 'style' => 'color:red;' // make sure style ends with « ; » ) ), 'crop' => array( 'label' => Yii::t('promoter', 'Apply cropping'), ), 'cancel' => array( 'label' => Yii::t('promoter', 'Cancel cropping') ) ), // URL to send request to (unused if no buttons) 'ajaxUrl' => 'controller/ajaxcrop', // // Additional parameters to send to the AJAX call (unused if no buttons) 'ajaxParams' => array('someParam' => 'someValue'), ));
You will need to create a controller action that handles the AJAX request.
When the cropper class is initialized, set the directory for saving the thumbnails. All subsequent calls will use the same path (for example in a loop).
Generally you will want to make the thumbnails directory a sub-directory of your full size directory. Thumbnails will have the same name as the parent image, so do not use the same directory for both !!
Yii::import('ext.jcrop.EJCropper'); $jcropper = new EJCropper(); $jcropper->thumbPath = '/my/images/thumbs'; // some settings ... $jcropper->jpeg_quality = 95; $jcropper->png_compression = 8; // get the image cropping coordinates (or implement your own method) $coords = $jcropper->getCoordsFromPost('idOrAttributeName'); // returns the path of the cropped image, source must be an absolute path. $thumbnail = $jcropper->crop('/my/images/imageToCrop.jpg', $coords);
Total 18 comments
I am showing the EJCrop widget inside a CJuiDialog. It works fine the first time, but the second time the dialog is shown the javascript (ejcrop_initWithButtons) of EJCrop is not initialised properly and I cannot crop. Any idea how to handle this issue? I've tried Yii::app()->clientScript->scriptMap['ejcrop.js'] = false; but without success. Any idea?
it does not crop and image get blured, does any idea what had happened all is working, and all is same as stated above.
Can i make the drag handler circular? since i need a circular croping.please let me know if it is possible.
-Sirin
It wasnt worked with normal ajaxUrl='controller/action' But when did with
It worked.
-Sirin
This code
creates an url like http://localhost/jcrop/index.php?r=images/DSC04753.JPG, and it does not show my image. Obviously, this is not the my image URL. How do I fix this?
Yes, I understand in some cases you may want to do things not covered by the extension. Flexibility vs ease of use is always a give and take ...
There is the jii-jcrop extension which is supposed to be more minimal.
However, if you want to make some modifications to JS code that would make it more flexible, this would be greatly appreciated and I will be sure to include it in the release.
Why putting online an extension made for a specific use ?
I went trought your JS code. For what you're using it for, it must be great. If we're not using it as you do, we're doomed ;-)
You should try to keep your project specific code in your own project, not in the extension...
Example : in ejcrop.js, the ejcrop_initWithButtons function don't give out any flexibility on the ajax request result... and it should.
Pretty sure this is not the only place where the extension fail at giving flexibility to developpers...
Good work. I may only have a too specific use to do with jcrop ;-) lol
I need define my own JS function why must be called after jCrop do "crop".
Thanks, the instructions have been updated with your corrections. I don't understand your question though, can you be a little more specific ?
I cath JS error when use example code and press on button "cancel":
Maybee need replace
to
and in controller need use absolute path
How I can set my action for buttons?
Bug has been fixed in latest release, thanks !
In my download the EJcrop file registers following script:
But the JQuery object in the .js files is Jcrop not jcrop. so I changed the capitalization to Jcrop in the above line to get this to work
Thanks, forgot to add that bit. When I updated to Yii 1.1.8, the extension also had to be updated.
Note that you need to run version 1.1.8 to get this work. I was using Yii framework 1.1.7 and it doesn't work on that version. Probably because in version 1.1.8 the jQuery library is updated.
I was using a custom CSS for my application, so did not include it. But you're right that for a general use extension it's much better to have the default CSS, so this has been added in the latest version.
I noticed that jCrop css is not included into archive, is that on purpose?
Yes, sorry about that.
I realized there was a major problem with the extension right before I was about to release. This has been fixed now and I will be making a fully working package available for download.
kinda hard to try it out :)
Leave a comment
Please login to leave your comment.