views:

826

answers:

7

I'm letting users upload a profile picture for themselves on my site. These images are coming in on all different formats and sizes. When I display these profile photos I want them to be of a uniform pixel size without distortion.

Do I have to have the user crop the image right when they upload it?

Given a randomn image, how can I scale it down so it is below a certain pixel height/width while not distorting the images?

+3  A: 

I think PythonMagick will let you do this. It's the Python version of ImageMagick

If you're going to insist on a particular aspect ratio, it'll probably make sense to let the user select a region of the right aspect ratio from their profile photo. For instance, Twitter profile photos are square, 48px by 48px. If you don't let the user choose a region of the right aspect ratio, then the image will be squished when you scale it down.

Security note: It's worth mentioning that always running user-uploaded images through a thumbnailing program is a good idea, as it's possible for malicious users to embed information in various image file types that can lead to security holes. There have been a series of these discovered over the last several years, and it's reasonable to expect that there are more. Another good practice is to serve user-supplied content from a non-trusted domain, so malicious code uploaded by users can't steal your application's cookies.

aem
+3  A: 

You can just use PIL, and Image.thumbnail method.

Roberto Liffredo
+2  A: 

Use Django-Sorl, which will give you tags that automatically generate and cache thumbnails are different sizes, including scaling and smart-cropping. I use this, placing the files and thumbnails at a local S3-mount, and it puts all the thumbnails automatically in place where the users can grab them from S3.

ironfroggy
+1. sorl-thumbnail is a fantastic app, goes in every Django project I do.
Carl Meyer
+1  A: 

If Flash is an option, then it is possible to read the image into the Flash movie (using the FileReference class), crop/resize it there, then encode the image into the desired image format (there's some encoders for PNG and JPEG in the AS3 corelib package) then upload it to the server.

Note: This can only be done in Flash 10.

For more info check out http://labs.findsubstance.com/2008/04/03/as3-upload-encode-images/

Cameron
+5  A: 

Why not use the django-avatar project?

They have a pretty neat solution with a tag that resizes the image before displaying it first time. You store the original image and define the image sizes you accept on the website and the rest is done automagically for you.

On Filmaster we also cache the avatars. You can take a look at the template tag code here: https://musielak.eu/public/film20/film20/userprofile/templatetags/avatars.py (user: justlookingaround, pass: film@ster).

michuk
+1  A: 

I've used a couple different apps to do this:

  • sorl.thumbnail: Simple enough interface and makes your tags for you (which is handy in the admin). I don't like that image sizes and specs get defined on the model attribute, which makes for cluttered code and muddles the separation of design and data.
  • Photologue: Image sizes get defined in the admin and stored in your database. That's handy. Upload zip files into galleries. Lots of good stuff. Maybe more than you need.
  • ImageKit: The next version of Photologue is being built on this. It's lower level, defines image specs (sizes and processors) in a specs file. It doesn't do more than it needs to but seems to do enough.

Like I said, I've used all three for different projects (in the order listed, coincidentally). For what it sounds like you're doing, I'd go with Sorl or ImageKit.

Chris Amico
Thanks for the great information!
MikeN
+1  A: 

... Flash?? Talk about taking a stroll around a mountain just to cross a pond.

Python has an imaging library, called PIL. You've got to download it yourself, but it doesn't get much simpler than that, unless you want an app like django-avatar to take care of some extra stuff for you.

http://www.pythonware.com/library/pil/handbook/image.htm

It's seriously that easy! Even converting image types is as easy as a single string argument to the 'save' method.

Tim
It's important to note that using Flash pushes the CPU load onto the uploader, whereas every other method uses the server. It will probably save a good chunk of bandwidth too, since you're no longer directly accepting 11 megapixel images for from your users for the purpose of avatar pics.It depends on your needs. Facebook has used Java applets and Flash for this exact purpose before they got really huge.
Matt Garrison
Fair enough, but that's honestly not the most direct answer one could give an amateur programmer asking about how to resize an image. It's not practical to expect one person to know how to program in 4 languages proficiently enough to not have security holes in each one. Flash is anything but bulletproof these days, in terms of security accusations. I'd prefer to stick with answering the question asked.
Tim