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 Link: 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.
1. Kiddi - 22nd May 2006 - 12:17 pm
WOW, thanks! Been looking for a free version of a online cropper that looks nice. Kudos!
2. chris - 22nd May 2006 - 10:02 pm
GREAT!!
looks very good :D
add a slider (timeline) with some markers, and you have a great tool for a JS-driven kenburns-creator :)
3. chris - 24th May 2006 - 12:34 pm
one question about the script:
is it possible to redraw the crop-box from outside?
i tried to call “setAreaCoords, cloneCoords, drawArea”, but no success…
(working on the timeline-”project” see above)
chris
4. Dave - 24th May 2006 - 1:45 pm
Chris:
I saw your original comment on the post yesterday and did a bit of research into the kenburns effect and thought that I could knock it up quite quickly (when I had some time), but didn’t have chance to reply to your post.
Although I designate these methods as being private you should be able to do:
myCropper = new Croper.Img( ...arguments... ); myCropper.setAreaCoords( { x1: 0, y1: 0, x2: 50, y2: 50 } ); myCropper.drawArea();The
method simply returns a clone of a coords object which has the
properties to allow you to use process same coordinates as another variable by value rather than by reference.
Hope that helps, if you have any more questions don’t hesitate to ask.
5. 1 Pixel Out » Blog Archive » Need a JavaScript image cropper UI? - 25th May 2006 - 3:58 pm
[...] This is one tool to bookmark if you’re thinking of building an image cropper into one of your apps. It’s built with the Prototype and script.aculo.us frameworks (which I strongly recommend by the way), it has plenty of configuration options and it feels very slick. I love how the UI is an exact implementation of what you get in Photoshop or any other commercial graphics package. Check the demo at Dave Spurr’s blog or read more about it on his doc page. [...]
6. kiwo - 31st May 2006 - 7:35 am
The cropper is great! Thanks.
There is only one thing that I can’t figure out. I need to change image’s src attribute using javascript. And when image changes, i need to setup cropper for new image with its new size and contents. How can i disable an existing cropper to be able to create a new one for a new image?
TIA, kiwo
7. Dave - 31st May 2006 - 7:28 pm
Kiwo:
Glad that you like the cropper. I’ve just spent a few minutes playing around with trying to get the image cropper to change the image dynamically; and I have to say that, currently, it’s not possible.
It is possible to apply a new cropper to a new image (or the same image but replaced) but there are a couple of errors that are related to the fact that the HTML that was added to the DOM for the cropper isn’t been removed.
I would have to work this in as a feature, but I’d like to know if this is feature that people would like to see added: to dynamically change the image within the cropper without reloading the page.
8. Stoyan - 2nd Jun 2006 - 9:22 am
but I’d like to know if this is feature that people would like to see added: to dynamically change the image within the cropper without reloading the page.
Yes, it would be nice to add a ‘zoom’ feature.
9. Stoyan - 2nd Jun 2006 - 9:52 am
It seems that area selection is not as smooth in IE as in Mozilla. IE recalculates something constantly and the ‘hour-glass’ appears at dragging.
10. Nick - 2nd Jun 2006 - 12:37 pm
Just great !!!!! That’s going to be a killer app !! Unique interface and features, no such analog for JS on the web !!! I’ll integrate it in the CMS I’m just working on.
Hope Dave is going to continue the development – adding new features and removing bugs.
11. Dave - 2nd Jun 2006 - 1:37 pm
Stoyan:
Can you expand on what you mean by a ‘zoom’ feature?
I think I know what IE is doing (it’s actually less obvious in IE 5.5 than IE 6), the thing is on the day that I launched I changed the entire way that the dark overlay is generated to improve performance on Mac based browser (Camino especially). I’m currently looking at the features requested by kiwo so I will also fix the IE performance issues as I believe I know what is going on there.
Nick: Thanks for the kind words, it’s nice to know that people like the cropper.
12. Stoyan - 2nd Jun 2006 - 3:18 pm
Hello Dave !!!
‘Zoom’ is wrong, I meant to fit the selected area to the image area, as it were ‘crop to mask’ (photoshop). On some software the cropped image is resized to the original image size, so it looks it’s ‘zoomed’.
These days I’ll intergate this nice app of yours with phpthumb, I could host a demo if someone wishes.
13. Nick - 3rd Jun 2006 - 11:11 am
1.1.0 – More tha good News. I’ll add a link to this site everywhere I use this tool from now on.
14. Nick - 3rd Jun 2006 - 11:16 am
Remove/reset cropper didn’t work on Firefox for me.
On IE – click ‘remove cropper’ and then ‘reset cropper’. The drag the select area – the crop values (co-ordinates) do not refresh.
15. Dave - 3rd Jun 2006 - 12:54 pm
Nick:
Thanks for pointing out the IE issues with the callback, version 1.1.1 that I’ve just uploaded should fix that problem.
As for your issues in Firefox, I’ve had no problems. Maybe it was a caching issue with the JavaScript, I’ve added something to the demo that should fix that if it was the case (or just do a hard refresh). But please tell me if you’re still having problems and whether it is within the demo or the test that comes with the download.
16. Nick - 3rd Jun 2006 - 1:33 pm
All works great, thanks for the quick fix. I’ll report the bugs if I find any.
17. Nick - 3rd Jun 2006 - 1:48 pm
I would like to repeat for all those who read this post- I have been searching for crop tool for months, this is the best solution, really the finest one ever- it works in Mozilla and IE ( the most used browsers) and it can be easilly integrated. There is only one other such feature-rich and bugfree tool, but it’s in Java and it is not free.
18. Andy Skelton - 8th Jun 2006 - 10:25 pm
Howdy and thanks for the great tool! I am struggling with the ratios (when GCD is too low it breaks completely) and about to work up a patch.
Dave, if you already have something in the works please let me know.
19. Andy Skelton - 8th Jun 2006 - 11:12 pm
I have a modified version of applyRatioToAxis that handles ratios more smoothly, without the jumping effect I was seeing, regardless of their common divisors. It alleviates the need for GCD calculation. Hopefully the resultant ratio is within an acceptable margin of error. Now we can push this feature out on WordPress.com! ;-) Thanks again for the great work.
20. Mike - 9th Jun 2006 - 2:31 am
“I’d like to know if this is feature that people would like to see added: to dynamically change the image within the cropper without reloading the page.”
Yes please! I’m working on a PHP web app with a form where a user uploads several jpegs, and I want to immediately let the user crop them to a 150×150 square – before submitting the final form.
What I’ve done (tell me if I’m doing this all wrong) is made a page with several input type=’file’ form elements, hooked up the submit button to submit to an invisible iframe that moves the file into place after upload, updates the src=’’ of the image tag, then tells the parent document to display the div containing the cropper. Thus, you upload, crop, save, and move on to the next image, all on the same page.
Anyway, to do this I need to be able to change the img src=’’ of the cropper on the fly.
21. Mike Burrell - 12th Jun 2006 - 9:40 am
First off, great script!!
I setup your cropper GUI onto my dev site, and am passing the x1, y1, x2, y2, width, height vars to hidden form fields which are then passed to my PHP script below when the form is submitted.
The final photo is cropped, however I am obviously off somewhere as the cropped image isn’t exactly correct. (It appears slightly smooshed and is off a few pixels possibly.)
Any ideas on what I am doing wrong here would be greatly appreciated!!
My PHP script below:
22. Francis - 12th Jun 2006 - 4:22 pm
Do you have an idea why do I can’t see the image
(sorry for my bad english)
Thx
23. Francis - 12th Jun 2006 - 4:25 pm
Can you tell me why I can’t see the image?? It was working well before…
http://eleves_tim.cmaisonneuve.qc.ca/e0368657/help.jpg
24. Andrew Okonetchnikov - 16th Jun 2006 - 1:33 pm
Great work! Thanks!
Take a look also on my JS “ModalBox” component here: http://okonet.ru/projects/modalbox/
Maybe you’ll have some suggestions too. Thanks again!
25. Jake - 19th Jun 2006 - 5:10 pm
This is the coolest script on the web since…, well, probably ever! It works so smoothly and beautifully and has saved me hours and hours of time.
Thought you should know, the cursor for the western handle is set to “e-resize” when you probably want “w-resize”.
26. Georgi Kostov - 20th Jun 2006 - 6:22 am
Very good code. I’m really impressed. I need to use it and I’m concerned about the license for using the code as it will probably go into a commercial app.
27. Aron - 27th Jun 2006 - 2:23 pm
Has anyone made a ASP or C# script that actually crops the image?
I see the PHP script above, I may just make the .net version.
28. Dave - 29th Jun 2006 - 8:28 pm
All:
Sorry for the late response to all of your comments, I’ve been away on holiday and have only just recently returned.
Mike:
I’ve not got around to implementing the server side part of the cropper yet, but when I do I’ll make them (CF & PHP) available here. Looking at your script I can’t see any problems, can you email me more details & some example screen shots/code.
Francis:
Do you have more information, browser version etc. or a live example URL, also what do you mean by it was working well before, is this since an upgrade to a newer version of the cropper UI?
Jake:
Thanks for the kind words on the cropper… I may just use that as a pull out quote one day. Also thanks for the heads up on the resize handle cursor problem, I’ve amended it and it will be in the next release.
Georgi:
The BSD License allows proprietary commercial use, and for the software released under the license to be incorporated into proprietary commercial products, but the copyright must be retained and all conditions met. If you do use it in a commercial piece of software that is successful to some degree then it might be good karma to leave an appropriate donation.
Either way keep in touch and if you could give me a heads up with what you’re using it in that’d be great, it’s nice to see your work out in the wild.
Andrew:
Again, thanks for the kind words on the cropper. I had a quick peek at the ModalBox demo on your page and it looks pretty sweet, good luck with it, I also quite liked the design of the pages – although it took me a few seconds to find the demo link.
Aron:
As I said above I haven’t got around to implementing the server side parts of the cropper I had planned to completment the UI, but when I do I will only be implementing them in CF & PHP. If you create a .Net version of the server side part and wish to release it then I’ll link to it from here if you wish.
Phew, that was quite a bit to catch up on… now to sort out my email…
29. John - 2nd Jul 2006 - 12:02 pm
Nice work!
well, if you could add two buttons to zoom in/out background image, it would be perfect!
30. Robert - 3rd Jul 2006 - 10:38 pm
Great work. I wonder how did you manage to do this in 1 and 1/2 weeks…
Here is my problem: I am trying to implement your cropper in C# .NET. My image (to be cropped) is inside Panel object (which is actually DIV element 400×400px) and if image is bigger then DIV element then I have scroll bars. If I move these scrollbars then drawing of the crop area doesn’t work properly. And error is actually the offset for which I moved the scroll bar. I guess I need to compensate for this movement. Where in your code I need to add these values: vertical and horizontal offset? Is there quick solution to my problem?
Second question is… what is the minimum required for cropper UI to work. Are all those JS files necessary. I am asking because I deleted some of them and everythig was still working fine. (I only need basic cropping functionality)
Thanks.
31. Robert - 5th Jul 2006 - 3:07 pm
OK, I did some reading on script.aculo.us. Please, disregard my second question. :-)
32. Stoyan - 6th Jul 2006 - 5:42 pm
Press the ‘Remove Cropper’ buton twice on IE and the image dissapears !
I want also to reset the coordinates on ‘Cropper Remove’ how to do that ?
33. Stoyan - 8th Jul 2006 - 5:56 am
Is it possible to define default selected area (wich appears oninit), without using area ratio ?
34. nation-x - 10th Jul 2006 - 3:41 pm
I looked at the cropper.js until my head hurt. :) I need to know how to do this… Let’s say I have an image that is 1000×1100… what I would like to do is set the width attribute of the testImage to 500px and crop by aspect ratio with a minimum width and height of 120px. How can I accomplsh this using this cropper? Can I get some help with this? I would greatly appreciate it.
35. bala - 10th Jul 2006 - 4:04 pm
Image Cropper features are good, but i need dynamic feature to upload and crop and update the server image with crop image. How I will do this? Guide me
36. Uberdoer - 10th Jul 2006 - 5:59 pm
Stunning. Well done Dave. You’ll be getting a donation from me for sure!
A few more examples would be good, including a bit on resetting and removing a previous crop region.
37. Dave - 10th Jul 2006 - 9:20 pm
All:
Thanks for your continued kind comments. I’ve also started up a private bug/feature request list for the cropper due to the huge ammount of interest in it, these I will assign priority as I deem fit or based on demand.
John:
I can kind of see the point in having a zoom function, but I can’t see it being worth the overhead to the script at the moment. Maybe if I get more requests in the same vein.
Robert:
“Great work. I wonder how did you manage to do this in 1 and 1/2 weeks…” Erm dedication and doing little else other than this in the majority of my spare time, I wouldn’t like to count the hours I spent on it, but if I were to put standard 9-5 working days against it I’d say 3 or 4 days max. So it’s not that bad, it’s always easier when you get into a groove with something like this.
As for your first question, the cropper doesn’t take scroll offsets into account, you should be able to tweak it quite easily to account for those. I have added this to the feature request list.
Stoyan:
Thanks for the heads up on the ‘remove cropper’ bug, and the feature request, which I’ve added to my list.
Nation-x:
I’m sorry the JavaScript made your head hurt, I feel the uncompressed one is fairly heavily commented and should give good pointers as to what does what. As for your feature request I’m not quite sure what you require, is it that you have a maximum area for the cropper UI (500×500) and any image larger than this would need to be resized? This will make cropping a little different as you’d have to work out the ratio of the cropper to the resized image, but it is do-able. If you clarify your requirements I may add it to the feature request list.
Bala:
At the moment this is just the JavaScript UI, I do have plans to develop the server-side part of a cropping tool & release this, but I’m not sure when I can squeeze that in.
Uberdoer:
Thanks for your very kind comment. I don’t know how much further I can take the examples – I have multiple live ones and the download comes with 6 different examples, or would you just like to see a couple of tutorial like examples?
38. nation-x - 11th Jul 2006 - 1:13 am
Sorry… I wasn’t trying to say anything bad about the javascript. :) It’s awesome… I just haven’t been working with javascript for awhile.
What I asking about is using aspect ratio for the cropper when the cropper UI has a maximum width of 500px (as an example) but the image is 1000px. handling the thumbnail isn’t an issue. If I am using a minimum width and height (say 120×120) then when I crop the image I can just crop the selected area and then resize the thumb to 120×120. What I noticed is that the preview window is actually showing the correct representation of what I have selected but the cropper UI is showing the large image in the selection. I suppose I could resize the image first before it’s displayed in the cropper UI, but I was hoping that it could just be done without resizing. I think the key is to use the image from the preview area inside of the selection but I am a PHP developer and haven’t worked with javascript in years… and your javascript isn’t simple… it would take me a month to know it intimately enough to modify it to do what I need… lol.
On the other side of that… I am more then willing to chip in the PHP to crop the images. :) I have a sweet class I put together from a couple of different sources. You can see it here. http://php.amnuts.com/forums/viewtopic.php?t=253
39. Bane - 14th Jul 2006 - 9:58 am
This is really great and usefull tool!
I have just one question, if you set minimum height and width than crop area is constrained to that ratio. Is it possible to set both minimum height and width without having constrained crop area?
40. Adam Roth - 17th Jul 2006 - 4:25 am
I tried to call cropper.setAreaCoords(...), and cropper.drawArea(...) as mentioned above, but I end up getting $( this.overlay ) has no properties. Any ideas? I need to draw the cropping box based on previously saved cropping coordinates.
41. Adam Roth - 17th Jul 2006 - 4:28 am
...or better yet, can I pass in starting coordinates for the cropping box when creating the cropping obj?
42. Neil - 18th Jul 2006 - 2:51 pm
I’ve taken the example and thrown a wrapper div around it, gave it a width, relative position and margins and the selector goes a bit wacky.
http://www.susan-deaux.com/z/cropper/tests/example-Preview.htm
43. Neil - 18th Jul 2006 - 3:00 pm
After playing a bit more it seems that the wrapper’s position: relative is giving the cropper the most problems.
44. Dave - 19th Jul 2006 - 8:05 pm
Nation-x:
Thanks, that script looks quite good, some of it I’ve seen before and am already using in some areas. Your other request has been added to the feature/bug list.
Bane:
I’ve added the ability to set minimum dimensions without applying a ratio constraint to the feature request list.
Adam:
That error sounds like you are trying to call the methods before the cropper has been attached. It should work for you once it’s attached, I’ve added your request of being able to set cropper dimensions & position on init to the feature request list.
Neil:
The wrapper needs to have a relative position as the select area etc. are all then positioned absolutely within that container, I’ve added extra testing of things like placing other wrappers around the cropper with different positioning, margins & padding etc. the bug list.
45. Reynold - 24th Jul 2006 - 3:54 am
Hey this is a great script. Just wondering when the image gets so large (say above 2000), it would be nice if I could instead crop a zoomed version of the image. I just tried with this cropper and , however, it doesn’t seem like the zoomed one. Instead, the script displays the original size image in the cropper window.
46. Dave - 24th Jul 2006 - 7:24 pm
Just a quick note to say that I’ve added a link to the list of feature requests & bugs for the image cropper. These are all areas I intend to address at some point, note they are not in priority order.
47. bala - 25th Jul 2006 - 8:26 pm
Can you tell me how to upload that cropped image into the server in ASP?
48. Skylog » Blog Archive » links for 2006-07-27 - 27th Jul 2006 - 6:28 am
[...] JavaScript Image Cropper UI, using Prototype & script.aculo.us (tags: javascript) [...]
49. aNieto2K - » Prototype JavaScript Image Cropper UI - 29th Jul 2006 - 3:19 pm
[...] Se trata solo de una demo, pero no deja a nadie indiferente el poder de prototype y lo que podemos llegar a hacer usando una librería tan completa como pesada como script.aculo.us. Sin duda las aplicaciones web están cambiando y cada día más y pasos más agigantados. Esto es una muestra de ello. [...]
50. Adam Roth - 31st Jul 2006 - 9:10 pm
I’d like to set another callback when I double click on the cropping area (which would then make the AJAX call to the server). Does the cropper currently support this functionality? Might it?
51. soi57 » Blog Archive » JavaScript Image Cropper UI - 1st Aug 2006 - 5:12 pm
[...] http://www.defusion.org.uk/code/javascript-image-cropper-ui-using-prototype-scriptaculous/ [...]
52. Adam Roth - 2nd Aug 2006 - 11:13 pm
New issue…
I’m using the cropper in a lightbox-style POP-up (absolutely positioned). If the page is scrolled (that is, the veritcal scrollbar is not at the top) then the crop selection is off by the height of the scroll.
I’m getting around this by scrolling the window back to 0,0 when the lightbox is displayed, and then back to the correct position after the crop. It’d be cool if the cropper could be aware of this, however.
53. Jason - 3rd Aug 2006 - 4:35 am
I’m still having trouble with IE’s callback… after the first crop the coordinates are always 0. I’ve attempted to do a cropper.remove(); cropper.reset(); in 100 different orders and then re-initializing a cropper object on demand… with no success. How does your IE fix work??
54. 1 Pixel Out » Blog Archive » Dave Spurr interview on WebWire - 4th Aug 2006 - 12:13 pm
[...] Ben Forta posted a link to an interview with Dave Spurr today. I know Dave quite well and even linked to some excellent work of his a few months ago. The first reason I’m blogging about this is that I’m quite impressed that his name showed up on Forta’s blog but also to point out that the post on WebWire doesn’t include a link to his excellent blog. [...]
55. Chris - 4th Aug 2006 - 1:36 pm
On the dynamically changable image demo the layout isn’t udpated between image selections.
56. Nate - 4th Aug 2006 - 1:36 pm
This is an excellent script, exactly what I was looking for.
Here is an ASP function I just wrote that works with this. Grabbing the variables is handled elsewhere, but should be easy enough to figure out.
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
// img is defined earlier in the Picture class
// I created, and is just a System.Drawing.Image
// x/y1 and width/height are the same as in
// cropper resWidth/Height are the resulting
// image dimensions, so I can resize the crop too.
// You probably just want to remove the Picture
// types and just use Image or Bitmap types
public Picture crop(int x1, int y1, int resWidth, int resHeight, int width, int height)
{
Picture res;
Bitmap bm = new Bitmap(resWidth, resHeight, PixelFormat.Format24bppRgb);
bm.SetResolution(img.HorizontalResolution, img.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bm);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(img,
new Rectangle(0, 0, resWidth, resHeight),
new Rectangle(x1, y1, width, height),
GraphicsUnit.Pixel);
grPhoto.Dispose();
res = new Picture((System.Drawing.Image)bm);
return res;
}
57. Ash - 10th Aug 2006 - 10:25 pm
Hi! Firstly I’d like to say what an absolutely fantastic piece of coding. You truly are amazing at what you do!
Could someone point me in the right direction about what to research to learn how to get this to save the cropped image on my server after hitting a crop button? I don’t really have the first idea, but i’m pretty good at learning if someone can tell me where to look for a starting point. Although at the same time the easiest and fastest method would be better as if someone has already made a cropping script then theres no point in me recreating it (and badly as i don’t really know what i’m doing).
Am i right in guessing that i can feed in the ingoing_filename, ingoing_width, ingoing_height, crop_topleft, crop_botright, outgoing_width, outgoing_height, outgoing_filename to some kind of PHP script (that possibly uses GD library or something) to save the cropped version of the picture. I know i’m probably insulting all of you guys intelligence but am i along the right lines at least?
Any help is greatly appreciated, but i’m not looking for someone to hold my hand. And if you guys are all too busy then thats no problem – i’ll completely understand and i’m sorry for wasting your time reading this.
Oh keep up the good work Dave. If i end up using this for any of my projects at all then i will definately donate.
Cheers
58. Matthew - 12th Aug 2006 - 10:24 pm
If you use CakePHP you will notice that including this in your script will break the CSS layout. This is due to the CSS rule
form div{
vertical-align: text-top;
margin-left: 1em;
margin-bottom:2em;
overflow: auto;
}
A simple workaround is to add another rule directly after this like so:
form div.no_cake, form div.no_cake div {
margin:0;
overflow:hidden;
}
and then in your code surround the img tag with a div with the class name of no_cake.
Cheers
59. Uberdoer - 13th Aug 2006 - 9:12 am
Finally got around to sending that paypal payment. Well done on a very good bit of coding.
I have to say that your cropperui has added panache to an almost completed www.paint-that.com. Thanks!
60. Dave - 14th Aug 2006 - 7:07 pm
Adam:
For your first question “I’d like to set another callback when I double click on the cropping area”, I don’t see that as being functionality I plan to add. As for the lightbox style presentation of the cropper my guess is that it relates to the issues the cropper currently has when within differently positioned wrapper elements, which is on the list of bugs.
Jason:
I’m having trouble trying to replicate your problem, everything is working fine for me in IE with the callback. Can you provide a test case to me via email that demonstrates the problem.
Nate:
Thanks for the ASP code that integrates with the cropper, do you have a link to this code that I can use in the code details. If not am I OK to package it myself and release it on this page?
Ash:
Thanks for the kind words. I haven’t taken much of a look at this script, or tried it in conjunction with the cropper UI, but you should be able to use this to crop the image server side in PHP.
Matthew:
Those styles are only part of the test cases & examples, all the class names used for the cropper itself are isolated with the imgCrop_ prefix.
Uberdoer:
Thanks a lot for your kind words and very kind donation, it is my second so far and is great. Thanks again.
61. Lucas Young - 15th Aug 2006 - 1:28 am
Hi! What a great tool!
Is it possible to have more than one cropper on a page?
cheers
Lucas
62. Ashen-Shugar - 15th Aug 2006 - 6:51 am
Hello
It is super brilliant!
I would like to know if it possible to have fixed size or fixed a maxheight and/or maxwidth.
Thank you
63. Nate - 15th Aug 2006 - 12:55 pm
Dave:
You can do whatever you want with the ASP code. I don’t have a public site, I’m using it on an intranet site.
64. Nathan Meurrens - 16th Aug 2006 - 7:11 pm
This is undoubtedly a really great script.
I was already using my own cropper but it is nothing compare to this one. I’ll definitely switch.
Thank you, Dave, for this work.
Anyway there is something I’ve miss…
Why are we passing through the EndCrop function at any click on the document? It may seems obvious for you but for me it isn’t. Actually I even don’t understand how does it happen and when is this function called.
Thanks again.
nathan
65. mp - 17th Aug 2006 - 1:48 pm
Honestly, this is truely awesome! I am sooo jealous of your JS abilities. I thought about writing a simple JS cropper for an upcoming project (it would have been no where near the level of this) but I decided to take a look at what was out there first. I’m sure glad I did, because you are a genious.
66. Jim - 21st Aug 2006 - 12:42 am
I work for a university and was designing a new administration with an image upload section in which they would select the most interesting area to crop out for thumbnails. I looked around the net for a good crop tool and did not find what I was looking for, so I threw together my own. I just recently stumbled across this one and was absolutely amazed! I will definitely throw out my horrible code and use this one! Thanks Dave!
67. Thomas Kenne - 21st Aug 2006 - 9:24 am
Hi,
on you feature list I can see that 0008 is checked. Does that mean that it is already implemented – and if so – how do I use it?
Thanks in advance
68. Dave - 21st Aug 2006 - 10:52 am
Jim:
I’m glad that you found my Cropper UI useful.
Thomas:
The feature will be in the next release, I did want to try and tackle some of the other items on the list but haven’t had time. I’ll build the next release this evening and make it available.
69. IGOR - 21st Aug 2006 - 2:27 pm
This is a really great script.
But how to save a cropped picture? Or just in Demo ist is not possible?
And i would like to know if it possible to have fixed cropped picture size?
Thanks!
70. James Walker - 21st Aug 2006 - 9:35 pm
Firstly I have to say what a really neat bit of coding – very cool! I am having one prob though – I’m trying to change the image dynamically using ImgWithPreview but can’t seem to get it to work, despite whether i use remove(), reset() or setParams() – it either displays the last image or the current one distorted or a mixture of both! Yet it works fine with the standard Img object – the code i’ve found to use for this is:
if (curCrop != null)
{
curCrop.remove();
$(‘upimg’).src = tempFileName;
curCrop.setParams();
}
else
{
$(‘upimg’).src = tempFileName;
curCrop = new Cropper.Img(
‘upimg’,
{previewWrap: ‘previewWrap’,minWidth:96, minHeight:96, displayOnInit:1, ratioDim: { x: 1, y:1 }, onEndCrop: onEndCrop }
);
}
Any ideas?
Cheers
James
71. Tj - 21st Aug 2006 - 10:16 pm
How are you supposed to crop the image to save it? I’ve looked around and couldn’t find anything… Thanks!
72. James Walker - 22nd Aug 2006 - 1:14 pm
TJ:
You’ll need to pass back the values to a server-side form and use something such as .NET, PHP etc to do the cropping…
73. Dave - 24th Aug 2006 - 7:28 pm
James:
Thanks for the kind comments on the Cropper and also thanks for stepping in with the reply to Tj’s question.
As for changing images dynamically with the ImgWithPreview class, I purposely didn’t add this feature when I added it to the main class as I wasn’t sure that the ImgWithPreview class would need it. Plus it would add a bit more overhead to both the classes as I’d have to create the hooks that call methods on the sub classes and also add all the logic for adding & removing the preview area.
I suppose it is a possibility so I’ve added it to the feature request list.
74. Alex - 25th Aug 2006 - 4:01 am
First of all – you are the man!
This js is exactly what I’ve been looking for. Very very nice work and very generous of you to give it away. If I ever profit from it I will be sure to donate to your paypal.
Its working perfectly for me in IE, However in Firefox I’m noticing some interesting behavior – it seems that if I have an image thats actually 1600×1200 but I have displayed it onscreen as 400×300 the crop area shows the a ‘zoomed in’ area – i.e. if I am cropping x1:100, x2:300, y1:90, y2:210 with a width of 200 and a height of 120 the crop window shows the capture from the area of the original image. Its not scaling the coordinates. Does this make sense? I’m only seeing this in Firefox.
I’m wondering if this behavior is expected and if so is there a setting I should look into? Anyone else seeing this?
Thanks again for the great script.
Alex
75. Julio Romano - 28th Aug 2006 - 7:36 pm
Congratulations!
This script is wonderful!
But i’m have a problem in IE.
I’m using the basic mode.
Sometimes, the image doesn’t appears. Then, i press F5, to refresh the page, and then, the image appears.
This problem doesn’t happen in Opera.
What is this?
Thanks
Julio Romano
76. Rue - 29th Aug 2006 - 10:49 am
Before I start, some more flattery; you’ve saved me quite a few hours’ work, thanks!
I am currently writing a control panel extension to enable colour channel changes and other image processing functions to be called on the preview image. I came accross a bit of a DOM problem in that you clone the image node for the preview pane. This only becomes a real problem when you try to access it via the DOM to make changes. Rather than just tell you about it I have fixed it for you and post a diff here for you to check/integrate/laugh at.
This is a diff against the latest version of cropper.uncompressed.js (internally labelled as “v. 1.1.0 – 2006-06-02”)
The backend I am using is RoR but if anyone is interested in the js code I am producing to do the calls to refresh the preview and/or any of the ruby, I could easily be pursuaded. ;)
77. miri - 31st Aug 2006 - 4:06 am
Thank you so much! This is just what I needed. It runs smoothly and looks wonderful in Firefox and IE6. Has it been tested in IE7? (When I install IE7, it somehow resets my internet connection and I can’t access the internet…which kind of defeats the purpose of the darn thing.)
78. Sune Kjaergaard - 31st Aug 2006 - 10:21 am
Hi Dave
First of all: what an excellent tool you’ve created. You’ve saved me loads of development time.
I’m integration your cropper into a CMS, but I have a bug, which as far as I can tell, only occurs in IE (ver 6.0.2900.2180). When the page with the cropper is first loaded, and I click inside the image to start selecting an area, the image disappears. I’m still able to select an area, but the image disappears beneath the overlaying div’s. If i reset the cropper, that is reinitializing it, the image is still gone. I am however able to load it in an object via javascript, meaning it exists somewhere in the DOM. Also I am able to see it in the DOM explorer of IE developer toolbar.
When I refresh the page the cropper will usually work without problems.
Any ideas as to where to start looking?
Regards
Sune
79. Dave - 31st Aug 2006 - 8:37 pm
All:
Again, thanks for the kind words, I can’t express that enough.
Alex:
The issue you raised is one of the known bugs, if you resize the image within the IMG tag then it won’t work properly in Firefox. The reason it works in IE is because IE implements the cropper in a different way to all other browsers, that is the original way which was far too slow when redrawing in Mac based browsers. I’ve looked at this problem and at the moment I’ve not come up with a fix for it. The only thing to do is resize the uploaded image to a maximum dimension for display with the cropper rather than resize with the IMG tag.
Sorry for the lack of a more robust solution at the moment.
Julio & Sune:
That sounds like it could be a bug with the CSS. it’s not something I’ve come across in IE in my testing, is the issue occuring when you implement the cropper inside some other elements positioned with CSS?
Rue:
I’m guessing you’ve got 1.1.2 or 1.1.3, I hadn’t noticed that the version number in the header of the uncompressed version hadn’t been changing, I’ve rectified that. I’ve also implemented your suggestion of making the ID unique on the previewImg by using the ‘imgCrop_’ prefix, this will be in the next release of the cropper, thanks for the heads up on that one.
Miri:
No it hasn’t been tested in IE7, I’m still waiting to get a standalone version of Beta 3 or the release candiate working reliably. However I just ran it quickly through my Beta 2 standalone version (which you should note is quite buggy in standalone mode) and the only problem I noticed were the marquees weren’t being resized properly. If anyone has the default installation of IE7 Beta 3 or Release Candidate (e.g. no registry hacks) then maybe they could verify it. But for now I can’t afford to lose the ability to test on IE6 for my other work.
80. Sune Kjaergaard - 1st Sep 2006 - 8:05 am
Hi again.
You’re probably right about the CSS bug. My initial suspicion was that the overlaying div’s lost their transparency. Having looked into the way the cropper is drawn in IE along with the fact that the image doesn’t reappear when resetting the cropper, tells me that this isn’t the problem.
The cropper is nested inside a div wich is again nested inside a td. Neither of those have any CSS positioning. Along with the cropper there is another div which is positioned with float:right, in the same parent div.
I’ll try to illustrate it:
[td]
[div]
[div style=”float:right;”][/div]
[div id=testWrap]the cropper[/div]
[/div]
[/td]
In case it’s an IE-CSS bug… Any ideas as for how to test for it and deal with it.
Regards
Sune
81. Dave - 1st Sep 2006 - 10:46 am
Sune:
As with most IE CSS bugs it may be a lot of trial and error, if you have a reliable test case that replicates the problem you could start by removing your custom CSS rules until the bug is fixed. If you have such a test case then you could send it through to me and I’ll try and have a look.
82. sebastian - 4th Sep 2006 - 2:20 pm
Is it somehow possible to create multiple previews? I have to crop a picture and want output a thumbnail of the cropped picture and the cropped picture itself.
Great tool!
83. miri - 4th Sep 2006 - 3:18 pm
Just an FYI – one of the people in my test group has IE7 installed. He said he had no problem with the Cropper UI. Yay!
84. Juan Pablo Ruiz - 5th Sep 2006 - 1:30 pm
Excelent script!
I just want to save the cropped file in the server. How can I do that??
Thanks
85. Lucas Young - 7th Sep 2006 - 4:41 am
Hi Dave
The cropper works great for me except when I stick it inside a DIV tag. I have a page where the content is inside a tag so the page doesnt extend too high and break. All the cropping is done inside this div. What happens in Firefox/Mac is that the selection area works fine except for the bottom right corner handle – when that’s clicked, the selection box jumps upwards and shrinks…
In IE the image being cropped extends way off the bottom of the div, behind some other page elements, and has the same bottom-right corner handle problem.
Could I do something to fix or prevent this?
many thanks
Lucas
86. Dan - 9th Sep 2006 - 3:11 am
Great, great and great!
Any news with regards to rendering of the selection image when zooming?
How does it handle multiple images on same page, all inline for cropping?
87. Dave - 10th Sep 2006 - 6:50 pm
Sebastian:
It is possible to create multiple previews, you will have to use the available preview class as a base and tweak it to your needs as this is not something I intend to add to the release.
Miri:
Great, thanks for that, I’ll add IE7 to the list of supported browsers.
Juan Pablo:
The cropper is just the UI for setting the crop co-ordinates to pass to your server-side language, this is where the crop will have to take place.
Lucas:
That must be due to the CSS settings on your containing DIV, are you applying any overflow rules?
Dan:
As for zooming no, not yet, I spent a while looking at it. But basically the way the cropper is attached, at the moment, in non-IE browsers (to address render speed issues in Mac browsers and improve performance in general) make zooming impossible. It is still something I intend to look into.
As for multiple images on the same page, it should be good – I haven’t tested it in a while but it should work fine.
88. Lucas Young - 10th Sep 2006 - 8:30 pm
Hi Dave
Yes, my tag is
is there a better way to do this that will prevent the problems with the cropper?
cheers
Lucas
89. Sune Kjaergaard - 11th Sep 2006 - 10:09 am
Hi Dave
I have not been able to pinpoint the exact cause of the bug mentioned earlier, however I do believe it has got something to do with the way the cropper is positioned inside the rather complex table structure. If I move it outside the table the bug doesn’t occur, and similar if I position the containing div absolutely it doesn’t occur either, though of course the known positioning bugs do.
As I am intregrating into an aleready existing CMS, it is not an option to change the layout, or the way it is built (in HTML). Therefore my solution was simply to open the cropper in a new window. Almost to easy huh!?
I’ve also not been able to replicate the bug outside the CMS.
Regards
/Sune
90. Bill Smith - 11th Sep 2006 - 1:23 pm
Hi Dave,
First off, great job on this script. You’ve done some good work here! I’m playing with some code where I want to be able to put the cropper inside of a javascript (virtual) dialog. I’ve played with a couple (iBox, Prototype Window Class) and haven’t had much luck. I finally pulled a dialog library that I’ve been playing with out of mothballs and am starting to have marginal success. In Firefox, the handles for the selected area end up in a different position that where you click. IE has the same problem with the addition if I clear the dialog, then redisplay it, the image disappears. I’m looking into anything that the dialog code is doing that may be causing these problems but I was wondering if you had any thoughts from your side.
You can check out the example
TIA,
Bill
91. Dan - 11th Sep 2006 - 1:33 pm
What about an added feature, so If you absolutly need to crop on scaled images, you can apply the cropper without an image preview, just the cut drag frame? Will this be possible?
92. Bill Smith - 11th Sep 2006 - 7:57 pm
Hi Dave,
I played with it a bit more today and found that my problem is tied to the getCurPos call. The offset in the dialog is the position of the upper left of the dialog. Any thoughts on how that can be subtracted out of the getCurPos call?
Bill
93. Lucas Young - 11th Sep 2006 - 9:02 pm
Oops, I meant my tag is
div style=”width: 379px; overflow:auto;”
cheers
Lucas
94. Kennet Primstad - 12th Sep 2006 - 12:19 pm
I just love the image cropper, have been looking for this a long time.
It works just perfekt for my needs.
Thanks….
95. Bill Smith - 13th Sep 2006 - 12:16 pm
Hi Dave,
I’ve made quite a bit of progress getting the cropper to work inside a prototype window. There were two things I did (although there may be a better way). The first was to change the calculation of the wrapOffsets to be based on cumulativePosition. Then I registered a callback so that when the window is moved, the wrapOffsets are updated to reflect the new position. This appears to work but may have side effects that I’m unaware of. The main problem I’m currently having is after I create the window and then close it, I call Cropper.Img.remove(). Unfortunately I’m finding that the onEndCrop callback still gets called. If I open a window multiple times, the onEndCrop callback gets called multiple times.
Bill
96. Luke - 20th Sep 2006 - 2:39 pm
hi Dave,
thought i’d just drop you a line to say i was very impressed with your javascript cropper. i’ve actually used it and built a mini jpg cropping application for anyone to use. it also offers the chance to create an MSN Display picture sized crop which might be useful to some people.
thanks for the great script, it really inspired me to make something useful – you’re fully outlined in the
.
cheers
Luke
97. Rafael Lima - 22nd Sep 2006 - 7:23 pm
Wonderful!!!
Thanks a lot for your work!
Cheers
98. Rafael Lima - 22nd Sep 2006 - 7:26 pm
I’ve tried to donate but receie an error from paypal:
“We cannot process this transaction because there is a problem with the PayPal email address supplied by the seller.”
99. Andy Stanberry - 26th Sep 2006 - 11:29 pm
I recently added a class to the mix that supports multiple previews. You can check it out here: Javascript UI Cropper with Multiple Previews
100. Dave - 27th Sep 2006 - 9:29 pm
All:
Thanks for th