Details
| Version | 1.2.2 |
|---|---|
| Last updated | 4th July 2011 |
| Requirements |
|
| Demo | View demo page |
| Links | |
| License | BSD License |
| Changelog |
|
About
The JavaScript image cropper UI allows the user to crop an image using an interface with the same features and styling as found in commercial image editing software, and is is based on the Prototype JavaScript framework and script.aculo.us.
Initially I performed quite a lot of searching for some ready made solutions to meet my requirements, but found none that had the complete feature set that I required or any complete versions based on Prototype.
So after a week and a half of work, I present the JavaScript image cropper UI, built on Prototype & script.aculo.us.
Features
![]()
- Un-obtrusive
- Based on Prototype and script.aculo.us
- Image editing package styling & functionality, the crop area functions and looks like those found in popular image editing software
- Dynamic inclusion of required styles
- Drag to draw areas
- Shift drag to draw/resize areas as squares
- Selection area can be moved
- Selection area can be resized using resize handles
- Allows dimension ratio limited crop areas
- Allows minimum dimension crop areas
- Allows maximum dimensions crop areas, if both min & max set as the same value then we'll get a fixed cropper size on the axes as appropriate and the resize handles will not be displayed as appropriate
- Allows dynamic preview of resultant crop (if minimum width & height are provided), this is implemented as a subclass so can be removed if not required
- Movement of selection area by arrow keys (shift + arrow key will move selection area by 10 pixels)
- All operations stay within bounds of image
- All functionality & display compatible with most popular browsers supported by Prototype, tested in:
- PC: IE 6 & 5.5, Firefox 1.5, Opera 8.5 (see known issues) & 9.0b
- MAC: Camino 1.0, Firefox 1.5, Safari 2.0
Usage
Extract to a directory of your choosing e.g. 'scripts/cropper/' and include the script and the required Prototype & script.aculo.us scripts:
-
<script type="text/javascript" src="scripts/cropper/lib/prototype.js" language="javascript"></script>
-
<script type="text/javascript" src="scripts/cropper/lib/scriptaculous.js?load=effects,builder,dragdrop" language="javascript"></script>
-
<script type="text/javascript" src="scripts/cropper/cropper.js" language="javascript"></script>
Options
- ratioDim obj
- The pixel dimensions to apply as a restrictive ratio, with properties x & y.
- minWidth int
- The minimum width for the select area in pixels.
- minHeight int
- The mimimum height for the select area in pixels.
- maxWidth int
- The maximum width for the select areas in pixels (if both minWidth & maxWidth set to same the width of the cropper will be fixed)
- maxHeight int
- The maximum height for the select areas in pixels (if both minHeight & maxHeight set to same the height of the cropper will be fixed)
- displayOnInit int
- Whether to display the select area on initialisation, only used when providing minimum width & height or ratio.
- onEndCrop func
- The callback function to provide the crop details to on end of a crop.
- captureKeys boolean
- Whether to capture the keys for moving the select area, as these can cause some problems at the moment.
- onloadCoords obj
- A coordinates object with properties x1, y1, x2 & y2; for the coordinates of the select area to display onload
The callback function
The callback function is a function that allows you to capture the crop co-ordinates when the user finished a crop movement, it is passed two arguments:
- coords, obj, coordinates object with properties x1, y1, x2 & y2; for the coordinates of the select area.
- dimensions, obj, dimensions object with properities width & height; for the dimensions of the select area.
An example function which outputs the crop values to form fields:
-
function onEndCrop( coords, dimensions ) {
-
$( 'x1' ).value = coords.x1;
-
$( 'y1' ).value = coords.y1;
-
$( 'x2' ).value = coords.x2;
-
$( 'y2' ).value = coords.y2;
-
$( 'width' ).value = dimensions.width;
-
$( 'height' ).value = dimensions.height;
-
}
Basic interface
This basic example will attach the cropper UI to the test image and return crop results to the provided callback function.
Minimum dimensions
You can apply minimum dimensions to a single axis or both, this example applies minimum dimensions to both axis.
Select area ratio
You can apply a ratio to the selection area, this example applies a 4:3 ratio to the select area.
-
<img src="test.jpg" alt="Test image" id="testImage" width="500" height="333" />
-
-
<script type="text/javascript" language="javascript">
-
Event.observe( window, 'load', function() {
-
new Cropper.Img(
-
'testImage',
-
{
-
ratioDim: {
-
x: 220,
-
y: 165
-
},
-
displayOnInit: true,
-
onEndCrop: onEndCrop
-
}
-
);
-
} );
-
</script>
With crop preview
You can display a dynamically produced preview of the resulting crop by using the ImgWithPreview subclass, a preview can only be displayed when we have a fixed size (set via minWidth & minHeight options). Note that the displayOnInit option is not required as this is the default behaviour when displaying a crop preview.
-
<img src="test.jpg" alt="Test image" id="testImage" width="500" height="333" />
-
<div id="previewWrap"></div>
-
-
<script type="text/javascript" language="javascript">
-
Event.observe( window, 'load', function() {
-
new Cropper.ImgWithPreview(
-
'testImage',
-
{
-
previewWrap: 'previewWrap',
-
minWidth: 120,
-
minHeight: 120,
-
ratioDim: { x: 200, y: 120 },
-
onEndCrop: onEndCrop
-
}
-
);
-
} );
-
</script>
Known Issues
- Safari animated gifs, only one of each will animate, this seems to be a known Safari issue.
- After drawing an area and then clicking to start a new drag in IE 5.5 the rendered height appears as the last height until the user drags, this appears to be the related to another IE error (which has been fixed) where IE does not always redraw the select area properly.
- Lack of CSS opacity support in Opera before version 9 mean we disable those style rules, if Opera 8 support is important you & you want the overlay to work then you can use the Opera rules in the CSS to apply a black PNG with 50% alpha transparency to replicate the effect.
- Styling & borders on image, any CSS styling applied directly to the image itself (floats, borders, padding, margin, etc.) will cause problems with the cropper. The use of a wrapper element to apply these styles to is recommended.
- overflow: auto or overflow: scroll on parent will cause cropper to burst out of parent in IE and Opera when applied (maybe Mac browsers too) I'm not sure why yet.
Next Steps
Feature Requests & Bug Reports
Please check the existing list of feature requests & bugs and the discussion list before posting requests or reporting bugs.
Leave a Tip
If you find this code useful you can leave a donation towards the continued development & support.
Comments
There have been 750 comments so far, join the discussion.
Pages: « 1 … 5 6 7 8 9 10 11 12 13 14 [15] Show All
701. Dave - 20th Sep 2010 - 9:50 am
Usman: Something is not included properly (maybe script.aculo.us or prototype) as the error is telling you that Draggable (defined by script.aculo.us) is not defined.
702. Octavian - 23rd Sep 2010 - 11:04 am
How should I implement this to crop the desired image with PHP? I should take advantage of x1×2 y1 y2 somehow?
Anyway, thank you for this marvelous script. Keep up the good work.
Octavian
703. Dave - 23rd Sep 2010 - 12:34 pm
Octavian: Please see the FAQs (http://code.google.com/p/javascript-image-cropper-ui/wiki/FAQs) for examples using PHP with the JavaScript Image Cropper UI.
704. Octavian - 27th Sep 2010 - 12:38 pm
If I load it into a dynamically created iframe how should I create the object ( [lang]Event.observe (
window,
‘load’,
function() {[/lang]
) it’s not working.
705. Dave - 27th Sep 2010 - 12:42 pm
Octavian, if that code is inside the iframe that you’re loading then I can’t think of any reason why it wouldn’t work.
706. Octavian - 27th Sep 2010 - 1:25 pm
It is not working. It’s very strange because I tested with a non-modified example page that works in a new window and when showed as a dynamically created iframe, the cropper object it’s not created. Should I use the cropper attach function?
707. Image Crop UI With Prototype | Rapid Library - 5th Oct 2010 - 4:56 pm
[...] Prototype JavaScript Image Cropper UI is an unobtrusive script based on Prototype and script.aculo.us. [...]
708. AJAX Style Image Cropper : MS-Joe - 9th Oct 2010 - 2:22 pm
[...] http://www.defusion.org.uk/code/javascript-image-cropper-ui-using-prototype-scriptaculous [...]
709. Java script prototype - 13th Nov 2010 - 11:34 am
[...] DEfusion.org.uk » JavaScript Image Cropper UI, using Prototype 6 Oct 2009. So after a week and a half of work, I present the JavaScript image cropper UI, built on Prototype & script.aculo.us. DEfusion.org.uk » JavaScript Image Cropper UI, using Prototype [...]
710. 5+ Useful JS Framework Plugin for Image Cropping | Blogupstairs - 20th Nov 2010 - 10:05 am
[...] jsCropperUI The JavaScript image cropper UI allows the user to crop an image using an interface with the same features and styling as found in commercial image editing software, and is is based on the Prototype JavaScript framework and script.aculo.us. It is compatible with most popular browsers supported by Prototype : IE 6 & 5.5, Firefox 1.5, Opera 8.5, Camino 1.0, Firefox 1.5, Safari 2.0 [...]
711. Andre - 8th Jan 2011 - 12:14 am
HI,
There is no cropper.php or index file.
How do i implement this?
712. Dave - 8th Jan 2011 - 12:46 am
Andre,
Read the above post or the FAQ for detailed instructions on implementing the cropper.
713. TheIceman - 8th Jan 2011 - 1:38 am
Yes, the FAQ and detailed documentation are very helpful and have helped me from knowing nothing to full implementation.
amazing script.
714. Tushar - 11th Feb 2011 - 3:21 pm
Hi,
I have added below scripts as mentioned:
added below Javascript code
// example with a preview of crop results, must have minimumm dimensions
Event.observe(
window,
‘load’,
function() {
new Cropper.ImgWithPreview(
‘testImage’,
{
minWidth: 200,
minHeight: 120,
ratioDim: { x: 200, y: 120 },
displayOnInit: true,
onEndCrop: onEndCrop,
previewWrap: ‘previewArea’
}
)
}
);
I am using ASP.Net MVC pattern for my application.When I tried to run the page I am getting error as
Draggable is not defined
$(”#videopreview”) is null
Cropper is undefined
Please let me know I am doing anything wrong
715. Dave - 11th Feb 2011 - 4:22 pm
@Tushar:
$(’#videopreview”) isn’t defined anywhere in my code so that’s not an error from my code. The “Draggable is not defined” is telling you that script.aculo.us isn’t loaded properly for the cropper (e.g. the Draggable module isn’t loaded).
-D
716. Christopher Thomas - 11th Feb 2011 - 5:33 pm
I’m confused, the people asking “why is cropper undefined” they are programmers? seriously? I mean, this issue is explained about 5 times in this page, more than that actually, yet these guys turn up, ask the same stupid question.
Is this really the low quality of programmers today who can’t even fix a simple problem properly? seriously….makes me cry almost…
717. TheIceman - 11th Feb 2011 - 10:35 pm
Well thats just it, a lot of programmers today think they are programmers but all they are, are artists. Like artists do, get some paint and put it on the page where it looks good and works ok, wanna be programmers today who call themselves programmers get ready made scripts and paste them on a page to make it look good and hope it works.
when it doesnt work then they are stuffed, they have no idea what to do and ask stupid questions.
718. Christopher Thomas - 13th Feb 2011 - 11:06 am
You are right, but they can’t READ as well as can’t program, that’s the sad part, anyone asking why is “cropper is undefined”, please read the comments above before asking the SAME question again.
At least do that for yourself…....
719. 10 Excellent JQuery Plugin for Image Manipulation | denbagus blog - 16th Feb 2011 - 3:32 pm
[...] image cropping is a common feature in web applications.Prototype JavaScript Image Cropper UI, unobtrusive script based on prototype and script.aculo.us. [...]
720. Gerarard - 22nd Apr 2011 - 1:49 pm
Hello,
I’m using cropper on my website.
Under FireFox and Google Chrome, everything is right.
But under IE 9 , there are problems.
Sometimes it’s works, sometimes not.
This feature is making me crazy.
Someone have solution to resolve my probleme ?
721. JavaScript??????UI (??Prototype ?Scriptaculous) - 1st May 2011 - 7:37 pm
[...] The JavaScript image cropper UI ?????????????????????????????????????Prototype?script.aculo.us???????????????????????????????????????????????????????????????????????????? image cropper UI [...]
722. blasting - 10th May 2011 - 8:04 pm
I was trying the examples(the basic example) in cropper and I noticed something weird
I put multiple images in a div(call them image1 and image2)
I wrote a function that would attach a cropper object on the first image when a button is clicked… so far so good
however whenever the cropper was attached to image1(i.e. the button was clicked), the ordering of the images would change i.e. image 2 comes before image1
Is there any way I can avoid this?
723. Dave - 10th May 2011 - 8:09 pm
Blasting:
The cropper removes the image from the DOM & replaces it with a wrapped image (by appending to the parent of the image). The easiest solution would be wrap each image in its own div.
724. Blasting - 10th May 2011 - 8:18 pm
Oh, I was actually hoping to avoid that..
But i guess thats the easiest way.
Thanks for your help :)
725. ZKJia.Com | ???????????????? - 14th May 2011 - 4:51 pm
[...] JavaScript Image Cropper UI, using Prototype & script.aculo.us allows the user to crop an image using similar features found in commercial image editing software. [...]
726. zhm - 16th May 2011 - 1:19 am
con’t drag on ie9.
727. 8 Most Popular JavaScript Image Cropping Scripts | Hot Scripts Blog - 27th May 2011 - 12:09 pm
[...] JavaScript image cropper UI allows the user to crop an image using an interface with the same features and styling as found in [...]
728. Boris - 10th Jun 2011 - 7:51 pm
First of all – PERFECT SCRIPT. E X A C T L Y what I needed…
That is why when I faced a weird issue I decided to debug…
In case somebody else will come across the same issue.
Everything is fine in Chrome, FF, IE8 (non compatibility view).
But ones switch to compatibility view – everything just stops. I see image, but no manipulation available – click, drag, resize .. nothing.
I see the preview in ‘previewWrap’... though…
After few hours of fighting I realize the following: – creating of div element and assigning CSS class at the same time doesn’t work.
In other words this:
this.handleN = new Element( ‘div’, { ‘class’: cNamePrefix + ‘handle ’ + cNamePrefix + ‘handleN’ } );
doesn’t work.
But this:
this.handleN = new Element( ‘div’ );
this.handleN.addClassName(cNamePrefix + ‘handle ’ + cNamePrefix + ‘handleN’);
works fine.
So I walked through JS file, converted everything to the above syntax, and now everything works just fine.
May be it is my only issue…. (version of prototype or scriptaculous… I do not know)... but may be somebody else experience the same….
729. Emmanuel Parlour - 15th Jun 2011 - 8:57 pm
I know this is a dumb question, but how can you save the cropped image?
730. nep - 4th Jul 2011 - 7:47 am
Cropper was a very very good script but…...
it doesn’t work on internet explorer 9 !
Don’t waste your time this script is not supported ( Last updated 6th October 2009 )
731. Dave - 4th Jul 2011 - 3:11 pm
@nep
The script is supported, I just didn’t have the ability to test in IE9. Today I did and other than requiring a newer version of Prototype/script.aculo.us (not sure which as upgraded both at the same time) it now works 100% in IE9 for me and required no changes to my script.
732. B and C Hosting - 15th Jul 2011 - 2:03 pm
The link to the FAQ is dead. It is not working. How can we implement this with PHP please ?
733. Dave - 15th Jul 2011 - 2:15 pm
@B and C Hosting:
The FAQ’s are working fine for me: http://code.google.com/p/javascript-image-cropper-ui/wiki/FAQs
734. Marc - 20th Jul 2011 - 4:25 am
IE8 & IE9 work great but I can’t seem to figure out this issue with IE7. Can anybody give me a hint as to what I’m doing wrong or have a solution?
Here is the error message from IE7:
Webpage error details
User Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)
Timestamp: Wed, 20 Jul 2011 04:22:11 UTC
Message: Object doesn’t support property or method ‘addClassName’
Line: 1357
Char: 4
Code: 0
URI: http://XXXXXXX.org/assets/js/cropper.uncompressed.js
735. 60+ Stunning Scriptaculous Applications - 31st Jul 2011 - 1:27 am
[...] JavaScript Image Cropper UI, using Prototype & script.aculo.us [...]
736. Jai - 9th Aug 2011 - 10:17 pm
I included all scripts, but i get the error – Draggable is undefined in cropper.js
I included all .js files including prototype,scriptaculous, etc but still see this error. Is there anything obvious that i am missing here? Please let me know.
thanks,
Jai.
737. Jessica Jenty - 8th Oct 2011 - 12:15 pm
Your code running very very well its really need donate, but i can’t find that how i can store my cropped image in my own folder ?
Any help would be greatly appreciated.
738. Dave - 8th Oct 2011 - 1:17 pm
Jessica,
This is just the UI for the cropping in JavaScript, it doesn’t do the saving of the images directly itself. There are details/links to examples in the FAQ page for various languages.
Hope that helps,
-D
739. Dave - 17th Oct 2011 - 4:09 pm
First, thanks a lot for Cropper, it’s quite fantastic.
One problem though… I can’t get it to work with mobile devices. I’ve tested on the iPhone (iOS4/5) and a number of Android devices and all fail. On the iPhone the scaling selection centers on a tap, but you can’t slide it around or resize it.
The Android devices are roughly the same, although some seem not to load the script at all, at least no resizing selection appears.
Am I doing it wrong? Or is it possible to improve for mobile devices? I was thinking of using the tap gesture to center the box and adding zoom in/out buttons to rescale?
Thanks for any ideas and thanks again for Cropper.
740. söve - 19th Oct 2011 - 2:37 pm
Thank u for posting.
741. Chris Zegelin - 20th Dec 2011 - 11:19 pm
Great script, integration was easy, until I needed to mess with the image that was displayed for cropping. I need to crop the very large original image, while displaying a smaller version for the crop tool. I found that the transform from the crop tool return data is insufficient to crop the original. The height and width return data is redundant given the two x,y data points. Is there a way I can modify cropper.js to return the image width and height, not the crop box width and height? I can probably figure it out myself, but I also think this would be a very good change to the tool.
Great work… Thank you
742. Christopher Thomas - 20th Dec 2011 - 11:34 pm
@Chris Zegelin: in all seriousness, your best option is merely to stop using Prototype and switch to jQuery, I was once a stanch supporter of Prototype, then I realised their plan for version 2.0 is to drop extending the DOM because it’s a super bad idea and to go with wrapping the DOM instead.
which is basically exactly what jquery does, except you can have jquery today and you most likely will have to wait about 2 years to get prototype 2.0, they have been talking about it for years.
then, even if prototype 2.0 does arrive, which is doubtful, jquery 2.x will be available and will blow the socks off it, prototype just didn’t evolve fast enough and now it’s dead, just waiting for fish to pick it’s bones clean, which is what libraries like backbone and underscore are doing.
save yourself the trouble, invest now in switching to jquery and forget prototype, it’s a dead library and has been for years, although nobody remembered to tell it. I invested the time and now I realise just how much better things are, I don’t look back, you shouldn’t neither.
and before anybody pipes up saying prototype isn’t dead, stop deluding yourself, this, is a dead parrot!
743. weaselspleen - 12th Jan 2012 - 2:49 am
Christopher, with all due respect, when someone asks for advice on how to do something with a tool, telling them to completely change to a different tool is generally not particularly helpful.
I mean, if I ask about ways to tune the fuel injection on my Oldsmobile, telling me Oldsmobile is dead and I need to get a Chevy really doesn’t help me at all.
744. ajax by lizuka - Pearltrees - 12th Jan 2012 - 1:47 pm
[...] DEfusion.org.uk » JavaScript Image Cropper UI, using Prototype & script.aculo.us Changed tests to use google ajax libraries API to load prototype & script.aculo.us Added option to not auto include the cropper CSS file No-longer package prototype & script.aculo.us with the release #00008 – Fixed bug: Dynamic include of cropper CSS expected cropper.js and failed when using cropper.uncompressed.js [...]
745. carousel by davidlafon - Pearltrees - 12th Jan 2012 - 1:49 pm
[...] setting a ratio when both min width & height passed, the ratioDimensions must be passed in DEfusion.org.uk » JavaScript Image Cropper UI, using Prototype & javascript form Javascript Web development ajax [+] directory extensions JavaScript fades [...]
746. Johan Fredrik Varen - 25th Jan 2012 - 2:55 pm
Great tool!
I’ve got a small problem, though. I use the cropper to crop images, where the actual image cropping is done on the server. The img element is updated with the cropped result without any page reload. The cropper is still visible, though. What I want is to be able to hide and reset the cropper, but the Cropper.reset() method doesn’t seem to do what I want. I also would like the user to click outside the selected area to hide the cropper. This should be possible, right?
747. Dave - 25th Jan 2012 - 4:09 pm
@Johan :
Please see the “Dynamically changing image” example/demo for how to remove/reset the cropper. You could use this functionality to remove the cropper when a user clicks outside the image.
748. Lubo - 27th Jan 2012 - 6:35 am
Congratulations, great work!
Please help to resolve the following problem:
Cropper works perfect if the is placed in a pure place. If is inserted in a table cell ( ), then the image disappears and cropper works only with background color (no image!). I have tried a lot of variants: with/without own div tags for images, with/without PHP, static/dynamic croppers and other things. The effect is always the same. Please help!
749. Lubo - 27th Jan 2012 - 6:40 am
Sorry, I cant see brackets in the previous message. Now I’m posting it without brackets:
If img tag is inserted in a table cell (tag td, tag img, tag td end), then the image disappears …
750. Dave - 27th Jan 2012 - 11:50 am
@Lubo
I’ve had reports before that the cropper doesn’t work within a table cell, but I’ve also had reports that adding the table-layout:fixed style to the table helps.
Leave a comment
No HTML please, only textile. For code please use [lang]...[/lang] tags (e.g. [html]...[/html] for HTML)