If you follow my blog you have probably seen a few previous posts about a plugin I have been working on to use Uploadify, jCrop, jQuery, and Impromptu to upload and crop images. The plugin is relatively small, but ties together other plugins to make things flow smoothly. Here is a video of it in action:
Ok, to get started you will need a copy of Uploadify, Impromptu, jQuery, and UberUploadCropper. Include them all in your page including their default CSS. Now to configure this puppy you can use most all the options for jCrop and Uploadify, so you can tweak how you want to scale, crop, etc.. (trust me when I say there are many ways to approach this, I will let you decide). Lets start with an html file input element and an img to show the preview:
<img src="/path/to/img.jpg" alt="Current Image" style="height: 85px;" id="PhotoPrev" />
<input type="file" id="UploadPhoto" name="UploadPhoto" />
Now to activate the plugin.. (be sure to place this inside of a document load event):
$('#UploadPhoto').uberuploadcropper({
//---------------------------------------------------
// uploadify options..
//---------------------------------------------------
'uploader' : 'jquery.uploadify/uploader.swf',
'script' : 'path/to/upload/script/',
'checkScript':'path/to/check/script',
'cancelImg' : 'jquery.uploadify/cancel.png',
'multi' : false,
'auto' : true,
'folder' : '/path/to/upload/image',
//---------------------------------------------------
//now the cropper options..
//---------------------------------------------------
//'aspectRatio': 1,
'allowSelect': false, //can reselect
'allowResize' : false, //can resize selection
'setSelect': [ 0, 0, 200, 220 ], //these are the dimensions of the crop box x1,y1,x2,y2
//'minSize': [ 100, 100 ], //if you want to be able to resize, use these
//'maxSize': [ 100, 100 ],
//---------------------------------------------------
//now the uber options..
//---------------------------------------------------
'cropScript': '/path/to/crop/script/',
'onComplete': function(imgs,data){
$('#PhotoPrev').attr('src', imgs[0].name +'?d='+ (new Date()).getTime());
}
});
Now the only thing left is the serverside scripts to upload and resize the images. For the most part you can start with the scripts provided with Uploadify, with a couple exceptions. In the upload script you will need to scale down the image to a reasonable size, but maintain the original aspect ratio so it will fit inside an Impromptu window. The second is that you must create a new file to actually crop the image once the selection has been made. For this file all of the information will be passed as a post variable. Since I develop in PHP and I originally intended on being able to upload multiple image and queue them up to crop, I pass an array of properties to this script named “imgcrop”, similar to the following:
imgcrop = [
{ name: 'filename1.jpg', x: 0, y: 0, w: 200, h: 220 },
{ name: 'filename2.jpg', x: 0, y: 0, w: 200, h: 220 },
{ name: 'filename3.jpg', x: 0, y: 0, w: 200, h: 220 }
]
To perform this crop properly you should loop through and resize each image. Performing the crop is super easy with PHP. Check out the docs here.
Unfortunately, there is a bug in the plugin preventing cropping more than one image. This is where I turn to you guys/gals for help. If you enjoy this plugin and are able to make any nice advances with it please feel free to post updates here. My time has been limited lately for development but I will try my best to update the code. The last Uploadify which it was used with was version 1.6.2. I know Uploadify has changed a good bit since then so it might be best to start with the older version and try upgrading. Also feel free to post suggestions as well. Enjoy!
Related posts:


