views:

180

answers:

7

Hi,

User of my site upload images which are used as avatars. I have set a upload limit size of 2 MB. At most places I only require thumbnails. But users upload images with far bigger resolutions. I store these files on my file-system.

How can i create thumbnails and store them instead of large sized files?

A: 

If you have Imagick installed in your PHP setup. Then there is a function that does this. Imagick::thumbnailImage()

If you don't have Imagick, then the GD library will come in handy. Since they have imagcopyresized()

Ólafur Waage
+3  A: 

Hi,

Depending on your installation / server setup, you have several possibilities.

I would say that I've quite never seen a server without GD installed -- for Imagick, its less likely to be installed by default :-(


Another solution might be to call the convert command-line utility (it comes with ImageMagick -- independantly of any PHP extension), using something like exec to call it.

Advantage, with that, would be that you wouldn't be limited by memory_limit, as the resizing would be performed by an external tool -- but, of course, it also means that your application will rely on an external tool, which is not always nice...


If necessary, there are plenty of tutorials about GD ; for instance, those might interest you :

Pascal MARTIN
+1  A: 

Check out the gd library, specifically the imagecopyresized function

tschaible
+3  A: 

You can use the GD module or the ImageMagick module to resize and shrink any uploaded images.

If you google around for something like "PHP image resizer", you'll find lots of examples. I tend to use GD, as I have an old bit of code kicking around that works just fine. Assuming you have a known uploaded jpeg image found at $srcImgPath, you could do something like the following, where $newWidth and $newHeight are the new sizes of image you want:

list($width, $height, $type) = getimagesize($srcImgPath);
$srcImg = imagecreatefromjpeg($srcImgPath);
if ($srcImg === false) return false;
$workImg = imagecreatetruecolor($newWidth,$newHeight);
imagecopyresampled($workImg,$srcImg,0,0,0,0,$newWidth,$newHeight,$width,$height);
imagejpeg($workImg,$newFilename,$quality);

Functionalize as appropriate, and be sure to specify the $quality. You can abstract this code out to handle gifs and pngs very easily as well.

zombat
A: 

Isn't Google AppEngine fitted with a subset of PIL? There is the 'resize' function that could be use... and best of all, you are getting a free quota!

jldupont
But I am not using Google AppEngine.
AJ
It was just a suggestion... free cycles are always cool, no?
jldupont
+1  A: 

Just a quick note, if you go down the GD route, use imagecopyresampled, as it produces cleaner looking images. By that I mean it won't look as grainy and/or pixely.

Link to the PHP manual on the function: http://us2.php.net/manual/en/function.imagecopyresampled.php

CoryC
+1  A: 

I would recomend the timthumb.php script. it is rock solid. http://www.darrenhoyt.com/2008/04/02/timthumb-php-script-released/

superUntitled