September 15th, 2005

Prototype/Script.aculo.us Javascript Image Cropper

26 comments on 430 words

UPDATE (May/20/2006): Dave Spurr has built a new cropper which includes most of what everybody wanted out of this cropper. His cropper superscedes this one, so check it out here: “http://www.defusion.org.uk/demos/060519/cropper.php”:http://www.defusion.org.uk/demos/060519/cropper.php

!http://encytemedia.com/blog/files/cropper.png!

For a better part of last night and today I’ve been working on implementing a Javascript image cropper built on “Prototype”:http://prototype.conio.net/ and “Script.aculo.us”:http://script.aculo.us. I’ve got an “initial version posted”:http://encytemedia.com/demo/cropper/ posted for those who’d like to play around with it.

h3. A few features

  • Easily initialized with one line of unobtrusive Javascript.
  • Draggable Crop Box
  • Constrain proportions by holding shift.
  • Callbacks pass the coordinates and size of the crop box allowing you to easily use the data in forms or Ajaxed solutions. RMagick anyone?
  • Works in Firefox and Safari on the Mac (anyone care to test and report on Windows?)
  • Ability to style cropbox as you please.
  • Built on Prototype and Script.aculo.us.
  • Sweeter than a bowl of “Kayro Syrup”:http://www.karosyrup.com/index.htm sprinkled with sugar.

h3. A few known bugs

  • Ability to drag crop box off of picture.
  • Can’t draw NorthWest.
  • Cropbox will go slightly outside the bounds of the image.

I’m working on getting the remaining bugs fixed and adding some polish. I’ve talked with “Thomas Fuchs”:http://mir.aculo.us/ and this will be included in the “Script.aculo.us”:http://script.aculo.us library when it’s ready!

