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
660. rv0 - 11th Feb 2010 - 5:20 pm
I think I found a bug here.. Or maybe it is a feature.. however, it’s very annoying in my usecase. If anyone knows a way around it without adjusting the cropper.js itself, please tell me.
onEndCrop gets called every time you click somewhere on the page. See for yourself: add
in the callback function in one of the demo’s.. you’ll see an alert wherever you click on the page.
Why is this? isn’t it supposed to be called when you release the crop or a handle?
Any workarounds?
thx in advance.
659. SEO Agency - 5th Feb 2010 - 5:20 am
Realy this is great work.
658. Dave - 30th Jan 2010 - 6:54 pm
Hitesh: There must be a problem with your path or version of script.aculo.us or prototype. Please ensure they’re actually being loaded.
Ron: No the cropper as released does not support this, however if you look at the boundary checking stuff I’m sure you can easily add this functionality to use custom supplied boundaries/margins.
657. Ron - 30th Jan 2010 - 5:47 pm
hello dave,
does the cropper ui allow to define a moving area or margins at the top, left, bottom, right, so that the selection can be only moved within this area and not within the whole image?
thx,
ron
656. Hitesh - 30th Jan 2010 - 1:14 pm
Hi Dave its a good work.
But when i
give path of correct path of scriptaculous.js by removing the google API’s , it does not work pls help!!
655. John - 21st Jan 2010 - 9:22 am
please disregard my previous, I delved into the code and changed it to suit my needs by changing the Cropper.ImgWithPreview subInitialize and subDrawArea functions.
654. Steven R - 21st Jan 2010 - 8:37 am
Javi,
Check out http://www.dolem.com/lytebox/, this just help you do what you are trying to acheive. It worked for me perfectly.
Steven
653. John - 21st Jan 2010 - 7:33 am
Is there a way for me to increase the size of the preview image, rather than have it be minWidth x minHeight?
652. tirengarfio - 19th Jan 2010 - 5:47 pm
Steven R:
thanks for your recomendation, but could you give a link? Anyway I prefer an open source one.
Javi
651. Steven R - 19th Jan 2010 - 1:47 pm
tirengarfio,
I managed to use this cropper with the lytebox2. It is allows for additional content rather than just images like in line HTML, new pages and iframes. Use this with your cropper on a different page for a seamless application.
Good Luck
650. tirengarfio - 19th Jan 2010 - 1:36 pm
Hi,
i want to operate defusion cropper inside a modal box. I have tried it with Lightbox2 and ModalBox but it doesnt work..(it just doesnt show the cross when you put the mouse cursor over the image).
Do you know any library for modal box that works with this cropper?
Regards
Javi
649. Dave - 14th Jan 2010 - 2:02 pm
Rob: The cropper doesn’t set any fields in your form you have to explicitly provide a callback that takes the co-ordinates and puts them in a form (or does whatever else you want with them) so just do your scaling in the callback that you’ve setup. See the callback section in the above documentation for more details.
648. Robert - 14th Jan 2010 - 1:43 pm
Steven,
If you need help with PHP cropping using this tool I can send over a basic version of my script (e: info at ninetyone degrees dot com)
Dave,
I’m sure you have enough on your plate but I was just wondering.. I have 4 fields that are populated whenever I perform a crop (x1,y1,x2,y2). I am looking to apply a multiplier to these values before they are set as the original image and scaled down image are different sizes. I realise I could just do the multiplier after I submit but could you tell me where in your code the fields are set.
Everything else is perfect (so far)
Rob (N Ireland)
647. Dave - 14th Jan 2010 - 10:51 am
It’s up in the box on the left at the start of the post, but here it is again: http://code.google.com/p/javascript-image-cropper-ui/wiki/FAQs
646. Steven R - 14th Jan 2010 - 10:49 am
I understand this Dave, but can’t find your FAQ page anywhere. I am using PHP at the server end, just looking to know how to use the saved coordinates of cropped image.
645. Dave - 14th Jan 2010 - 10:40 am
Steven: Please see the FAQ’s the cropper is only the UI part and you need to save the cropped image in your appropriate server-side language.
644. Steven R - 14th Jan 2010 - 10:37 am
Hi
Just found this site and think its awesome, I have been playing around with it and so far implemented a small application where I have an AJAX image upload to call back and display the uploaded image on page where I then use this cropper to set a fixed size for user to crop. At this point I am stuck. Can anyone point me in the right direction to then save this newly croped image before leaving the page?
Thanks in advance.
Steven
643. Robert - 14th Jan 2010 - 9:52 am
Dave,
Thanks for that! Never seen that onloadCoords and now I’m flying! Will certainly be implementing this in my personal site and I’ll even make a lil donation to help you out.
Cheers,
Rob (N Ireland)
642. Dave - 13th Jan 2010 - 5:09 pm
Timothy:
I’m not sure what you mean, the cropper doesn’t stretch images – in fact doesn’t actually do anything to the image tag but wrap it in the HTML for the cropper.
If you’re having problems can you please post to the google group the comments are not really useful for having conversations to fix issues.
641. timothy - 13th Jan 2010 - 5:02 pm
Dave,
I think I could implement a recalculation of the crop based on the ratio ‘picture size’ ‘width 500pixels’. But then you still have to make your crop on a stretched picture. Wich isn’t easy. Is there no way to prevent js cropper of stretching pictures? For my site it doen’t matter if that results in different layouts depending on the uploaded image’s size.
Thanks for all the help I get here
Leave a comment
No HTML please, only textile. For code please use [lang]...[/lang] tags (e.g. [html]...[/html] for HTML)