January 22nd, 2007

Crop & Resize with JavaScript, PHP, and ImageMagick

Update: A new version of this example and demonstration has been released. Version 1.1 includes a GD version of the script as well as a number of functional improvements worth checking out.

Update: There has been significant discussion in the comments regarding a GD version of this script. I have re-released v1.0 of this example as a GD version as well as an ImageMagick version. You can find both downloads below The download has been removed in preparation of version 1.1 due for release on December 17, 2007. They behave function exactly the same as one another. Thanks for all the contributions (Stefan Leever and others) and requests!

Image management is often a big part of a comprehensive CMS, and in my personal experience, has been one of the more difficult areas to make really easy for people to use. Those that are technically inclined are often able to successfully format an image to their liking if given proper instruction. On the other hand there are many who find image editing to be difficult, intimidating, and/or confusing.

View JavaScript Crop & Resize Demo

In an effort to better the situation, I put together a demo which gives the end user the ability to upload, crop, and resize their image from within their Web browser. The intention, on my end, would be to use it for server-side image pre-processing before the image is used by a client in their CMS for their website.

What powers JavaScript cropping & resizing

This demo wouldn’t be possible without a number of scripts that can not go without mention. First and foremost, the entire client side process is based on the Prototype JavaScript Library. Prototype has just been updated to version 1.5.0 and it acts as the foundation for both the crop and resize stages of the demo.

Another essential piece of this demo is script.aculo.us, a collection of effects addons for Prototype. It provides some really excellent UI tools to work with as well as various animation effects. Both the crop and resize stages use script.aculo.us features.

JavaScript Image Cropper UI by Dave Spurr

In my opinion, the most comprehensive aspect of this JavaScript image crop and resize demo is the cropping stage. The JavaScript Image Cropper UI by Dave Spurr is a script that deserves the spotlight in the demo. It was written using both Prototype and script.aculo.us and it is the one piece that really brings the demo together.

Dave’s JavaScript Image Cropper UI has many more capabilities than the basic version included in the demo. If you check out his documentation he includes cropper user interfaces that can inherit various options such as retaining an aspect ratio, min/max width/height, and also display a crop preview if desired. The JavaScript Image Cropper UI by Dave Spurr is a feat in and of itself and should be given proper recognition.

Server requirements

The provided demo was written for a Linux based Web server running PHP and having ImageMagick installed. It should run on a Windows based server with minimal adjustments, such as including the path to your ImageMagick install for each command.

ImageMagick is a powerful command line image editor which performs all of the actual image transformations server-side. Installation is usually quite easy after following directions written for your Web server configuration.

Installation notes

After you’ve downloaded the demo, extract the files to a common directory and upload that folder to your server. After uploading, the directory will need to be given write permissions. Failing to do so will destroy the functionality of the demo as uploads won’t be saved and there would be no way to create the adjusted images.

Download ImageMagick Crop & Resize (.zip)

Closing thoughts about cropping and resizing with JavaScript

This demo was put together in an effort to make very basic image editing easier for less technically inclined users. The main intention is to provide it as a component to include in a variety of applications including custom content management systems or Web applications.

I hope the demo proves to be useful, but I absolutely welcome any comments and criticisms readers may have. Please take a minute to post your thoughts below so that the demo can be in constant revision to ensure it’s effectiveness.

 Comments

