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 … 28 27 26 25 24 [23] 22 21 20 19 18 … 1 » Show All
460. Gökhan PUR - 29th Mar 2008 - 10:20 pm
thnx
459. Daniels - 28th Mar 2008 - 1:13 pm
If you want to crop and resize from image.jpg to obtain a 70px X 30pxthumb in php:
$tt1=”path_to_image/image.jpg”;
$tt2=”path_to_image/image70X30.jpg”;
$tama = getimagesize($tt1);
$wi = $tama0 ;
$he = $tama1 ;
$image_p = imagecreatetruecolor(70, 30);
$image = imagecreatefromjpeg($tt1);
imagecopyresized ( $image_p , $image , 0 , 0, $_POST[‘x1’] , $_POST[‘y1’] , 70, 30 , $wi , $he );
imagejpeg($image_p, $tt2);
imagedestroy($image_p);
imagedestroy($image);
458. Daniels - 28th Mar 2008 - 1:07 pm
I didn’t know that was enough to send the x1 data. I was asking for the input value.
457. Daniels - 27th Mar 2008 - 7:54 pm
From POST 130.
DJ, just put form tags around those input fields from the cropper and put a submit button so that you can send them to the PHP page. After you get that working you can set them to hidden or whatever…
Sorry my ignorance, coul you be more specific please? How to put form tags on those vars. Thank you
456. Dave - 26th Mar 2008 - 10:05 am
Akhil:
The endCrop is meant to be called on any mouse up event as this is the only way to ensure that a user can select right to the very edge of the image without them having to hold their mouse over the very last pixel while letting go of the mouse.
I think your problem is that it is actually calling your callback every time the mouse is clicked, that’s the bug that needs fixing (it should really only fire your custom callback if we are currently drawing/changing the cropper), I’ve added this as a bug.
455. Akhil Bansal - 26th Mar 2008 - 8:43 am
Hi,
This is really a good lib. I gave this a try. I found a problem which is because of line 507 which says:
Event.observe( document, ‘mouseup’, this.endCropBind );
Due to this callback function called each mouseup event, no matter where it occurred. Means click any portion of page this callback will be execute.
Instead you can replace this line with:
Event.observe( this.imgWrap, ‘mouseup’, this.endCropBind );
It works fine for me.
Akhil
454. Jonny Muir - 20th Mar 2008 - 3:43 pm
To Sune Kjaergaard (regarding post – 1st Sep 2006).
Apologies if this has already been answered, there are lots of posts in this thread!
I recently came across the cropper transparency not working correct in ie6 when it was placed in a table (making it look like the image dissapears). I have found a fix as follows:
In cropper.cs
Remove the addition of the click area in the onLoad function.
i.e. the line:
this.dragArea.appendChild( Builder.node( ‘div’, { ‘class’: cNamePrefix + ‘clickArea’ } ) );
Then add it back after the imgWrap has been resized in the setParams functions:
i.e.
$( this.imgWrap ).setStyle( { ‘width’: this.imgW + ‘px’, ‘height’: this.imgH + ‘px’} ); // Existing line – set the size
var cNamePrefix = ‘imgCrop_’; // new inserted line
this.dragArea.appendChild( Builder.node( ‘div’, { ‘class’: cNamePrefix + ‘clickArea’ } )); // new inserted line
I can only assume this is a bug with ie6 as there is no reason why it shouldn’t work the original way.
453. prabhat singh - 19th Mar 2008 - 6:45 pm
Hi
I want to pass COORDS to asp.net to crop an image.
Please suggest for how to do it.
prabhat {at) gmail (dot} com
452. Dave - 18th Mar 2008 - 5:08 pm
Jimmy:
To me it sounds as though customizing the dynamic image example would be easiest, I’ve emailed you directly so we don’t fill up the comments discussing this.
451. Jimmy - 18th Mar 2008 - 5:05 pm
Also, the problem with using the CropImageManager object is that I also need to pass different “onloadCoords” and “ratioDim” values each time as well as the different imageID.
450. Jimmy - 18th Mar 2008 - 4:54 pm
Hi, I appreciate your help. I’m really just calling each one with this:
“new Cropper.Img(theImageID, { onEndCrop: onEndCrop });”
I’m then removing a DOM node that contains my image and all the new nodes created by Cropper, and then recreating the node and the various bare images in it, and applying the above line for each image ID.
I have changed the onEndCrop function in your cropper.js to return the ID its referring to (in addition to the coordinates/dimensions), so I can keep track of the co-ordinates of more than one image at a time…
I’d use the example if I could, I just can’t seem to find a way to get my varying imageIDs into the “new Cropper.Img” call in attachCropper…. I must seem silly but I feel I’m so close, and there are other people on this forum who also seem to long for a way to easily remove the cropper…. I see the “remove:function()” in “cropper.js”, if only I could pass my imageID to it!!
449. Dave - 18th Mar 2008 - 4:03 pm
Jimmy:
A link to an example of your implementation would be helpful – or details on how you’re removing/re-adding the cropper if you’re not using the same technique as in the dynamic image example.
448. Jimmy - 18th Mar 2008 - 3:41 pm
I almost have this wonderful tool doing magic… the only problem is that no matter what I do, I can’t seem to remove the onEndCrop co-ordinates/dimensions from images once they’re removed from the DOM. In other words, once I’ve loaded a second image called with an ID the same as the first (though the first is removed from the DOM) I get all the old dimensions followed by the new ones when onEndCrop is called; I think this is the same thing that is referred to in the comments in the script as ”#00015” (onEndCrop callback gets called multiple times when removed and then reapplied). Everything on my page is pretty AJAX-y, and I simply cannot get the my cropper to remove() itself. I’ve looked at the dynamic-image example but can’t grok it; my case is pretty different. Tips??? THANKS!
447. Paul - 9th Mar 2008 - 7:17 pm
If I get this doing what I need it to in my project, I’m donating..
Looks great.. thanks
446. Dave - 8th Mar 2008 - 10:02 am
*@Dave:*
If you pass both the min and max co-ords and set them both to the same values then the user won’t be able to resize (and the resize handles will not be displayed).
445. Dave - 8th Mar 2008 - 12:27 am
Hi i would like know if it’s possible disable the resize option. I need to pass coords on load, but without resize the crop. Is it possible?
Tks a lot! ;-)
444. Christophe - 26th Feb 2008 - 4:36 pm
Hi, very nicely done
But I have a question though. Do you keep track of all the area’s that get highlighted? I tried to use the script in combination with an ajax script that loads the image.
When the image is loaded, I call a method to initialize the events. I got that working. When I select an area and onEndCrop is loaded, I reload the image via the server (It’s a map where I try to zoom in).
But here it goes wrong. When I do it again, the script is called twice, with the coordinates from the first time and the ones from now.
I tried a lot, but could not find a solution.
Thanks anyway
443. Dave - 21st Feb 2008 - 11:23 pm
*@Brent:*
I’m glad you liked it. There are plans to do a jQuery (and other frameworks) version (see the V2 plans linked to on the post), but I’m totally snowed under with other projects at the moment.
442. Brent O'Connor - 21st Feb 2008 - 9:06 pm
Awesome job! This is exactly what I was looking for. I only wish you had a jQuery version.
441. StAT - 19th Feb 2008 - 6:08 pm
And there was little problem with absolutly white image. After refresh of the page everything is ok. But with new image for crop the same picture. Fixed with:
.imgCrop_handle {
position: absolute;
border: 1px solid #333;
width: 6px;
height: 6px;
background: URL(tr.gif) fixed;
opacity: 0.5;
filter:alpha(opacity=50);
z-index: 4;
}
I don’t know. May be this problems mine only.
FF and Opera work ok.
Leave a comment
No HTML please, only textile. For code please use [lang]...[/lang] tags (e.g. [html]...[/html] for HTML)