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 23 … 1 » Show All
560. Dave - 27th Feb 2009 - 10:53 pm
Md:
Prototype isn’t being included properly there, please check that all the required scripts are being loaded correctly.
559. Md - 27th Feb 2009 - 10:46 pm
I am trying to get the code working. But I am getting this error “Event.observe is not a function” on firefox and on IE “Event is not a function”. Please suggest.
558. Kidino - 26th Feb 2009 - 11:39 pm
Hey, this thing rocks! I got it working, multiple images on a single page. I even got into the JQuery Thickbox in the WP’s admin. WP has JQuery built it. The only problem is that when we scroll the page down and then display the thickbox, well, the cropping doesn’t work too well. Was trying out your position Absolute example—no luck yet.
Some other things that I am thinking about are this…
Can I make any of the sides fixed to specific location? For example, the bottom cropper fixed to the bottom of the page.
Can I make one side of the cropper matches the other side on the opposite direction? Like if I pull the right side 10 pixels to the right, the left side follows 10 pixels to the left.
Actually I want to make a fixed centered crop area, fixed to the bottom of the page. Using it to allow WP users define their own content area and sizes.
And I’ve been reading a few comments about multiple croppers in an image. Can you point me out to the right direction?
557. Dave - 26th Feb 2009 - 4:05 pm
Kidino:
I have tried with a couple of images on one page before (a long time ago) and it worked as expected, however performance was not so great so this was never a recommended approach. Have you got it working on multiple images or had issues?
556. Kidino - 26th Feb 2009 - 3:50 pm
This is awesome. I am implementing it in my WordPress theme panel. But I am facing problem with putting two or more image on the same page. Anybody can direct me to the right direction for this… Implementing the cropper on two or more images in one page.
555. anju - 10th Feb 2009 - 5:42 am
How could i save cropped image by JavaScript Image Cropper UI, using Prototype & script.aculo.us.
554. Wade - 10th Feb 2009 - 3:49 am
Cool utility, but you should probably mention in the Usage section that the cropper.css is required for this to work. The sample HTML pages don’t refer to that file, and there’s no reference to it in the instructions either!
I finally figured it out by making my server output an identical file as the sample, and mine still didn’t work so then I knew I must be missing a file somehow.
553. Dave - 7th Feb 2009 - 5:50 pm
Jim:
Yeah I need to remove the pledgie campaign as that has now finished and post about that and v2 but I haven’t had chance yet (been super busy since October last year). If you still wish to donate there is a PayPal donation link in the next steps section above the comments.
Glad I was able to help you.
552. Jim Mead - 7th Feb 2009 - 5:46 pm
Thanks Dave, that works great
I would donate to version 2, but Pledgie is saying “Campaign has ended”
551. Dave - 7th Feb 2009 - 4:13 pm
Jim:
You’ll have to physically remove the cropper instance in the CropImageManager, so make the following change:
550. Jim Mead - 6th Feb 2009 - 2:03 pm
Hi Dave,
I had disabled the validation temporarily to show my boss how the cropper and resize work.
If you submit the form without changing any of the options below the reset button it should bring you back to the cropper.
Thanks for your help.
549. Dave - 6th Feb 2009 - 1:22 pm
Jim:
I took a look at your example but I think I may be missing something as I can’t get it to submit the form and bring me back to the cropper. I’ll be happy to help if you can give me steps on your example to replicate your issue.
548. westworld - 5th Feb 2009 - 7:57 am
Could anyone point me to some sites that use Cropper UI plz?
547. Jim Mead - 3rd Feb 2009 - 11:44 pm
Hi,
I am trying to maintain the crop area on submission so that if a user submits the form without entering all the required information they wont have to re-select the area.
I have managed to get that working OK, however, I would like the reset button to fully reset, rather than reverting back to the onloadCoords.
If you view the source you will see that I have tried removing the cropper, and re-initiating it without the onloadCoords. But it is still resetting to the coordinates that were used when the page was loaded.
To be honest JavaScript is not my strong point, and I have only been playing with this page for a few hours, so it is probably something fairly simple that I have overlooked.
546. Chris - 20th Jan 2009 - 2:39 pm
541 – Lucas,
If you know how to program, it is possible to do. If you are looking for kiddie-scripts, you will probably not find any for this.
545 – Andrew,
I think donations to the software help encourage updates due to browser updates/changes. :)
545. Andrew - 20th Jan 2009 - 10:40 am
Really like this cropper, but it doesn’t work on IE8 (mode: IE8; document mode: IE8). Do you have plans to support it and when? Thanks!
544. Colin M - 20th Jan 2009 - 1:46 am
Hi,
Can not create a popup window without the imagearea/cropper flashing white and then disappering.
Has anyone else had this problem and solved it or know of a fix?
543. Dave - 19th Jan 2009 - 6:54 pm
Mustapha:
displayOnInit only applies when using ratios as I assumed that using the ratio as the starting size was the best way to do that. I believe that was before I added minWidth & minHeight handling. To be honest you are correct that it should be an available option with both, I’ve added it to the feature request list.
542. Mustapha F. - 19th Jan 2009 - 5:35 pm
can anyone say why displayOnInit: true doesn’t work with minWidth and minHeight? It works only with ratio.
thanks for help.
541. Lucas H. - 14th Jan 2009 - 10:24 pm
Has anyone done a classic ASP implementation for this excellent UI yet? Is such a thing even possible? All my research has led to dead ends.
Also – comment 364 in this thread appears to break the page in IE6.
Leave a comment
No HTML please, only textile. For code please use [lang]...[/lang] tags (e.g. [html]...[/html] for HTML)