104 Comments

  1. Sam January 22nd, 2007

    This is really great Jon! I can definitey see it being used within a CMS. Are there any plans for a WordPress plugin version, perhaps as an editing stage when uploading an image? I’m sure it would be very well received.

  2. Mike Papageorge January 22nd, 2007

    Nice work!! I’d wanted something like this for a while now for our CMS…

  3. P.J. Onori January 22nd, 2007

    Oh man, that is slick indeed. The tool is ridiculously easy to use and quite powerful to boot. Great work man - awesome stuff.

  4. Chris Robinson January 22nd, 2007

    Great work! I to think this would be a great Wordpress plugin, I’ve actually been looking for one that accomplishes just this.

  5. Milan January 27th, 2007

    This looks promising, keep up the good work! Some things to consider when making a wordpress plugin out of it:

    - Why rely on ImageMagick? I know it’s powerful, but it’s not as common as GDlib. This might prevent people from using it. Or am I mussing a point?

    - Features for maximum width/height, aspect ratios, default cropping sizes would be good for preventing editors from screwing up image work.

    Thumbs up!

  6. All in a days work… January 28th, 2007

    [...] Crop & Resize with JavaScript, PHP, and ImageMagick a demo which gives the end user the ability to upload, crop, and resize their image from within their Web browser, uses ImageMagick, Prototype, scriptaculous, and PHP (tags: Image_Editors) [...]

  7. parth January 30th, 2007

    hi,
    i really liked this thing…
    its fast and look & feel is also good..

    regards,
    Paarth.

  8. kyle January 30th, 2007

    Another thing that would be nice to see is the height and width of the resized images while resizing. Ideally, you’d have two text input boxes with the values - so if I wanted to have it be 400px wide I could just enter that in the text box. A constrain proportions checkbox would be handy as well - since I might want something to be 300×200 pixels, even if those aren’t the current dimensions.

    Which leads to wishlist item #2 - a ratio option for cropping.

  9. richard rubbra February 2nd, 2007

    Hi, I have been looking for a tool like this. I need just the ability for the client to select a portion of a very wide image, save it and have it sent back to me. I run on a windows server, You state that it is simple to adjust this php to run. I know nothing about this area. Is it really that easy? And any idea exactly what one adjusts?
    Thanks
    Richard

  10. Crop & Resize with JavaScript, PHP and ImageMagick | Avinash - An IT Blog February 7th, 2007

    [...] Site: Monday By Noon [...]

  11. wozzzza February 23rd, 2007

    brilliant script, just brilliant. apart from one factor, this script could be very very popular, more popular than it is now. the simple fact that imagemagic is not on every server and lots of servers have exec() disabled for security reasons. i was desperate for a script like this, i put this in my too hard basket and kept looking for another one to do what i wanted because of this image magic problem. then i thought, stuff it, i will try and change it over to use gd library. so i did it, took me a couple hours and was quite easy to change to use gd library, i dont know why the author doesnt do it, was very easy to do then more people would want this script.

  12. wozzzza February 24th, 2007

    got a problem now, in IE6 when i use this script in a layer created with Advanced Layer Popup http://www.dmxzone.com/ShowDetail.asp?NewsId=12769 the crop area creates a hole through to the website in the browser underneath and you cant see what ya cropping, any ideas on how to fix this?? works fine in firefox.

  13. scriptvote » Blog Archive : Crop & Resize with JavaScript, PHP, and ImageMagick » Crop & Resize with JavaScript, PHP, and ImageMagick February 25th, 2007

    [...] http://mondaybynoon.com/2007/0.....magemagick This entry was posted on Sunday, February 25th, 2007 at 8:00 pm and is filed under Webmaster, [...]

  14. Julian Schrader April 2nd, 2007

    > - Features for maximum width/height, aspect ratios, default cropping sizes would be good for preventing editors from screwing up image work.

    Ditto.

    Great work though!

  15. Marcus April 5th, 2007

    Hey Jon,
    This is pretty impressive stuff. I really want something like this for my website running on W2k3. I think I will download it and see if I can get it running on W2K3. Very good demo….
    Marcus

  16. Robert A. April 5th, 2007

    I tried the demo and it’s awesome! I just got imagemaick installed and uploaded all the files. set the permission. having a problem though. when it goes to image_resize.php, i get this error.

    Warning: chmod() [function.chmod]: No such file or directory in /home/www/****/resizer/resize_image.php on line 34

    When I dont crop or resize, the error doesn’t happen and i’m able to finish the process but without any editing. Any ideas what the problem may be. I did /usr/local/bin/convert logo: logo.gif to check if imagemagick was working and i didn’t get any errors.

  17. Il meglio della settimana - 27 | Napolux.com May 13th, 2007

    [...] steps to become a better blogger H3rald.com Getting started with PEAR Best PHP tools of the month Crop & resize with JavaScript & PHP barcamp matera, blog, H3rald, Il meglio della settimana, javascript, pear, [...]

  18. jigga May 14th, 2007

    This is exactly what I have been looking for. BUT I get the same error Warning: chmod() [function.chmod]: No such file or directory in ****/resize_image.php on line 34.

    It seems that the

    exec($convertString);

    fails. This will resize the image and move it to right folder but it does not do it. I tried in ie6, ie7 and ff2.1 but no luck.

    $convertString is “convert working/uploads/logo.jpg -crop 483×219+411+229 working/cropped/logo.jpg”

    I would appreciate an answer soo much couse this is exaclty what I have been dreaming of..

  19. Jorge Salvador Caffarena May 15th, 2007

    Nice script!

    I tried setting custom crop and ratio settings as told in the Javascript Image Crop script docs, but it doesn’t work with your script Jon, seems to ignore what I put in init_cropper.js. Maybe that’s why you get so many request for it Jon, seems to not be working properly.

    Anyway, will be watching in case of a fix, as this is nice work indeed.

  20. Brandon June 1st, 2007

    Any updates on whats breaking the script? Awesome script man! Very nice.

  21. nicky June 1st, 2007

    Great and easy to use! Although I don’t have access to ImageMagick so I need this with GD-support. Have anyone made it or do I have to make it myself?

    I’d also like to lock the ratio, but I haven’t tried it yet.

    Thanks for a cool script!

  22. patrick June 7th, 2007

    can you also add a rotate option, imagemagick has a rotate command. also say i wanted to keep the ratio… ex. image is 400×400 and i want the image cropping to always be within the ratio down to 50×50 how would i do that? the user shouldn’t be able to size the image to 100×50

  23.   links for 2007-06-12 by SridhaReena June 12th, 2007

    [...] Crop & Resize with JavaScript, PHP, and ImageMagick - Monday By Noon (tags: javascript crop resize image) [...]

  24. ste June 28th, 2007

    oooooo a GD version… sounds good…. I’m just in the middle of chrooting imagemagick so a straight GD version would certianly be easier to setup

    Good luck with the development

  25. Andy July 26th, 2007

    I’ve adapted this to integrate back office/shopping cart to allow the admins to crop a picture then it provides a thumbnail and a standard size picture.
    Good Work !!!

  26. will August 20th, 2007

    i really like this script… it would in theory solve a couple of issues i encounter with a project i’m working on. kudos… i’m curious… is it possible to set relative x y say % instead of pixel size attributes to the crop tool?

  27. John Clark August 28th, 2007

    Hey Jon, Great script! I was just wondering how close you are not to releasing the new version? If you need a tester, please let me know. Thanks!

  28. Stefan September 4th, 2007

    Currently I have managed to create a GDLib version of this script myself as well.

    I’m currently shaping it a bit to the needs I have to have for a website i’m creating (Jonathan, your name and this site are of course noted underneath your script!).

    But the GDLib implementation is fairly easy although you have to play with vars a bit.

    I have managed to use the same values Jonathan posts when you’ve selected the fragment of the image. After that I just used the Imagecopyresampled function like this:

    (file: resize_image.php the code is between line 33 (if-statement) and the else.
    code:
    $dest = imagecreatetruecolor($cropWidth, $cropHeight);
    $source = imagecreatefromjpeg($sourceFile);
    // Resize
    $crop_dest_X = $cropX - $cropWidth;
    $crop_dest_Y = $cropY - $cropHeight;
    imagecopyresampled($dest, $source, 0, 0, $cropX, $cropY, $cropWidth, $cropHeight, $cropWidth, $cropHeight);
    // Output
    imagejpeg($dest, $destinationFile);
    //—

    I have used a destination file as the usual iMagick script does as well… after a bit of throwing with numbers I discovered that setting the original size to the size the user has selected it works fine and fetches the fragment on the pixel!

    Good luck with it yourself!

    Stefan Leever

  29. Stefan Leever September 4th, 2007

    @Jonathan Christopher: sure you could use it in a GDLib alternative download with the due credit hehe, I have to note a minor change of some unused variables in the code above.

    This would be the final code:

    $dest = imagecreatetruecolor($cropWidth, $cropHeight);
    $source = imagecreatefromjpeg($sourceFile);
    // Resize (using the selected size the user has selected to crop (last 2 vars)).
    imagecopyresampled($dest, $source, 0, 0, $cropX, $cropY, $cropWidth, $cropHeight, $cropWidth, $cropHeight);
    // Output the resampled image to a file:
    imagejpeg($dest, $destinationFile);

    Good luck! And I will probably see the new update soon!

    Stefan Leever

  30. Rob Nugen September 8th, 2007

    Fantastic script!!

    I made a bit of a code change that I’m sure you’ve already made as well, but I just want to include it so you can change the current downloadable version (which I just downloaded)

    Under the line // RESIZE IF UPLOAD IS TOO BIG

    you check the width twice.

    if(($dimensions['width']>$maxWidth) || ($dimensions['width']>$maxWidth)){

    I changed it to width and height

    if(($dimensions['width']>$maxWidth) || ($dimensions['height']>$maxHeight)){

    And then changed to use the variables instead of the hardcoded 640×480 which had been there.

    $cmd = “convert ” . $imageLocation . ” -resize ” . $maxWidth . “x” . $maxHeight . ” ” . $imageLocation;

    ——

    I’m interested in tweaking it so that I don’t have to upload pics, but can just point to pics that already exist on my server. I have thousands, and all the thumbnails were created by just shrinking. I want to recreate them with yer beautiful code.

    If I get it working, I’ll definitely send copies.

  31. Serdar October 10th, 2007

    thanks for this great job! but I’d like to ask something. I am doing a new web site and I want to use this picture thing. But some of my friends say that no hosting company let you use this scripts because of some root priviliges. Can you inform me about that? Thanks in advance

  32. speedstar October 10th, 2007

    Hello
    first of all thanks.. My question is similar to Serdar’s. In your php document, there are exec() commands and most of host company do not let you use them. What can I do? Do I have to change them or have to use an interface like IMagick?
    Thanks

  33. Sussie October 11th, 2007

    Hey.
    I love this. but…
    Could someone help me with this? I don’t want to use this script for uploading, as I already uploaded the files I would want to crop and resize.
    Do you guys have an idea how to get crop_image.php, resize_image.php and last_step.php to use a $_get variable??????
    I’m not an advanced programmer.
    Thanks.

  34. Henryk October 12th, 2007

    hi,
    can I use this demo zip on my website and why the images are beeing deleted after 5 minutes?

  35. Henryk October 12th, 2007

    How can I change the picture upload size from 50k to more? please help

  36. Henryk October 12th, 2007

    Any idea why am I getting this error after image uploaded?

    Warning: exec() [function.exec]: Unable to fork [identify 'working/uploads/Water lilies.jpg' 2>/dev/null] in D:\Domains\domain.com\wwwroot\crop_image.php on line 43

  37. seb October 18th, 2007

    I need this to work on my Joomla website…

    I tried to use the code but the image that get displayed is a regular one… (i.e. there are no cropping functionality.)

    This may be a path related issue. As joomla doesn’t allow relative path. (i.e. In the php files, we must insert the directory into which the code is placed.)

    Thanks for any help !

    P.S. My goal was to make it work with GD2.

  38. Rob Nugen October 19th, 2007

    @Sussie, cc@Jonathan:

    I modified the code to not require (not even allow) uploads. I have thousands of pictures on my site already. I just want to crop and thumbnail them.

    The code I wrote is tweaked for my website, but should be pretty easy to make work for others.

    The source for what I wrote is on my site here:
    http://robnugen.com/downloads/.....ler_v1.1r/

  39. Rob Nugen October 19th, 2007

    @Serdar, @speedstar

    I am using the code no problems on my site, which hosted by http://www.dreamhost.com/r.cgi?204388

  40. Cassius October 25th, 2007

    “How can I change the picture upload size from 50k to more? please help”

    In the file named index.php, where it says:
    input type=”hidden” class=”hidden” name=”max_file_size” value=”50000″

    Change th 50000 to whatever you need.

  41. Shalimar October 28th, 2007

    Hi, I saw some posts about various warnings with function.chmod and that is my problem too. However, I have 10 warnings 4 chmod ones, 4 mkdir errors, and 2 move_uploaded_file errors. I have everything set to 777. What could be the problem?

    Warning: mkdir(working) [function.mkdir]: Permission denied in /home/aspensan/diamondloungegirls/demo/crop_image.php on line 21

    Warning: chmod() [function.chmod]: No such file or directory in /home/aspensan/diamondloungegirls/demo/crop_image.php on line 21

    Warning: mkdir(working/uploads) [function.mkdir]: No such file or directory in /home/aspensan/diamondloungegirls/demo/crop_image.php on line 22

    Warning: chmod() [function.chmod]: No such file or directory in /home/aspensan/diamondloungegirls/demo/crop_image.php on line 22

    Warning: mkdir(working/cropped) [function.mkdir]: No such file or directory in /home/aspensan/diamondloungegirls/demo/crop_image.php on line 23

    Warning: chmod() [function.chmod]: No such file or directory in /home/aspensan/diamondloungegirls/demo/crop_image.php on line 23

    Warning: mkdir(working/finished) [function.mkdir]: No such file or directory in /home/aspensan/diamondloungegirls/demo/crop_image.php on line 24

    Warning: chmod() [function.chmod]: No such file or directory in /home/aspensan/diamondloungegirls/demo/crop_image.php on line 24

    Warning: move_uploaded_file(working/uploads/IMG_0002.JPG) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/aspensan/diamondloungegirls/demo/crop_image.php on line 38

    Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move ‘/var/tmp/phpsCOi0x’ to ‘working/uploads/IMG_0002.JPG’ in /home/aspensan/diamondloungegirls/demo/crop_image.php on line 38

  42. Shalimar October 28th, 2007

    Nevermind my last post, it was a bit premature. Everything is working fine now. Awesome program!

  43. shalimar October 28th, 2007

    just wondering what file/function i’d edit so that the box isn’t drawn to user specification but to my own (300px by 300px) when cropping. In short i want the cropping tool to be 300 by 300 as the default

  44. nitin sharma October 29th, 2007

    hi this script grb but facing the problem on the resize_image page
    Warning: chmod() [function.chmod]: No such file or directory in C:\wamp\www\crop3_best\resize_image.php on line 34

    how ot solve that problem
    Thanks

  45. Magnus Ingi November 5th, 2007

    @ shalimar

    Just edit the init_cropper.js file and use the “ratioDim” and “displayOnInit” to make it work like you want to.

    An example:

    Event.observe(window, ‘load’, function() { new Cropper.Img(’cropImage’,{onEndCrop:onEndCrop,minWidth: 300,minHeight: 300,ratioDim: {x: 300, y: 300}, displayOnInit: true}) });

    This will give you a 300 by 300 crop on page load and it will not allow the user to shrink the crop to less then 300×300 :)

  46. Magnus Ingi November 5th, 2007

    Hey Jonathan,

    I’ve been testing your script a little and I’ve gotten into a bit of a problem regarding IE7 (and maybe IE6, haven’t tested it yet).

    The problem is that when I use the Crop UI inside and iFrame it doesn’t display correctly in IE. It only displays the “crop box” and a dark background but not the image it self. It works great in Firefox but not in IE. When I crop the image and submit it everything is submitted OK, both in IE and FF, but the Crop UI doesn’t seem to want to display the image in IE.

    Do you know why this is? Do you have a solution for me? :)

    Sincerely,
    Magnus

    PS. Nice script!

  47. Magnus Ingi November 5th, 2007

    @ Jonathan Christopher: No, I’m not trying to use an iframe to escape the page refreshes. I’m using an iFrame because I’m displaying the crop interface within a “Thickbox” (http://jquery.com/demo/thickbox/).

    I tested the page out with out using the iFrame (with the same CSS and same Javascript) and it works fine when not inside the iFrame. But when I try displaying in in the Thickbox, it doesn’t work in IE. I’m going to see if it works in Safari and Opera, if so, IE sucks ;-)

    If you have any ideas, please let me know!

    Sincerely,
    Magnus

  48. Thue Andersen November 20th, 2007

    There is a bug in the JavaScript selection tool.

    If you … how can i explain this. I’m Danish. I will try my best.
    If you select a wide area that goes all the way from the left to the right, but keeping a selection with only a small height, then somehow the entire picture is selected.

  49. <blog> interfce.com </blog> » Blog Archive » bookmark dump November 20th, 2007

    [...] Crop & Resize with JavaScript, PHP, and ImageMagick - Monday By Noon [...]

  50. fish November 25th, 2007

    I am trying to figure out where to config my path to imagemagick. I have everything installed and the path added for my user id on the server, but I am not sure where in the PHP files to prepend my path.

    Thanks in advance and thanks for this awesome script!

  51. Paul November 29th, 2007

    I’ve only just come across this srcipt today and I really like it think it could come in very handy if only I could get it to work. I noticed its been some time since the mention of a GDLib version was last mentioned, is this on stop now or is it still in the process of being coded?

    I thought its well worth asking as I’ve spent a few hours this evening following Stefan Leever’s posts above in attempt to sort a working GDLib version for myself but am not really any closer after 4 hours than I was after five mins lol, as you’ve probably guessed by that, I’m a real beginner (at best) when it comes to php.

    Despite getting the cropping part to work well, I can’t for the life of me get the re-sizing part within last_step.php working properly. At best I could achieve a resize in the width but not the height (and only then if a crop had been performed previously, if the image was passed onto last_step.php uncropped the re-sizing step either resulted in php erorrs or a very strange final image which barely resembles the original image or both at the same time).

    I managed to fix one or two smaller problems I had with getting the script to work (all be it without the re-size part) on a Windows server by:

    replacing lines 40 and 41 in resize_image.php with… rename(”$sourceFile”, “$destinationFile”);

    and replacing lines 27 and 28 in final_step.php with ….
    rename(”$sourceFile”, “$destinationFile”);

    While looking for them lines I noticed a few more instances of $cmd in the files that may not work properly on a windows server properly so in the absence of any better ideas I’ll go back and take a look at them.

    Hope a release of proper GDLib version is still on the cards…..
    Paul.

  52. Paul November 30th, 2007

    Hi Jonathon,
    Thankyou for the reply, good to hear that a GDLib version hasn’t been forgotten about :-)

    I like playing around with scripts and although my lack of experience with PHP or any script for that matter slows me down I am definately learning a fair bit about PHP through Trial and Error so I’m not worried about spending a bit of time playing around.

    That said I’ve spent a few more hours playing around with this again tonight and I’m very close to getting a fully working GDLib version working on a Windows server, Infact I only really have the initial resize pic to $maxWidth and $maxHeight to sort now (it works for landscape pics but need to think of a way recoginse portrait images and deal with them properly), the rest seems to work fine though :-) .

    I’d be more than willing to pass on the files I’ve ended up with / got so far - if they would be any help to you Jonathon? although the bits I’ve changed probably look a bit crude and poorly written to someone who knows what they are doing, it seems that they are at least functional. Just send me an email if you think they would be of any use to you.

    I did think about listing all the changes I made in here so others could build off it but I changed so much in the end that it would take me all night to type it all up, thats if I could rememeber / recognise all the bits I did alter :-s LOL, besides it would be a far better idea for people to wait for the proper GDLib version to be released as its bound to be better than what I came up with.

    Paul.

  53. Crop & Resize with JavaScript, PHP, and ImageMagick - Monday By Noon | David Bisset: Web Designer, Coder, Wordpress Guru December 4th, 2007

    [...] This gives the end user the ability to upload, crop, and resize their image from within their Web br… Tags: ImageMagick, Javascript, PHP [...]

  54. fish December 4th, 2007

    I am trying to get it working on a BSD system. It is a shared hosting platform. I know for sure they dont have IM installed by default…. thanks for your reply!

    -fish

  55. acctman December 5th, 2007

    do you have any code to rotate the image 90, 180 and 360 from left to right?

  56. acctman December 7th, 2007

    ok i changed the Resize image to check just the width and also check to see if the image is too small. If, image is less that 320 in width increase…

    $maxWidth = 640;
    $maxHeight = 480;
    $maxWidth2 = 320;
    $maxHeight2 = 240;

    // RESIZE IF UPLOAD IS TOO BIG
    if(($dimensions['width']>$maxWidth) || ($dimensions['height']>$maxHeight)){
    $cmd = “convert ” . $imageLocation . ” -resize 640 ” . $imageLocation;
    $results = exec($cmd);

    // RESIZE IF UPLOAD IS TOO SMALL
    } elseif(($dimensions['width']<$maxWidth2) || ($dimensions['height']<$maxHeight2)){
    $cmd = “convert ” . $imageLocation . ” -resize 320 ” . $imageLocation;
    $results = exec($cmd);

  57. acctman December 7th, 2007

    ok, i changed the init_resize.js so the user can not resize an image to small…
    old : range:$R(20,200),
    new: range:$R(100,200),

  58. acctman December 7th, 2007

    can someone assist me with using the “convert -rotate 90″ command. I want the user to be able to rotate there image 90, 180 from left to right

  59. Paul December 7th, 2007

    I looked quickly into acheiving the same thing as you acctman but unless I have got the wrong end of the stick you need to use php (or another server side script) to rotate the image as javascript, being a client side script can’t do it. Of course if you are using ImageMagick then it may be easier than figure out your own custom php to do the job, but I have no idea how image magick works.

    Even using Image Magick you could still use php to do the same thing (by appling the rotate to the finished image)
    The way I thought about doing it was to add extra form fields in resize_image_php - add a few radio buttons which correspond to a set rotation angle which when selected submit a value to last_step.php then in last_step.php you’d need to use an IF statement to check if the rotate buttons are selected and if they are then perform the corresponding rotate on the final image using php image rotate - http://uk.php.net/imagerotate

    Obviously this way of doing it means that you have no preview of what the image would look like rotated, and I’m no where near clever enough to figure out a way to do that :-S. Ideally you could do with being able to rotate the cropper / image in the beginning but I suspect the amount of extra javascript needed to do that would be huge.

    Maybe some smarter people will have a better way to do it?

  60. acctman December 7th, 2007

    anyone have a code to convert the image format to .jpg after upload or upon saving?

  61. Mehul B Dobariya December 9th, 2007

    I am getting errors:
    Notice: Undefined offset: 1 in C:\Program Files\EasyPHP 2.0b1\www\crop\crop_image.php on line 52

    any idea?

  62. Mehul B Dobariya December 9th, 2007

    And what if i want to uplaod file bigger then 50K

  63. Mehul B Dobariya December 9th, 2007

    Ohhh i got everything….

    Cool script…

    good effort..

    Thanks..

  64. Mehul December 10th, 2007

    Hay,
    Can you help how I can make crop area fixed? I mean when I upload image and suppose image size is whatever like (1000 X 800, 2000 x 1200 etc) but after uploading I want to set crop area only 800 X 600. I don’t want to allow user to change it. And is it possible to make crop and resizing step in on page only?

    Please help me asap.

    Thanks..

  65. Mehul December 10th, 2007

    i want somehthing like

    http://www.easycropper.com/
    Here you can move crope area but you can not resize it and also you can resize and crop image in same page not like first you crop and then resize..

  66. Paul December 10th, 2007

    To fix the crop size set both minWidth and maxWidth to the same value (800) and minHeight and maxHight to the same value (600) in: lib/init_cropper.js

    i.e. replace:
    Event.observe(window, ‘load’, function() { new Cropper.Img(’cropImage’,{onEndCrop:onEndCrop}) });

    With:
    Event.observe( window, ‘load’, function() {
    new Cropper.Img(
    ‘cropImage’,
    {
    minWidth: 800,
    minHeight: 600,
    maxWidth: 800,
    maxHeight: 600,
    onEndCrop: onEndCrop
    }
    );
    } );

  67. Mehul December 11th, 2007

    Paul.. Thanks for your help. but its not working.

    i changed init_cropper.js but no luck at all…

    can you please tell me whats the reason?

    Thanks,
    Mehul

  68. Paul December 11th, 2007

    Oops Sorry Mehul, my mistake I left out a }

    anyway try this instead:

    Event.observe(window, ‘load’, function() { new Cropper.Img(’cropImage’,{maxWidth: 800, maxHeight: 600, minWidth: 800, minHeight: 600}, {onEndCrop: onEndCrop});} );

  69. acctman December 11th, 2007

    does anyone know if its possible to combine the 3 steps into one with no page loading? I ask the because I’d like to add the script to a registration page and I’m trying to figure out the best way to do it.

  70. Mehul December 11th, 2007

    Ohh thanks Paul. you rock..

    Can you tell me how can i make croping and resizing in same page. I want these thing in one page only i dont want first croping and then resizing.

    Is it pissible?

  71. Mehul December 11th, 2007

    One more thing paul..

    I have to click on the image to see the crop area. can i make appear once image get loaded. so visitor can have a idea or crop area.

  72. Paul December 12th, 2007

    Hi Mehul, glad that worked for you.

    Getting it all on one page is bound to be possible but is too difficult for me to work out, Best wait for Jonathans next release than try and figure it out for yourself as it’ll save quite a few hours of work.

    As for getting the cropper to show on pageload try this in place of the code above:

    Event.observe(window, ‘load’, function() { new Cropper.Img(’cropImage’,{ratioDim: {x: 4,y: 3},minWidth: 800, minHeight:600, maxWidth: 800, minHeight: 600, displayOnInit: true},{onEndCrop: onEndCrop});} );

    Hope that helps.

    Looking forward to the next release Jonathan, I’ve got a fully working GDlib version now but It’s a little bit inneficient, as I’ve pretty much done away with passing the variables from file to file with a form and have calculated the image dimensions in each file and gone from there. I’m sure it could be slimmed down a hell of alot, I also made a start with a rotating function, an input form to select an angle etc and the PHP to actually rotatate the image is easy enough but the javascript / DHTML to preview the rotation seems to be the difficult bit to get working with multiple browsers without errors. I.E is ok when using Microsoft filters but Firefox is another matter. Anyway I’ll wait for your next release before playing with it any more as it sounds like its going to be much better than what I was going to do :)

    Paul.

  73. Mehul December 12th, 2007

    Hay paul..

    One weared thing i found.
    if i change the code to
    Event.observe(window, ‘load’, function() { new Cropper.Img(’cropImage’,{maxWidth: 800, maxHeight: 600, minWidth: 800, minHeight: 600}, {onEndCrop: onEndCrop});} );

    it worked fine i am getting image with croping area 800*600. but after that if i hit save… its not croping the image. i am getting original image.

    check http://mehuldata.freehostia.com/crop/

    any clue?

  74. Paul December 13th, 2007

    Hi Mehul,

    After quite a bit of playing I think I got it:

    Event.observe(window, ‘load’, function() { new Cropper.Img(’cropImage’,{ratioDim: {x: 220,y: 165},minWidth: 800, minHeight: 600, maxWidth:800, displayOnInit: true, onEndCrop: onEndCrop});} );

    Thanks for the GD release Jonathan I think that it will please alot of people :) Only had a quick look but I can see starightaway its alot neater that what I got lol.

  75. Mehul December 13th, 2007

    @Paul: I used your code but still its not working. Everything else is working fine. Only crop is not working after chaging init_cropper.js.

    check out : http://mehuldata.freehostia.com/crop/

  76. Paul December 13th, 2007

    @Mehul
    I’m really not sure why that last section of code didn’t work for you, that is the exact code (apart from the chosen dimensions) as I have in use here:
    http://www.completefisher.com/cropgdratio/

    The only difference is that I’m using a GDLib version I cobbled together whereas you must be using Jonathons original release (that uses ImageMagik?) It may be that ImageMagik is for one reason or another ignoring the JS cropper output? I really can’t help with ImageMagik as I’ve never used it. Since Jonathan has released a GD version very similar to what I am using it may be worth trying that instead of the version you have now and seeing if that makes any difference.

    @Jonathan

    I’ve put your GD version on both my localhost server and a live server (both windows servers) and on both there are a few problems with the re-sizing in the last_step.php and also problems if you don’t perform a crop (in crop_image.php) or resize (in resize_image.php) you can see what I mean here:

    http://www.completefisher.com/cropgd

    The problems of the image not being moved if a crop or re-size is skipped, is I think, a windows server issue as it doens’t seem to perfom the exec $(cmd); line. I got around these problems by using:

    // CROP WAS SKIPPED — MOVE TO CROPPED FOLDER ANYWAY…
    rename(”$sourceFile”, “$destinationFile”);

    etc.

    In place of
    // RESIZE WAS SKIPPED
    $cmd = “mv ” . $sourceFile . ” ” . $destinationFile;
    exec($cmd);

    (similar for each file affected)

    As for the last_step.php not returning the correct / chosen crop at the chosen size, I think that might not be platform specific, Rather than type out the whole section of code that works for me I’ll email you the last_step.php that I come up with - I just spent a bit of time tidying it up and replacing variable names to match those in your version, so you can see whats going on (I hope - It’s still a bit messy). The first link above uses this last_step.php file.

    Hope thats of some help
    Paul.

  77. Mehul December 14th, 2007

    @Paul: finally it worked.. Thanks buddy..
    now only one thing i want is crop and resize should be in one page only.

    i tried a lot but cant do it.. can you help me please? any idea?

  78. Crop and Resize Images with GD or ImageMagick (v1.1) - Monday By Noon December 17th, 2007

    [...] the past couple weeks, conversation has picked up quite a bit surrounding the Crop & Resize with JavaScript, PHP, and ImageMagick demo I put together nearly [...]

  79. Mehul B Dobariya December 25th, 2007

    can anybody tell me how can i make crop and resize in one page only….

  80. harneet December 25th, 2007

    thanks dude! its really work and i m using it very well

    can u tell me how can i optimize this for best image cropping……

    :)

    again thanks dude

  81. Dennis Aucoin January 14th, 2008

    How do you upload from a link?

  82. cabz January 24th, 2008

    excellent work dude! this is what im looking for.
    btw, is it possible to skip the resize page? how :D
    Thanks!

  83. Avlesh January 25th, 2008

    @Jonathan - Hats off!!! Brilliant script (especially the JS part of it). I am hooked on to it for the last two days.

    @Mehul - I can understand your problem. In fact I faced it myself and none of the solutions specified above were working. When i tried getting into the code it turned out to be a JS error. The JS was not updating the recent coordinates and dimensions to the form fields and thus they were not even being sent to the backend. This seemed to work for me -

    Event.observe(window, ‘load’, function() {
    new Cropper.Img(
    ‘cropImage’,{
    ratioDim: {x:0,y:0},
    minWidth: 200,
    minHeight:200,
    maxWidth:200,
    maxHeight:200,
    displayOnInit:true,
    onEndCrop:onEndCrop,
    captureKeys:true,
    onloadCoords:null
    });} );

    I duplicated all the init parameters being used by Jonathan in his creating the instance method.

    - best
    Avlesh