Discussion

  1. Cody Cody said on September 15th

    Looks great, can’t wait to see the finished version!

  2. dave dave said on September 15th

    That’s very cool, I was looking into doing something like this recently but got bogged down because I thought I’d need to use Flash.

    The exact functionality I was going to build was when you had a set size/proportion and you zoom and drag the image behind a fixed viewfinder.

    For reference see the buddy image cropper in iChat or this flash implementation: http://dev.worldoptimizer.com/flashcropsave/interface.swf

    and some variations: http://flashrocket.worldoptimizer.com/article/12/flash-based-cropping-tool

    How do-able would something like that be?

  3. Scott Scott said on September 15th

    Nice!

  4. ichigo ichigo said on September 15th

    very nice work :o)

  5. Justin Palmer Justin Palmer said on September 15th

    dave: Nice examples! Something like that would involve using CSS’s clip property which is still kinda shaky and would also require rewriting most of the script. Just not something I’m up to at the moment.

  6. atmos atmos said on September 16th

    That’s insane, nice work man.

  7. Justin Justin said on September 18th

    Nice!

    but it doesnt work in IE on windows (like many things)

  8. Justin Palmer Justin Palmer said on September 18th

    Justin, thats a question I asked in my blog entry. If any of you guys are running Windows and can test in IE and FF that would be might square of you. ;-)

  9. Alex Alex said on September 27th

    Cool. A couple items for a wish list.

    Need to darken the croped area not lightenthe area to keep.

    Need to be able to resize crop box after its been created.

    the fist one is done on the demo at http://www.speedingrhino.com/cropper.htm

    I actualy remember seeing a demo somplace where someone had both of those working, and it was very cool.. I have been trying for 2 hours now to find it but no love… Any how, it will be nice to have this working using Scriptaculous.. I am uing that alread in some of my other code…

  10. Alex Alex said on September 27th

    Nice Ajax implimentationon these coments!! That was so cool I have to add anouther!!!

  11. veged veged said on October 3rd

    In the IE/Win default drag behaviour on image block your functional.

  12. mef mef said on October 18th

    http://www.speedingrhino.com/cropper.htm

  13. mef mef said on October 18th

    oops, never mind, didn’t read the followups

  14. mef mef said on October 18th

    check this one out too http://ditchnet.org/dhtmlgallery/crop.html

  15. test test said on October 19th

    Testing, sorry ;)

  16. MichelT MichelT said on October 20th

    very well, can you save the picture . If you want i can test it on lot of windows version and browser …

    go to my site for contact : www.pixeldargent.com

  17. louis louis said on November 3rd

    it would be nice if you could send a ratio for the resize box to be into it. forinstance if you had to create images a certain dimenions then you send in the dimensions, then the user can resize the box (keeping ratio) then export to a ressize.

  18. Gustavo Gawryszewski Gustavo Gawryszewski said on November 24th

    nice, but it doesnt work in IE/Windows
    and it works in FF/Windows

  19. Mik Mik said on December 27th

    Nice, good luck in fixing those bugs. Hope somebody could also come up with an IE port of this useful tool. Keep it up, man! :)

  20. Glenn Nilsson Glenn Nilsson said on January 16th

    Hi, I “solved” the IE problem.

    The problem is that IE lets the mousedown-event go through the canvas down to the original image. Seems like a browser bug to me…

    During my testing of the problem I found that this solves the problem:

    this.canvas.style.background = “blue”; Element.setOpacity( this.canvas, 0 );

    Add that last in initCanvas. The color has no meaning, but there has to be something in the background. Then set the opacity to 0 to nullify the effect of the bg-color.

    I’ll be glad to see this in the scriptaculous library later.

  21. Chris Chris said on March 28th

    I think this thing is great. The IE hack helped a ton.

    I’m trying now to figure out how to use this on an image that’s centered on the page and I’m having a horrible time with it.

  22. Chris Chris said on March 28th

    It took me a few hours, but I think I figured it out. When I centered the image using CSS, the initial point would be recorded wrong. I traced it down to the setting of the initialHandleCoords in the onDrag method. When the initialHandleCoords are set, it is using something like this:

    x: Event.pointerX(event) – this.picContainerOffsets.left

    I noticed that when I dragged the mouse, however, the cursor and the corner of the drag box matched up. After much looking, I figured out that it is because when dragged, the offset is found in a different way. Rather than using the picContainerOffsets variable, it is set like this:

    var currentX = Event.pointerX(event) – this.picContainer.offsetLeft

    The picContainerOffsets are set using in the initialize method using prototype’s Position.positionedOffsets. I’m not sure why these were used rather than the picContainer.offsetLeft calls that are used later.

  23. Alex Alex said on April 7th

    Does work in IE

  24. Charles Charles said on April 11th

    I found a simple solution for fixing the box so it can’t be put outside the image when dragging. I’ve tested it in FF on a PC and with the fix above from Glenn Nilsson tested it in IE on a PC. There are still a few minor issues with it working in IE. If you’re still working on this and interested I can continue to do testing.

    For thi fix add this directly after the height, width, top and left get set in cropComplete:

        if (left + width > this.picture.width) {
            this.handle.style.left = (this.picture.width - width) + "px";
        }
        if (top + height > this.picture.height) {
            this.handle.style.top = (this.picture.height - height) + "px";
        }
        if (top < 0) {
            this.handle.style.top = "0px";
        }
        if (left < 0) {
            this.handle.style.left = "0px";
        }
        var top = this.handle.offsetTop;
        var left = this.handle.offsetLeft;
    
    
  25. Adam Adam said on April 16th

    ...any progress on this?

  26. Dave Spurr Dave Spurr said on May 21st

    Just a quick note to thank Justin for linking through to my cropper project which is based on Prototype and script.aculo.us.

    As he says it covers most of the features requested in these comments, which include (to name a few):
    • Image editing package styling & functionality
    • 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 dynamic preview of resultant crop
    • Movement of selection area by arrow keys
    • All functionality & display compatible with most popular browsers supported by Prototype

    See the demo page and the cropper project page for more details, and thanks again to Justin for the link.

Sorry, comments are closed for this article.