18 Responses
apot
29|Oct|2009 1This is really nice. I am seeing some differences in the new uploadify, still using the old version for now though
VaN
03|Nov|2009 2waiting for the multi-crop fix, and I’ll use it everywhere I can.
VaN
04|Dec|2009 3Trent, do you plan to fix that multi-crop bug, or will this script stay like that for a long time ?
trent
04|Dec|2009 4I’ve been working on it as time permits, however in theory uberuploadcropper should be able to handle it, the jcrop plugin didn’t like multiple instances like that. Maybe it is a bug in my code, also there may be a newer version of jcrop since I last touched it which plays a little nicer.
I will try to take another look at it. It is a plugin I use often so I keep tweaking it, however I don’t have time to dedicate solely to it’s development.
John
26|Dec|2009 5Awesome work! One of the best upload crop scripts I have seen.
I got everything working except for the crop script.
How do you past the crop variables and call for them in my crop script?
Any help would be appreciated. Thanks
trent
28|Dec|2009 6Hey John,
By passing variables do you mean into $(‘#UploadPhoto’).uberuploadcropper({}), or into the onClomplete callback function? For the most part the options passed to uberuploadcropper() are the options available for jcrop and uploadify. I added a couple options for tying the two together like ‘cropScript’ and ‘onComplete’.
The serverside script on the other hand gets its sizes through the POST variable ‘imgcrop’, which is an array of images and sizes.
nodnarboen
25|Jan|2010 7Hi John,
Could you create a demo page for this jquery of yours? would love to see how it’s implemented
trent
29|Jan|2010 8nodnarboen, since this plugin requires uploading of images from the public I’m unable to provide a demo. Sorry.
Robert M. Cavezza
08|Mar|2010 9Interested in more examples. I’m trying to get this to work, but I am having difficulties.
trent
08|Mar|2010 10Hey Robert,
Unfortuantely I don’t have a sandbox type environment available to set this up on. Are you able to get all the scripts on your site and can see the Upload flash button? If so everthing on the client side is installed correctly, the serverside will be all that is left. For uploading the php script Uploadify gives you can upload it, then you will have to use your own image resize.
Ares
10|Mar|2010 11Would you mind publishing all your code!? It does not seem to work even if I followed the instructions.
Anyone got it working!?
trent
10|Mar|2010 12Hey Ares, I developed this while using CakePHP framework, so my entire setup isn’t stand alone. I will try to go through and piece together a stand alone example for a download. Or maybe I can post my cake functions to upload and crop?
Ares
10|Mar|2010 13Hello Trent,
Thank you for the prompt reply. I am trying to do something in ZF or plain PHP, but I believe I will be able to read the code even if developed in CakePHP.
My problem is that I cannot bring together all the JS components in one page to get the result of prompting for a crop.
If you can start with that … would be helpful to me personally.
[code]
Uploadify Example Script
$(document).ready(function() {
$('#UploadPhoto').uberuploadcropper({
//---------------------------------------------------
// uploadify options..
//---------------------------------------------------
'uploader' : '/getperd/uploadify/scripts/uploader.swf',
'script' : '/getperd/uploadify/scripts/uploadify.php',
'cancelImg' : '/getperd/uploadify/cancel.png',
'multi' : false,
'auto' : true,
'folder' : '/home/fserver/public_html/tmp',
//---------------------------------------------------
//now the cropper options..
//---------------------------------------------------
//'aspectRatio': 1,
'allowSelect': false, //can reselect
'allowResize' : false, //can resize selection
'setSelect': [ 0, 0, 200, 220 ], //these are the dimensions of the crop box x1,y1,x2,y2
//'minSize': [ 100, 100 ], //if you want to be able to resize, use these
//'maxSize': [ 100, 100 ],
//---------------------------------------------------
//now the uber options..
//---------------------------------------------------
'cropScript': '/getperd/jcrop/crop.php',
'onComplete': function(imgs,data){
$('#PhotoPrev').attr('src', imgs[0].name +'?d='+ (new Date()).getTime());
}
});
});
[/code]
trent
10|Mar|2010 14Ares, I replied by email
Ashish
11|Mar|2010 15Hi Trent,
Please help me I am trying to combined all plugins but I can’t do it.
trent
11|Mar|2010 16Are you able to see the Upload button?
Ashish
13|Mar|2010 17Yes I can see the upload button but its default browse button
trent
13|Mar|2010 18if you view the source of your page, can you take the url of the javascript files and view those urls in your browser? There should be 4 scripts at least: jquery, jcrop, uploadify, impromptu.
If you can see them then check to make sure your settings in
$(‘#UploadPhoto’).uberuploadcropper({});
are correct (specifically the file paths).
And if those are correct is the input field id the same as the jquery selector? $(‘#UploadPhoto’)
Leave a reply