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.
Learn More: 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 … 24 23 22 21 20 [19] 18 17 16 15 14 … 1 » Show All
380. Ruud Boon - 5th Nov 2007 - 4:41 pm
Hmm. I only works one time. When i close the browser and repopen the page it works again.
379. Ruud Boon - 5th Nov 2007 - 3:16 pm
I Found the problem. The script places the image in a div and uses the opacity of the imgCrop_clickArea. By addding this tag to the cropper.css the problem is solved
.imgCrop_wrap img{
z-index : 500; // don’t think you need this one.
opacity: 1;
filter:alpha(opacity=100);
}
378. Ruud Boon - 5th Nov 2007 - 2:50 pm
—> Magnu
Same problem over here. Image in Iframe disappeares. I see the background of the iframe. I didn’t find a solution for it. I hope that i can’t find something
377. kevin - 2nd Nov 2007 - 6:44 pm
Hi cannot get to to work in IE6 when image is within a table cell.
I’ve searched here and this the only suggested solution seems to be from GhostGambler (post num 308, 7th Sep 07).
He suggests replacing this:
new Cropper.Img(
‘thaimage’,
{});
}
with this:
window.onload=function(){ new Cropper.Img(
‘thaimage’,
{});
}
my javascript is very weak so I’ve not been able to fully understand the fix.
Could someone please show me how to ammend this specific code from my site? Or, another fix?
Event.observe(
window,
‘load’,
function() {
new Cropper.Img(
‘testImage’,
{
minWidth: 130,
minHeight: 130,
ratioDim: { x: 130, y: 130 },
displayOnInit: true,
onEndCrop: onEndCrop
}
)
}
);
Many thanks,
Kevin
376. scriptomaniac - 2nd Nov 2007 - 2:50 pm
If I’m using ratio, I’d like the preview image to have a fixed size and always contain the exact same contents as the square/frame. An example: if the image is 1000×1000, the selection area/square is currently adjusted at 100×100 and the preview window is 200×200, I’d like the 100×100 selection to be scaled up to fill the 200×200 preview window. Possible?
375. Magnus - 1st Nov 2007 - 3:17 pm
I’ve had some problems with the script in IE7 when displaying the script in an iFrame. IE7 won’t display the image, it only displays the cropped border and then a black background.
Any one know about this issue?
374. Giorgio - 31st Oct 2007 - 9:24 am
I ask excuse for my English.
I have used cropper tool and it works.
I have this problem; the image should be updated in background I introduced it to the inside of the ajax control “UpdatePanel”, using a timer for the refresh, planned for test to a minute.
But within the first minute the thing works, then a time carried out the first refresh I have not more the possibility of to choose the area.
How could it be done to resolve this problem?
thanks
373. Bogguard - 30th Oct 2007 - 11:41 am
Hi,
very nice library…Exactly what i was looking for. One small bug i discovered.
The onendcrop is not called when you move the cropbox with the arrow keys.
Kind regards
372. TheIceman5 - 26th Oct 2007 - 12:31 am
Yo Freek d00d, this is what you want. http://mondaybynoon.com/2007/01/22/crop-resize-with-javascript-PHP-and-imagemagick/
371. Freek Felling - 25th Oct 2007 - 6:19 pm
Hi Dave,
Excellent work on the cropper tool.
Is there anyway this script can be modified to resize images. So that you don’t draw a selection but drag the image to a smaller or larger size.
If this is possible you could retrieve the results and scale the images server side.
Thanks a lot fot the great work.
370. Paul - 25th Oct 2007 - 12:23 pm
First of all – this is GREAT piece of script! Very handy! Thank you to the author!
Now, I’ve never got a grip on Javascript, and these are probably simple questions, so bear with me:
1) Is it possible to have the default crop area start as equal to the image width (and keep the preset ratio)
2) I use two different image ratios on my web page – 5:3 and 3:1 (in both cases, as you can see, I mostly like to preserve the image width of the original, see q1 above). Is it possible to have a radio button or similar to choose different ratio options for the current image?
Any help appreciated! :-)
369. dg - 24th Oct 2007 - 5:35 pm
Has anyone had any luck fixing bug 00027(problems when cropper inside table in IE 7) or come up with a work around?
368. Dave Schlaegel - 24th Oct 2007 - 5:03 am
Hi Harry. I am not sure how you are doing the multiple instances. So hard to tell you what’s breaking. Do you have a link I can see it break. Let me know if you do and I will check it out. It will be an easy fix.
To the other Dave who developed this genius thing. I’m making a JQuery version of this I will send to you if you like. Only reason is I use JQ all over the place so the Prototype / script.aculo.us confict with any of my main pages.
367. Harry - 24th Oct 2007 - 12:43 am
Dave Schlaegel and Dave:
I’m currently using multiple instances of the cropper on one page, however IE is breaking the second instance. The image displays fine and the cursor changes to the crop tool icon. However nothing is being selected. Firefox works as it should (FFWAIS I’m proposing should be a new web acronym)
Any help would be appreciated.
366. Oren Goldfinger - 19th Oct 2007 - 2:42 am
Hey Dave,
Thanks for this awesome tool! I found a bug (and already created a fix) that I wanted to report to you.
On the Remove function, you have these 2 lines of code:
// remove the elements we inserted
this.imgWrap.parentNode.insertBefore( this.img, this.imgWrap );
this.imgWrap.parentNode.removeChild( this.imgWrap );
I’m not sure what that is doing but it was throwing an error for me. I made it a bit more defensive and then everything worked perfectly:
if(this.imgWrap != null && this.imgWrap.parentNode != null){
// remove the elements we inserted
this.imgWrap.parentNode.insertBefore( this.img, this.imgWrap );
this.imgWrap.parentNode.removeChild( this.imgWrap );
}
Also, as a seperate issue, the uncompressed javascript wasn’t working for me…is it corrupt?
365. seb - 19th Oct 2007 - 1:23 am
Greetings,
The image is displayed but nothing else happens… Just a regular image on a regular web page… I am posting the essential of my code.
Thanks for any help !
364. seb - 19th Oct 2007 - 1:15 am
Greetings !
The image is displayed but that’s it !... there are no javascript funcitonality… nothing… just a regular image on a web page.
Thanks for any help… this is the essential of my code.
$styles = ’