Details
| Version | 1.2.1 |
|---|---|
| Last updated | 6th October 2009 |
| 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.
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.
Comments
There have been 667 comments so far, join the discussion.
Pages: « 34 33 32 31 30 [29] 28 27 26 25 24 … 1 » Show All
580. lorenzo - 2nd Jul 2009 - 8:11 am
Hi! I experienced the same problem in Firefox 3.5
All healed when I explicitly added cropper.css to the head tag :)
(not kidding, somehow i forgot to do it, or hoped that the javascript will do the job, and it worked for many months under everything, except this new FF3.5)
Hope it helps
regards,
lori
579. Tim - 1st Jul 2009 - 9:32 pm
Thanks for the quick reply. I don’t get any errors. When the page loads I am not getting the crop box over the image and the preview isn’t being show correct. I checked it in other browsers and it was working fine in them.
578. Dave - 1st Jul 2009 - 7:14 pm
Tim:
I’ve just try the demo/test files in FF 3.5 and everything is working fine for me so it could be an integration issue. Do you get any errors in the console?
577. Tim - 1st Jul 2009 - 5:50 pm
Hi, I wanted to see if anyone else was having problems with the Cropper not working in Firefox 3.5.
576. Abcdef - 29th Jun 2009 - 12:41 pm
Finally you gave us the script we are seeking for from very longer.
575. Dave - 19th Jun 2009 - 10:33 pm
Leonard,
I see what you mean, if you really want to use the preview version then you’ll have to try and apply your factor to the min & max dimensions you are setting for the cropper.
574. Dave - 19th Jun 2009 - 10:12 pm
Leonard:
We’re doing exactly the same thing at work, we accept the upload, resize it to a maximum size for the display of the cropper (say 600px wide) and then also put the scale value of the original to the resized cropper image in a hidden form field. Then when you submit the cropper you just apply the scale value to the values the cropper supplied. If you’re using the preview one (so you have a fixed-size result) then all you need to do is crop the image applying the scale then resize to your fixed dimensions.
Hope that makes sense,
-D
573. Ronni - 5th Jun 2009 - 8:09 am
Amazing tool which is so simple to implement and use.
572. szdszd - 31st May 2009 - 9:19 pm
Great work! But there is one bug,i can’t us this cropper while i haven’t resolved it. When i use ratio(on small preview user can select crop area,and then on original image crops area,that is needed),the crop area is jumping… Does anyone know,is it possible to fix this bug? Thanks.
571. Rex - 29th May 2009 - 12:02 pm
Great work! thx so much :) :)
570. vikram - 29th May 2009 - 11:34 am
can any one let me know how can we save the crop image in any named folder…?
i m using asp.net 2.0 with VB.netlanguage..
will wait for reply
thanks regard
vik
569. Legall - 25th May 2009 - 2:52 pm
Hello all,
I’m looking for to make a fixed dimension result (x:160px and w:210px) and save the result in a folder with another name .
Thanks a lot,
L
568. James - 10th May 2009 - 3:16 am
multiple croppers in an image (allow multiple croppers on a single iamge). Can you please email me the solution? I’ve read a few comments here, but no luck. I thought it had been solved. it is kind of urgent. Thanks So much.
567. James - 10th May 2009 - 3:16 am
multiple croppers in an image (allow multiple croppers on a single iamge). Can you please email me the solution? I’ve read a few comments here, but no luck. I thought it had been solved. Thanks So much.
566. SanjayU - 6th Apr 2009 - 1:46 pm
Mahesh,
ASP.NET (C#) version here: http://geekswithblogs.net/SanjayU/archive/2008/09/08/asp.net-image—photo-cropper-in-c.aspx
565. Mahesh - 6th Apr 2009 - 12:01 pm
Hi , Thanks for such a good article. Is there any possibilities to save the preview image using asp.net .. Thanks in advance
564. Marco Borsoi - 13th Mar 2009 - 3:57 pm
I’m facing the same problem that Nitesh (post n#536 – page 27).
He wrote: “I have tried this control using Ajax ModalPopupExtender. The Crop Box is not getting displayed. Can any one please help me.”
If we remove the style=”display:none” of the referenced panel by the PopupControlID ModalPopupExtender, the cropper works perfectly!
Now the problem is that the the panel is showed as soon the page starts to load. Seconds after the load, the ModalPopupExtender hides it.
How to solve this?
Maybe a timeout? I don’t know… I’ll continue to research…
Note: this problem only occurrs on IE, on FF everything is fine.
563. Art - 6th Mar 2009 - 9:55 am
I tried to use this in Joomla! but it doestn’t work… I suppose, there is some conflict with mootools.js integrated in Joomla. Can someone help?
562. Kidino - 1st Mar 2009 - 11:00 pm
Dave, I replied to your email. Maybe you can also check the Spam/Junk folder too. :)
561. Kidino - 1st Mar 2009 - 2:29 pm
Hmm… I need some customization/enhancement. Can you do this Dave? Or know anybody who can help me? Things like multi-cropper in a single image and some extra behavior coding… I can do server side programming but JS/JSON/freakinly-awesome frontend interface is not my strong-side.
Leave a comment
No HTML please, only textile. For code please use [lang]...[/lang] tags (e.g. [html]...[/html] for HTML)