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.
Related Website: 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 … 22 21 20 19 18 [17] 16 15 14 13 12 … 1 » Show All
340. Dave - 5th Oct 2007 - 9:45 am
Nick:
That is a known issue with the current cropper, as the code that does the dynamic includes for the stylesheet tries to find the path based on the location of cropper.js. As you’re not including the cropper.js file it can’t find the path of it. Simple solution (for now) is to rename cropper.js to cropper.compressed.js and croper.uncompressed.js to cropper.js while you’re playing around with the uncompressed version.
339. Nick - 5th Oct 2007 - 9:42 am
Hi there,
Nice work, however how come when the cropper.uncompressed.js file is used instead of cropper.js it doesn’t work?
I’m trying to update the textboxes whilst resizing and allow them to control the cropping?
338. Austin - 3rd Oct 2007 - 2:34 am
Is it possible to save the cropped section without using gd or something similar ? :\ MediaTemple GS servers don’t allow you to install programs like that.
337. Vinay - 27th Sep 2007 - 9:40 am
Oops, the prev comment was supposed to have a img src example that used a width of 350px.. that got stripped off..
336. Vinay - 27th Sep 2007 - 9:39 am
scales the image to 350px. That causes the script to get the x,y coordinates wrong. Issue disappears if I remove width specification.
Has anyone figured out a simple solution/fix for this issue?
Thanks,
Vinay.
335. Ebru - 26th Sep 2007 - 9:08 am
Hello Serdar,
/**
* Create an area to click & drag around on as the default browser behaviour is to let you drag the image
*/
.imgCrop_dragArea {
width: 100%;
height: 100%;
z-index: 200;
position: absolute;
top: 0;
left: 0;
overflow: scroll;
}
Setting overflow property is enough for your need.
334. Mike - 26th Sep 2007 - 8:31 am
Rightio, I’ve got it working; but it may seem like a bit of a hack for the way our site works. I’ve introduced three new options into the initialize.
overFlowEnabled: 0,
containerWidth:0,
containerHeight:0
Then i’ve changed setParams to enable/disable overflow on imgWrap based on the information given above.
if (this.options.overFlowEnabled) {
// resize the container to fit the containing div
$( this.imgWrap ).setStyle( {‘width’:this.options.containerWidth + ‘px’,’height’:this.options.containerHeight + ‘px’, ‘overflow’:’auto’ } );
// resize the dragArea to fit the image
$( this.dragArea ).setStyle( { ‘width’:this.imgW +’px’,’height’:this.imgH +’px’ } );
}else{
// resize the container to fit the image
$( this.imgWrap ).setStyle( { ‘width’: this.imgW + ‘px’, ‘height’: this.imgH + ‘px’ } );
}
hopefully that helps a few people, and seems to be x-browser.
333. John - 25th Sep 2007 - 9:00 pm
Not sure if I’m missing something, but what do I do with the coordinates to show the croped image on another page? Do I use PHP to crop the image? Does the javascript do it on the page?
332. Paul - 25th Sep 2007 - 8:03 pm
Hi Dave,
is there a way to stop the crop beyond a maximum amount on the x and y? so we can have the crop set to a set amount and not allow the user to change the size
331. serdar - 25th Sep 2007 - 6:00 pm
Hello;
I have been using the codes you have written, in one of my projects. However I do have a problem that I believe you may provide an easy solution.
I use the code in Asp.net. I need to enlarge a picture in webform while in a panel. Due to the limits of the panel, the image is shown only at the size of the panel and the scroll bars are shown on the panel to see the other part of the image. Although in design view, the image is shown in the panel correctly, when aspx page is run, the image over sizes and goes out of the boundaries of the panel.
Is there any way to correct this problem?
Thank you.
330. Mike - 25th Sep 2007 - 11:40 am
Heres the full fix,
// resize the container to fit the image
$( this.imgWrap ).setStyle( { ‘overflow’:’auto’ } );
// resize the container to fit the image
$( this.dragArea ).setStyle( { ‘width’:this.imgW +’px’,’height’:this.imgH +’px’ } );
Got a few issues still. but its a fix for what I need.
329. Mike - 25th Sep 2007 - 11:27 am
Hey Dave,
Tried that and I couldn’t get it working, although I do think it is possible that way. One other method I have succeeded with is changing the JS file to use
// resize the container to fit the image
$( this.imgWrap ).setStyle( { ‘overflow’:’auto’ } );
Rather than implicitly setting the height and width of imgWrap.
It works the location out correctly but the grey overlay doesn’t correctly work, which seems like an easy fix. If I persue this method I will post up the code.
Thanks for the help.
328. tunc - 24th Sep 2007 - 8:53 pm
codes are be disable !
327. tunc - 24th Sep 2007 - 8:52 pm
yeap thanks for this nice script. Maybe you can help me for this posting. How can i do cropping image save as. Sorry IM use asp and this code.
thanks for thy answer.
326. Pat - 24th Sep 2007 - 1:59 pm
Hello,
This is certainly a wonderful script. I have a question, what would I need to do to have the x1,y1,x2,y2 callbacks updated dynamically as the drag/resize is actually being done rather than at the end of the crop action? I have seen drag scripts where the coords are updated during the drag/resize itself. Any help would be appreciated.
Pat
325. Dave - 21st Sep 2007 - 3:56 pm
Mike:
I spent many an hour trying to figure out what was going on with IE when the cropper is within a wrapper that has scrolling overflow, so I too know what it feels like for it to drive you mad.
My short answer is, I’m not sure if it’s possible because I have no idea why IE is causing it to ‘POP out’ of the element when you attach the cropper. However if memory serves me correct you can try this: remove the overflow setting, attach the cropper, then attach the overflow setting.
Let me know if that works, if so it might be something the cropper could do itself.
324. Mike - 21st Sep 2007 - 10:57 am
Brilliant tool, but has anyone got a workaround/fix for the scrolling issue in IExplorer. I’ve created a Div with the following style, {width: 924px; height: 487px; overflow: auto;padding: 0; margin: 0; float: left; } the image to crop sits inside this div. As mentioned in your issues bit, it breaks out in IE but is perfect in FF. Its driving me mad, if it can’t be done it can’t be done. But i’d love to get this working.
323. OHR GUY - 19th Sep 2007 - 3:40 pm
very excellent.
Do you provide this in Asp.net? thanks
322. Debasis Sil - 18th Sep 2007 - 10:17 am
Sir,
on basis of the co -ordinates the image get marked when it is focused on the text box.
But what i can not achieve is when the focus goes to the next box i could not reset the cropping and another layer comes on the top of the existing one.
plz help.
Regards
Debasis
321. Flo - 17th Sep 2007 - 6:29 pm
Wow… atleast in FF2/Win everything runs super-smoothly. Before I start reading 300+ comments, is there already a PHP script known somewhere I could use for saving the cropped area?
Thanks a lot – nice work.
Leave a comment
No HTML please, only textile. For code please use [lang]...[/lang] tags (e.g. [html]...[/html] for HTML)