Introducing cfImageCropper - Custom Tag For Client Side Image Cropping

Posted By : todd sharp Posted At : August 2, 2007 9:07 AM Posted In: cfImageCropper, JavaScript, ColdFusion

47

Yesterday I decided to change my headshot on GMail and noticed that when I uploaded my new image there was a slick little tool to crop my headshot. Obviously Google is using some sort of client side JS to do the cropping and then doing the actual image resize on the server (so I assume). I was pretty impressed by this and it got me thinking how simple it would be to throw something together like this for use with the new imageCrop function in ColdFusion 8 (or any image manipulation package for that matter).

Enter the cfImageCropper custom tag. I did a bit of Googling to find a good JavaScript image cropping demo that I could use for the client side. I came across David Spurr's demo and decided to wrap it in a custom tag. The end result in my opinion is pretty damn cool.

Check the demo to see how it works, but it's essentially dead easy. Here's a quick bit of sample code:

<form name="testForm" method="post">
    <cfmodule template="../imageCropper.cfm" imageCropperName="testimage" scriptSrc="/cfImageCropper/lib/" imgSrc="castle.jpg" />
    <br />
    <input type="submit" name="submit" value="Crop This Image!" style="font-weight:bold;" />
</form>

This tag simply creates a croppable image. Drag and drop the crop area to see it work. When the crop is complete the tag updates six hidden form fields that you can then use to crop the image on the server. The form fields will be prefixed with "imageCropperName_" so that you can use the tag multiple times on a page if need be. Take a look at the form dump on my demo to see what fields are created.

Documentation will follow - but for now most of the options listed on David's page are supported as attributes in the tag. The main difference is the variables that he's using as objects (structs) I have broken out into individual prefixed attributes. Check the custom tag itself to see what these are for now.

The really cool thing about this is that you can constrain the maxWidth/maxHeight, aspect ratio, etc in your crop area. Imagine having a site where your users can upload a personal image or headshot that you would like to keep constrained to 150px X 150px. This would be dead simple with this tag and the magic of ColdFusion 8.

Comments (47)

Dave Ferguson's Gravatar This just f-ing rules! Now I just have to fine a use for it.

Robert Owen's Gravatar Wow... That is LEAGUES better than the cropper you have to use for your google image!!

Todd Rafferty's Gravatar Awesome job Mr. Sharp.

Dan Byron's Gravatar AWESOME!!

Gareth's Gravatar Wow! That is really fantastic. Really useful too. It's getting all kinds of praise at my office :)

Dave Farr's Gravatar Just building an image upload with the new cf8 image functions. Your tag will be a great addition. Thanks,

todd sharp's Gravatar If only we could use file upload with Ajax stuff in CF 8... :(

Gary Gilbert's Gravatar Todd,

That is a really sharp custom tag (no pun intended honestly!).

So many new Image functions to play with and so many ways to use them both server side and client side!

Steve Walker's Gravatar Todd,

This is a truly useful tool. Thank you for taking the time to do this.

todd sharp's Gravatar There is another tag in the package that I forgot to mention. I'll blog it later, but if you download and want to use this with CF 8 Ajax stuff (like cfwindow) you can use a cf_ImageCropperScriptImport on your main page to import the JS stuff (in CF8 you need to do this) on the page that calls the window...

And thanks for all the nice comments guys! Drop a line if you use it in your app!

Sam Farmer's Gravatar Awesome

Cory Toth's Gravatar Tags is excellent, Another cool addition would be to have a fixed size selection box where the user simply moves the selection box over the portion of the photo they want to crop.

Just a thought, Great work.

todd sharp's Gravatar That can be done - though my non-existent documentation wouldn't lead you to believe that.

minWidth, minHeight, maxWidth, maxHeight are all optional attributes. Set them all and you've got what you're looking for.

todd sharp's Gravatar Correction: there is a bug with maxWidth, maxHeight, etc...simple fix...working on it...

