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:


37 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’)
Luke
09|Apr|2010 19Hi 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|2010 20Hey 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|2010 21sorry i miss read, I want to put together a simple example of the php on the blog soon
shankar
17|May|2010 22Hi,
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|2010 23hey 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|2010 24hi 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|2010 25Hey 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|2010 26hi,
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|2010 27hi,
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|2010 28shankar,
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|2010 29hi 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|2010 30Hi,
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|2010 31Ok, 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|2010 32hi 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|2010 33Hey 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|2010 34shankar, 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|2010 35hello 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|2010 36Hi 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|2010 37srry on my above full cdoe was not displayed
‘+
Leave a reply