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!
Update: A download example is now available.
Related posts:


50 Responses
apot
29|Oct|2009This is really nice. I am seeing some differences in the new uploadify, still using the old version for now though
VaN
03|Nov|2009waiting for the multi-crop fix, and I’ll use it everywhere I can.
VaN
04|Dec|2009Trent, do you plan to fix that multi-crop bug, or will this script stay like that for a long time ?
trent
04|Dec|2009I’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|2009Awesome 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|2009Hey 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|2010Hi John,
Could you create a demo page for this jquery of yours? would love to see how it’s implemented
trent
29|Jan|2010nodnarboen, since this plugin requires uploading of images from the public I’m unable to provide a demo. Sorry.
Robert M. Cavezza
08|Mar|2010Interested in more examples. I’m trying to get this to work, but I am having difficulties.
trent
08|Mar|2010Hey 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|2010Would 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|2010Hey 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|2010Hello 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|2010Ares, I replied by email
Ashish
11|Mar|2010Hi Trent,
Please help me I am trying to combined all plugins but I can’t do it.
trent
11|Mar|2010Are you able to see the Upload button?
Ashish
13|Mar|2010Yes I can see the upload button but its default browse button
trent
13|Mar|2010if 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’)
Luke
09|Apr|2010Hi Trent. I love the impromptu plugin!
Just wondering if you had any code examples of the cake integration for this one?
do you use a component, I imagine?
Luke
trent
09|Apr|2010Hey Luke, Are you refering to a cakephp component? I use a component for the image manipulations in cake, other than that since the uploads are so customized they are just functions in the controllers
trent
09|Apr|2010sorry i miss read, I want to put together a simple example of the php on the blog soon
shankar
17|May|2010Hi,
Nice code, but it does not support BMP files.
Please let me know if any changes has to be applied in the code.
Thanx
trent
17|May|2010hey shankar,
It should support bmp just fine, I only demonstrated croping jpg for simplicity. UberUploadCropper is a javascript plugin, the backend can be in any language of your choice and resize/crop any image type you choose (jpg, png, bmp, gif, etc.. within Jcrop’s abilities)
shankar
18|May|2010hi trent,
Thanks for ur reply,
But i changed the file ext ‘fileExt’ : ‘*.jpg;*.jpeg,*.bmp’,
It accepted bmp files. uploaded it on specific directory but was unable to load it for doing the process of crop.
If i am not wrong just like u have done for jpeg and gif it has to be done for bmp too.
Secondly how to restrict if i don’t want to upload specific format and file size, any suggestion via this code.
Thanx for u kind reply
trent
18|May|2010Hey shankar, You’ve allowed the fileExt correctly, the sizeLimit option should solve your file size issue. To resize bmp files would be up to which ever serverside language you’re using, and if you’re using a framework it may handle different file types for you. I only demonstrated jpg for simplicity and to show how the jquery plugin works. I am leaving the resizing/cropping different file types up to the individual users :)
shankar
16|Jun|2010hi,
When i upload the image in IE 7 and IE 8 it takes too much time to process. sometimes it gives i/o error or upload double images. and how to use aspect ratio of the images for cropping and allow only specific width and height images.
is it something to do with “minSize” & ‘maxsize’ &’aspectratio’
Thanx
shankar
16|Jun|2010hi,
while uploading i want to check whether the width of image is of allowed width or not.
if not then close the pop up of uploadify.
pls let me know if possible.
trent
16|Jun|2010shankar,
some changes would have to be made to the source, uploadify will let you return a different value after upload (which could be used to indicate error), but the uber (uber=uberuploadcropper) script is programmed to expect a certain value. You would need to change uber to close if it doesnt get back the value expected. this takes place in
var singleUploadComplete = function(event,queueId,fileObj,response,data){
imgdata.push(fileObj);
};
in here you can do $.prompt.close() if you get your error (the data var should have your message from the upload script)
shankar
17|Jun|2010hi trent,
thanx for ur reply.
But as i told it uploads double image in IE 7 and IE8.
secondly if i upload smaller image size of wight height get increase too much as compared to when bigger images are uploaded.
may i am unable to set height of the image properly.
mukesh
20|Jun|2010Hi,
trent nice code, ya while uploading even i get the same issue of double upload in IE7 and iE8.
I have removed the dynamic width and height of the images and removed the static height of the div. it works perfectly fine in all browser but fails in IE7.
Please let us know is there any bug in IE 7
trent
24|Jun|2010Ok, I’ll try to take a look in to whats happening, although I don’t know how much time I’ll have coming up to update it. If anyone finds a solution could you please post it?? (sorry thats probably not the answer you were looking for.. :-/ )
shankar
01|Jul|2010hi trent,
i think if we can use IE7 css hack on
imp.find(‘.jqi’).width(biggestwidth).css(‘marginLeft’,((imp.find(‘.jqi’).outerWidth()/2)*-1));
I have tried to do but failed.
But i am sure its some what here because now i am uploading whole image (larger image) in div but it fails in IE7 and IE8 rest browser it works as expected thx to ur previous comments.
Please let me know if it helps.
Thanx
John
01|Jul|2010Hey trent,
It works perfectly fine on my windows m/c but it fails on my linux m/c.
Whenever i upload images it gives me htaccess dialogue prompt.
Any suggestion for it.
Might be issue of htaccess bcoz it works on my frnds m/c who has not introduce htaccess in its website
trent
01|Jul|2010shankar, can you tell exactly which part is failing? The width assignment or the centering of the prompt?
Hey John, that does sound like an htaccess issue. however, linux is case sensitive, are all your paths the correct case? Is your htaccess like the on on your friends? do you have any specific file handling commands in your htaccess?(like telling the server how to handle certain file types)
peck
12|Jul|2010hello trent,
i follow all your indications to use uberuploadcropper , but i’m not able to see the swf button witch must replace the
First Into a dialogbox (jquery ui) … thinking it was not possible I try in a test.html but I’ve got the same Problem.
I sure of my files path (i used uploadify with the sames and i’ve got no problems)
If you can help me it would be nice !
(excuses for my bad english i’m french ^^)
shankar
19|Aug|2010Hi trent,
I gave hard coded width and height to the div handling the images.
Since, when i use to upload images with bigger size it use to acquire whole page.
So, i had to use fix width and height and rest portion is shown via scroller in the div.
But now the issue use to arise that it displays bigger div height as compared when uploaded small size images i was able to handle width of the div but not height.
$.each(imgdata,function(i,obj){
var btn = {};
var str = ”+
—-
—-
shankar
19|Aug|2010srry on my above full cdoe was not displayed
‘+
johnM
24|Sep|2010hello and thanks for the great plugin.
any idea to fix the image-crop display if it is uploaded an image bigger than screen width or height ?? It would be great if it could be resized to fill completely in the screensize..
One more question:
I would edit this line: $(‘#PhotoPrev’).attr(‘src’, imgs[0].name +’?d=’+ (new Date()).getTime()) to change image path using a PHPsession value generated by crop.php file. Is it possible?
Thanks for help and sorry for engl but i’m from ita.
jM
trent
24|Sep|2010JohnM,
The first question: On upload you can check the size of the image. If you need to resize it you could do it on upload. Javascript could pass over the screen width/height. Also you might would make a copy of the original file if you needed the original size after the crop.
The second question: The way I have it working is just to directly link to the image, so php session would be irrelevant. but this is entirely up to the developer to how this works. But yes you could just add the session variable to that URL.
Also speaking of session variables sometimes because uploadify uses flash php will kick off a different session. This can cause issues if you are in a logged in area. Flash’s new session in your application may request login.
Leigh
29|Sep|2010Great script and thanks for sharing this with the community!
Just a couple of questions –
How would I save the original image as well as the cropped image
Can I upload another image after the first and also display all images that have been uploaded
Is there an option to limit image uploads (to say 4 images)
Thanks again for all the time you have put into this plugin!
trent
29|Sep|2010On the php side (or which ever serverside lanuage you use) just make a copy of the file before you resize it.
Leigh
29|Sep|2010Hey Trent, thanks for the fast reply. I have worked out how to display all images using.append to a div in the form, also saving the two images and limiting upload.
I am struggling with the file rename. I’m able to create a new file name in uploadify.php but this is not being applied to the cropped image for some reason.
Where would you create the new file name (just timestamp + original filename)?
Thanks again
trent
30|Sep|2010I think what you need will require a small change to uberuploadcropper.js. I started a change like this some time ago, but never completed it. When you upload (with uploadify) there is a callback, which catches from uploadify the name of the file uploaded. You can see the notes on it here under the onComplete function:
http://www.uploadify.com/documentation/
Currently the uberuploadcropper.js gets the filename from the fileObj parameter. If in your php you pass back the new file name as a string or json, it should be available as the “response” parameter. This is all found in the “singleUploadComplete” function. You might change the function to this:
var singleUploadComplete = function(event,queueId,fileObj,response,data){
fileObj.name = response; //the new name you just passed back from the server
imgdata.push(fileObj);
};
Please let me know if you’re able to successfully make this change, I would like to roll this into the library at some point.
Alidad Modjtabai
23|Feb|2011Hi, i need your help with this, i used your scripts and is looks great, but jcrop is not working on my site. please take look the sample site and see the demo what they do.
Can you please provide some some tip how can i solve this please thanks.
please leave subject “jcrop” for me to get attention if you are going to reply it thanks.
AM
mrx02xx
04|Mar|2011Hi, i need also help for this. The Problem is the same as Alidad Modjtabai.
Jcrop is not working on my site.
Thanks
Katrina
20|Nov|2011I am looking for something similar but I need multiple image uploads and crops :( on my video collection site.
Gudm Th Egilsson
18|Dec|2011Hi, i need also help for this. The Problem is the same as Alidad Modjtabai and mrx02xx
Jcrop is not working but the upload is ok
Thanks
Gudm.
trent
18|Dec|2011I have just completed a version 2 of uberuploadcropper. You can find it on github:
https://github.com/trentrichardson/UberUploadCropper
I will be writing more about this new version this week. Try it and see if it works for you.
Tarcisio Fonseca Lima
16|Jan|2012Guys, i need some help here! I want to use that plugin, i install and it’s work. But if i upload a image with a big resize, the Impromptu create a pop up that not display all in the screen and the image get cut!
trent
16|Jan|2012Hey, I have released a newer version of the plugin which addresses this problem. Here is a post, it includes examples and a video describing the solution to your problem:
http://trentrichardson.com/2011/12/20/uberuploadcropper-v2-upload-crop-no-flash/
Hope that helps