Adam Fortuna's Gravatar Great work man. Easy to use, responsive interface and fast processing!

todd sharp's Gravatar Cory: Is this what you meant?

http://h127171.cf8beta.com/cfImageCropper/test/tes...

I'll post the bug fix that is necessary for this to work tonight - basically my tag kinda was ignoring a few attributes, but the fix will be in the next zip.

Adrock's Gravatar You sir are a gentleman and a scholar!

Bad Ass!

Viva RiaForge!

Michael Sharman's Gravatar Outstanding Todd, nice work!

Jason's Gravatar This is awesome..Is there any way to get it to work on CF MX7? I definitely want to use it but don't plan to upgrade for about 3 months...

Thanks!

todd sharp's Gravatar The tag itself should work with 7 - the image cropping action is made easy with 8, but it's not necessary to use 8 to crop the image on the server.

To do that check out Rick Root's imageCFC or Doug Hughes Alagad Image component. They should be able to handle the cropping AFAIK.

Clark Valberg's Gravatar Very well crafted!

So useful I might just toss it into a client's site without asking them :)

James Netherton's Gravatar Excellent work!

I can think of quite a few uses I'd have for this :)

Alvaro's Gravatar Hi, thanks for it. I have a question, if some way to do it in PHP? because i dont know that CFM language, and i need do the same that gmail (like you say). And need crop a image and upload this new image. But i can't understand you source code of .cfm

Obouillaud's Gravatar Do you have any idea on how to make an "automatic cropping" for example to remove white space around an image ?

I think about loops with a test of the color of each pixel in a row/column and remove them if they are all white, but CF8 does not seems to offer this (retrieve the color of an x/y pixel).

Radoslaw Prabucki's Gravatar But how to crop rotated image?
It will be wonderful when we would to rotate an image in background, set wrap pattern and then upload and crop with rotation (and maybe other graphic processes available by class.upload.php). My wisdom lvl is to basic to edit cropper.js script to make that change (a know only how to rotate image without cropping mask recalculating).

SneG's Gravatar Excellent work!
tnk!!!

Martin Parry's Gravatar Wicked - I might integrate this into my Auction system

patrick's Gravatar Why dont you have an auto preview in this tag? I modded your tag to add this if you want to include it just let me know.

todd sharp's Gravatar Patrick: Send me what you've got I'll take a look.

Thnx.

Raj's Gravatar Very good job. I will integrate it into my e card website.

todd sharp's Gravatar Raj - your e card website is PHP - how are you going to integrate my custom tag? Or were you just referring to the JavaScript?

Ben's Gravatar Nice Job! One small problem... I use this tag multiple times on the one page using a unique name for each. However, only the last image crop works. Do you have a demo of this tag working when used more than once on a page?

David Spurr's Gravatar I was just following some referrers to my site (it's been a while since I have) and came across this.

Looks like a nice implementation of my cropper UI, we used Coldfusion at work and that's where the cropper idea first came from - I did the cropper in my spare time but the integration with CF at work so I couldn't open that code up like I could with the UI.

I've bookmarked this page as part of V2 of the cropper I want to have a gallery of back-end implementations that work with my cropping UI. Let me know if you'd be interested in that.

szitakalman's Gravatar Great work man. Really easy to use this.

Alex Griffin's Gravatar Just building an image upload with the new cf8 image functions. Good works

Don't want to say's Gravatar Apparently I am the dumbest programmer on the planet. I have two issues....but first of all thank you for a good looking tag.

My first issue is that I get an error that I can't nest a cfinput inside a form tag. So I change the form from a form to a cfform. Then it works but I am unable to conjure up the crop window. I downloaded your tag from RIAforge and added the lib js files from the demo.

I have worked on this thing for a few hours and broke my entire site once but that was sort of unrelated. Anyway if you could help out I would appreciate it.

