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 Resources: 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 513 comments so far, join the discussion.
Pages: « 26 … 11 10 9 8 7 [6] 5 4 3 2 1 » Show All
120. Jason - 23rd Oct 2006 - 7:53 pm
Awesome tool, but I was wondering…is there an easy way for me to clear the crop markers and the translucent overlay after I’ve finished doing what I need to do with the cropping?
119. Andrea - 23rd Oct 2006 - 3:13 pm
In post 117, at the line
imagejpeg($thumb, null,100);
just replace “null” with a path+filename
118. David Kingston - 23rd Oct 2006 - 10:17 am
I love the interface. By far the best I have come across. But has anyone developed the basic version with PHP to load $image then output the cropped image result to a specified directory?
117. Geartjan Meijer - 22nd Oct 2006 - 12:55 pm
I mean:
$file = “test.jpg”;
header(‘Content-type: image/jpeg’);
$x1 = $_POST[‘x1’];
$y1 = $_POST[‘y1’];
$width = $_POST[‘width’];
$height = $_POST[‘height’];
list($ow, $oh) = getimagesize($file);
$image = imagecreatefromjpeg($file);
$thumb = imagecreatetruecolor($_POST[‘width’],$_POST[‘height’]);
imagecopyresized($thumb,$image,0,0,$x1,$y1,$width,$height,$width,$height);
imagejpeg($thumb, null,100);
imagedestroy($image);
imagedestroy($thumb);
116. Geartjan Meijer - 22nd Oct 2006 - 12:53 pm
This works fine
115. Frank - 21st Oct 2006 - 3:58 am
How do you actually do something to the image instead of just selecting certain areas?
114. Justin - 19th Oct 2006 - 11:48 am
$t1 = $ow / $width * ‘120’;
$t2 = $oh / $height * ‘160’;
imagecopyresampled($thumb, $big, 0, 0, $x1, $y1, $t1, $t2, $ow, $oh);
This is a fix for that PHP script that mike made… the fix that max posted didnt work for me. I am using a fixed size for my output of 120×160 so replace the 120 and 160 with what you are using. Also my source image was the same ratio as my new one, i haven’t tested for stretching if the ratio is different but i would guess it to be an issue… Thanks for the great tool!
113. Andrea - 17th Oct 2006 - 2:39 pm
I was looking for a nice way to build avatars for my site users, your solutions looks amazing, can’t wait to implement! The preview box is damn good too! :)
112. Dave Marks - 13th Oct 2006 - 1:39 pm
This cropper is absolutly brilliant – far better than anything else I’ve seen!
A feature that would be nice – rotating…
Flipping between portrait and landscape – this can be done already by submitting the current crop area and ratio to a dynamic page which does a bit of maths and then starts the crop again with set postions and reversed ration but it would be nice if this could be handled by js
Rotating by an arbitary degree would also be nice but not sure if this at all possible…
Thanks! Keep up the good work!
Oh and Multiple monitors are aweseom – check out multimon at www.realtimesoft.com – i couldn’t live without it on multi monitor setups!
111. Dave - 9th Oct 2006 - 9:11 am
Ravikiran:
The test1.html should not have been included as part of the release and I will ensure it is removed in the next release.
As for the functionality this test page provides (zooming) it is not something that I currently have as part of the official release as I’ve been concentrating on fixing the major bugs for now. The test1.html file was submitted by someone else but is quite buggy in it’s implementation. I do intend to add zooming at some point but will start from scratch with that functionality once I’ve cleared out all the major bugs.
110. Ravikiran - 9th Oct 2006 - 8:10 am
your’s example test1.html is not working well, after image got small size the cropping is not working well and also after image zooming the cropping is not working will. plz fix the bugs , bcoz i want the test1 solution in my project.
109. Ravikiran - 9th Oct 2006 - 5:18 am
hi,
i couldn’t find setParamsAfterZoom() in crooper.js file. Actually my requirement is we placed image in div, that div in a table. i am unable to get correct cropping area and if i scroll div down, i am unable to do select the area where scrolled area of div.
can u let me know how to solve this problem.
Thanking u,
Ravikiran
108. max - 5th Oct 2006 - 8:35 am
Mike Burrell, I resolved this problem!
Replace string:
imagecopyresampled($thumb, $big, 0, 0, $x1, $y1, $x2, $y2, $ow, $oh);
with
imagecopyresampled($thumb, $big, 0, 0, $x1, $y1, $width*2, $y2, $ow, $oh);
Enjoy!
107. Chris - 2nd Oct 2006 - 8:17 pm
I think I found the problem I was having earlier. Cropper does not do well if the image is less than 100% of the size of the enclosing div and right aligned. I had my image in a floating div of 50% of the canvas, and Cropper got sick when the image was a small one taking up less than the full div.
Dave: if you are working on updates, please consider this case.
Also, please consider parameterizing the location of the cropper.css file instead of assuming it is in the same directory as cropper.js.
106. Dave - 2nd Oct 2006 - 1:24 pm
Lucas:
Yes the overflow bug, plus the other bugs due to different CSS positioning of the parent element(s) have been fixed.
105. Lucas Young - 2nd Oct 2006 - 11:19 am
Hi Dave
Any chance of that div overflow bug being resolved in the next release?
cheers
Lucas
104. Dave - 2nd Oct 2006 - 10:43 am
Ben:
The overlays in the croper UI are actually implemented differently for IE and all other browsers. This is because the original implementation was very sluggish in all the Mac browsers and the new implementation caused problems in IE.
However at the weekend I spent some time playing around with the cropper and I have improved the performance of the original implementation so all browsers now use the original implementation. This change fixes bug #00010 and will allow me to implement some other changes, I am now aiming to get the next release out sometime within the next week.
103. Ben Schwarz - 2nd Oct 2006 - 7:08 am
Due to the way that this library is written you are not able to resize your image and crop it within the same interface.
This is because the image src is written as a background image of a div, sized and positioned accordingly, unfortunatly to my knowledge you cannot resize a background image.
Thanks for the library David, I may end up writing my own implementation of it in order to resize the image in the DOM, I’ll try to remember to post back if I get to it.
102. Chris - 30th Sep 2006 - 1:55 am
I’m not very experienced with Javascipt, but I didn’t think I was a tard. Cropper has me thinking otherwise. I have followed the examples very closely and no matter what I do, the img I pass to Cropper is MOVED to the end of my layout (upon loading the page) and RESIZED slightly. Furthermore, while I can see the callback is being triggered, no selection box is created. It also seems as though the active area becomes the whole canvas -the callback is triggered when I click ANYWHERE on my page.
I have gone so far as to remove all my CSS and pull the img out of the enclosing td. No go. Any ideas? Please? please…
-Chris
101. Bill Smith - 28th Sep 2006 - 6:35 pm
Dave,
I’ve been playing around with this some and I figured out the onEndCrop problem. It has to do with the calls to Event.observe. The current implementation is passing in anonymous function refs. I found if I did something like the following, then everything cleaned up.
The other problem I’m having is that I have a routine that changes the cropper to/from a fixed ratio by clicking a checkbox. In Firefox it works fine, in IE, it seems that it takes an extra mouseclick to get the cropper to reset. Here is the function I’m using in my wrapper class. I’m sure there is a better way.
Leave a comment
No HTML please, only textile. For code please use [lang]...[/lang] tags (e.g. [html]...[/html] for HTML)