Announcement: V2 plans & fund raising
Please take a moment to read my plans for version 2 of the cropper and how you can support it.
Details
| Version | 1.2.0 |
|---|---|
| Last updated | 30th October 2006 |
| 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=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.
Other Links: SEO Agency advanced JavaScript experience can enhance your site functionality. Adding the JavaScript Image Cropper is a good way to improve the user experience.
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.
Announcement: V2 plans & fund raising
Please take a moment to read my plans for version 2 of the cropper and how you can support it.

Comments
There have been 494 comments so far, join the discussion.
Pages: « 25 … 11 10 9 8 7 6 5 4 3 2 [1] Show All
20. Mike - 9th Jun 2006 - 2:31 am
“I’d like to know if this is feature that people would like to see added: to dynamically change the image within the cropper without reloading the page.”
Yes please! I’m working on a PHP web app with a form where a user uploads several jpegs, and I want to immediately let the user crop them to a 150×150 square – before submitting the final form.
What I’ve done (tell me if I’m doing this all wrong) is made a page with several input type=’file’ form elements, hooked up the submit button to submit to an invisible iframe that moves the file into place after upload, updates the src=’’ of the image tag, then tells the parent document to display the div containing the cropper. Thus, you upload, crop, save, and move on to the next image, all on the same page.
Anyway, to do this I need to be able to change the img src=’’ of the cropper on the fly.
19. Andy Skelton - 8th Jun 2006 - 11:12 pm
I have a modified version of applyRatioToAxis that handles ratios more smoothly, without the jumping effect I was seeing, regardless of their common divisors. It alleviates the need for GCD calculation. Hopefully the resultant ratio is within an acceptable margin of error. Now we can push this feature out on WordPress.com! ;-) Thanks again for the great work.
18. Andy Skelton - 8th Jun 2006 - 10:25 pm
Howdy and thanks for the great tool! I am struggling with the ratios (when GCD is too low it breaks completely) and about to work up a patch.
Dave, if you already have something in the works please let me know.
17. Nick - 3rd Jun 2006 - 1:48 pm
I would like to repeat for all those who read this post- I have been searching for crop tool for months, this is the best solution, really the finest one ever- it works in Mozilla and IE ( the most used browsers) and it can be easilly integrated. There is only one other such feature-rich and bugfree tool, but it’s in Java and it is not free.
16. Nick - 3rd Jun 2006 - 1:33 pm
All works great, thanks for the quick fix. I’ll report the bugs if I find any.
15. Dave - 3rd Jun 2006 - 12:54 pm
Nick:
Thanks for pointing out the IE issues with the callback, version 1.1.1 that I’ve just uploaded should fix that problem.
As for your issues in Firefox, I’ve had no problems. Maybe it was a caching issue with the JavaScript, I’ve added something to the demo that should fix that if it was the case (or just do a hard refresh). But please tell me if you’re still having problems and whether it is within the demo or the test that comes with the download.
14. Nick - 3rd Jun 2006 - 11:16 am
Remove/reset cropper didn’t work on Firefox for me.
On IE – click ‘remove cropper’ and then ‘reset cropper’. The drag the select area – the crop values (co-ordinates) do not refresh.
13. Nick - 3rd Jun 2006 - 11:11 am
1.1.0 – More tha good News. I’ll add a link to this site everywhere I use this tool from now on.
12. Stoyan - 2nd Jun 2006 - 3:18 pm
Hello Dave !!!
‘Zoom’ is wrong, I meant to fit the selected area to the image area, as it were ‘crop to mask’ (photoshop). On some software the cropped image is resized to the original image size, so it looks it’s ‘zoomed’.
These days I’ll intergate this nice app of yours with phpthumb, I could host a demo if someone wishes.
11. Dave - 2nd Jun 2006 - 1:37 pm
Stoyan:
Can you expand on what you mean by a ‘zoom’ feature?
I think I know what IE is doing (it’s actually less obvious in IE 5.5 than IE 6), the thing is on the day that I launched I changed the entire way that the dark overlay is generated to improve performance on Mac based browser (Camino especially). I’m currently looking at the features requested by kiwo so I will also fix the IE performance issues as I believe I know what is going on there.
Nick: Thanks for the kind words, it’s nice to know that people like the cropper.
10. Nick - 2nd Jun 2006 - 12:37 pm
Just great !!!!! That’s going to be a killer app !! Unique interface and features, no such analog for JS on the web !!! I’ll integrate it in the CMS I’m just working on.
Hope Dave is going to continue the development – adding new features and removing bugs.
9. Stoyan - 2nd Jun 2006 - 9:52 am
It seems that area selection is not as smooth in IE as in Mozilla. IE recalculates something constantly and the ‘hour-glass’ appears at dragging.
8. Stoyan - 2nd Jun 2006 - 9:22 am
but I’d like to know if this is feature that people would like to see added: to dynamically change the image within the cropper without reloading the page.
Yes, it would be nice to add a ‘zoom’ feature.
7. Dave - 31st May 2006 - 7:28 pm
Kiwo:
Glad that you like the cropper. I’ve just spent a few minutes playing around with trying to get the image cropper to change the image dynamically; and I have to say that, currently, it’s not possible.
It is possible to apply a new cropper to a new image (or the same image but replaced) but there are a couple of errors that are related to the fact that the HTML that was added to the DOM for the cropper isn’t been removed.
I would have to work this in as a feature, but I’d like to know if this is feature that people would like to see added: to dynamically change the image within the cropper without reloading the page.
6. kiwo - 31st May 2006 - 7:35 am
The cropper is great! Thanks.
There is only one thing that I can’t figure out. I need to change image’s src attribute using javascript. And when image changes, i need to setup cropper for new image with its new size and contents. How can i disable an existing cropper to be able to create a new one for a new image?
TIA, kiwo
5. 1 Pixel Out » Blog Archive » Need a JavaScript image cropper UI? - 25th May 2006 - 3:58 pm
[...] This is one tool to bookmark if you’re thinking of building an image cropper into one of your apps. It’s built with the Prototype and script.aculo.us frameworks (which I strongly recommend by the way), it has plenty of configuration options and it feels very slick. I love how the UI is an exact implementation of what you get in Photoshop or any other commercial graphics package. Check the demo at Dave Spurr’s blog or read more about it on his doc page. [...]
4. Dave - 24th May 2006 - 1:45 pm
Chris:
I saw your original comment on the post yesterday and did a bit of research into the kenburns effect and thought that I could knock it up quite quickly (when I had some time), but didn’t have chance to reply to your post.
Although I designate these methods as being private you should be able to do:
myCropper = new Croper.Img( ...arguments... ); myCropper.setAreaCoords( { x1: 0, y1: 0, x2: 50, y2: 50 } ); myCropper.drawArea();The
method simply returns a clone of a coords object which has the
properties to allow you to use process same coordinates as another variable by value rather than by reference.
Hope that helps, if you have any more questions don’t hesitate to ask.
3. chris - 24th May 2006 - 12:34 pm
one question about the script:
is it possible to redraw the crop-box from outside?
i tried to call “setAreaCoords, cloneCoords, drawArea”, but no success…
(working on the timeline-”project” see above)
chris
2. chris - 22nd May 2006 - 10:02 pm
GREAT!!
looks very good :D
add a slider (timeline) with some markers, and you have a great tool for a JS-driven kenburns-creator :)
1. Kiddi - 22nd May 2006 - 12:17 pm
WOW, thanks! Been looking for a free version of a online cropper that looks nice. Kudos!
Leave a comment
No HTML please, only textile. For code please use [lang]...[/lang] tags (e.g. [html]...[/html] for HTML)