http://www.mediadeliver.com/cfImageCropper/test/te...

Don't want to say's Gravatar Apparently I am the dumbest programmer on the planet. I have two issues....but first of all thank you for a good looking tag.

My first issue is that I get an error that I can't nest a cfinput inside a form tag. So I change the form from a form to a cfform. Then it works but I am unable to conjure up the crop window. I downloaded your tag from RIAforge and added the lib js files from the demo.

I have worked on this thing for a few hours and broke my entire site once but that was sort of unrelated. Anyway if you could help out I would appreciate it.

http://www.mediadeliver.com/cfImageCropper/test/te...

Vid's Gravatar Nice work Todd and David. A great cfc for a great tool!
Putting cfImageCropper to good use has been on my todo list for a year (jsCropper for two).

I just finished implementing it and I wanted to pass on some tidbits to fill in a gap or two. [Don't want to say], this should help;
1. The cfImageCropper zip doesn't contain all the files you need.
2. The docs refer to the optional scriptSrc attribute which points to the required javaScript docs you need; those files are in the lib folder of David Spurs' jsCropperUI-1.2.0.zip.
3. The test.cfm demo file holds the key to the last missing piece: cropper.css. Todd's test page points to the lib/ folder for it and David has it in the main directory. Certainly it can go anywhere but I dropped in the lib folder for the test and all was right in the world of cropping castles.
So to sum up; Your lib file should contain these 7 files:
builder.js,
cropper.js,
dragdrop.js,
effects.js,
prototype.js,
scriptaculous.js
&
cropper.css.

And not:
controls.js,
cropper.uncompressed.js,
slider.js
&
unittest.js

Russ Hall's Gravatar Don't want to say,
I've gone through about the same batch of issues. I think I'll probably get it figured out tomorrow though. Either way, I'll post a comment in the next couple days about any progress. Hope you haven't given up entirely!

Don't want to say's Gravatar Sorry for the delay.

Follow Vid's instructions to a T and it will work. Very strange that deleting js files makes it work but it does.

My link doesn't go to a working page. I moved it somewhere else and got it up and running. I will come back here and post a working version if I get a chance.

Kevin Fricke's Gravatar How do i turn on the cropped image preview?

Ted Daniels's Gravatar Came across your blog when figuring out how to crop, resize and composite legal documents. Given that docs are normally scanned at 300dpi, or higher, the original image has much higher res than on screen. How about doing the crop like you do, then display the crop at original res or at user defined res?.

That way one could display a complete doc, do your crop mask over, say the signature, display the crop (eg signature) in an overlay window at magnified res.

Like, "Do you recognize this doc?" then, run your crop and display zoomed window, "and is this your signature?"

ramesh's Gravatar i am having aissue with this tag, when i load the edit page the image flickers and disappears , if i click on the the page the crop windows appears ....but once out of 50 times it works right ...help!!

Jennifer's Gravatar First, I want to thank you for this tag. It is exactly what I need! Second, I was hoping you could assist with a problem I am having. It is the same problem as Ramesh, but only in IE and only with specific dimensions. I have two image sizes: 400x600 and 600x400. This problem is only and always occurring with the "tall" images (400x600). The image loads, then disappears. No JS errors -- I have no idea how to troublshoot this. Any ideas??

Jennifer's Gravatar To follow up on my previous comment: I am using IE 8; I have isolated the problem to (I think) cropper.css, the .imgCrop_clickArea style. If I play with the width and height, by changing them to fixed width and height rather than 100%, it fixes the "disappearing" problem (which is really the background-color overwriting the image). However, this causes problems with the selection area handles (the ones across the bottom and along the left move way outside the image)... Next I'm going to look at the .imgCrop_selArea styles that are set in the JS.
Anyone have any ideas?

Jennifer's Gravatar To close this out: Found a solution on another blog. Apparently the CfImageCropper cannot be in a table or IE problems will arise. Fixed my issue.