Forum Moderators: coopster

Message Too Old, No Replies

Image Upload & Thumbnail Creation

         

matthew2riches

3:15 pm on Jun 8, 2010 (gmt 0)

10+ Year Member



I am setting up a website for an art gallery and need to upload images to 2 folders, thumbs and full.

I need an uploader script that will generate an image size of 640x480 and a thumbnail of 180x180. The 640 image should go into the 'full' folder and the 180 image should go into the 'thumbs' folder. The name of the file should be the same in both files.

I already have the system setup to display the images, I just need to get the images in there now by a web interface.

Any ideas? Cheers!

jatar_k

3:31 pm on Jun 8, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could take a look at this one
[webmasterworld.com...]

not sure if you need the mysql stuff but you could nuke that, the image and thumbnail creation is there

watch if you are forcing 640x480, if the source image is smaller then you may not want to make it larger, also if the image is larger remember to retain the h/w ratio

rocknbil

5:05 pm on Jun 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



need to upload images to 2 folders, thumbs and full.


The first thing to think about (IMO) if you're doing this in PHP is what library you'll be using. PHP is usually compiled with the GD library for resizing images, but there's two catches.

The first is that by default uploaded files have a 2 MB file size limit (and other defaults, like a timeout limit on uploads, and amount of memory PHP can use.) These aren't so difficult to change, you can modify the limits via php.ini if you have access to it, or per-directory .htaccess directives, which is actually a bit safer.

The problem then becomes the way GD does it's image resizing, which is probably one of the reasons file sizes are limited to 2 MB by default. When you upload, say, a 5 MB image from a digital camera as a jpg, that is it's compressed size. To resize an image, GD constructs an uncompressed bitmap of the image in memory, which can be exponentially larger than 5 MB, and suck up the server memory. This will cause a "memory exhausted" error and the script dies.

You can return to the php.ini or .htaccess to up the amount of memory PHP uses, but 1) in many hosting environments it may not be allowed, and 2) do you really want PHP eating up 128 - 256 MB of memory or more just to upload and resize an image?

The best option is to use ImageMagick with the PHP interface, Imagick. ImageMagick opens a second process on the server separate from PHP, and does it's remapping operations in virtual memory on disk, not in memory. The only real downside there is getting your hosting service to install them.

So before you start, consider a) that you'll have to scale down your images before uploading without large hurdles to jump, or b) work with Imagick instead of GD. There are hundreds of image resizing examples on this site, try a search in Google for PHP thumbnails site:webmasterworld.com to find them.

Here's a resizing snippet from one of mine using Imagick. Notice it bases the "other dimension" proportionate to the width or height, whichever is greater, for both resizings.

// These go in your config as constants.
define('IMG_WIDTH',600);
define('THUMB_WIDTH',150);
// Usage:
// $large_image = full/path/to/images/image.jpg, AFTER move_uploaded_file
// $thumbnail = full/path/to/thumbnail/image.jpg, which does not exist yet
// "$error" is expected to be an empty string '',
// so if $error != '' output an error.
$error = resize($large_image,$thumbnail);


function resize($original,$thumb) {
$resize_error='';
$newwidth=$newheight=$width=$height=$largeTest=0;
$img_width = IMG_WIDTH;
$thumb_width = THUMB_WIDTH;
// Note we create both versions from $image, but
// overwrite $original.
$image= new Imagick($original);
$width = $image->getImageWidth();
$height = $image->getImageHeight();
$type = $image->getImageFormat();
if ((! ($width > 0)) or (! ($height > 0))) {
return "<li>Could not get image size on this image, operation aborted.</li>";
}
if ($resize_error == '') {
if (!
((preg_match("/jp*eg/i",$type)) or (preg_match("/gif/i",$type)) or (preg_match("/png/i",$type)))
) {
$resize_error = "Uploaded images must be in JPG, GIF, or PNG formats only.";
}
}
//
if ($resize_error == '') {
// Sets EITHER the width or height, whichever is greatest
$largeTest = ($height>$width)?$height:$width;
// If it blows up your layout, just use width.
//$largeTest = $width;
if ($largeTest > $img_width) {
if ($largeTest == $width) {
$newheight=intval(($img_width * $height) / $width);
$newwidth=$img_width;
}
else {
$newwidth=intval(($img_width * $width) / $height);
$newheight=$img_width;
}
$image->thumbnailImage($newwidth,$newheight);
$image->writeImage($original);
}
## Thumbnails
if ($largeTest > $thumb_width) {
if ($largeTest == $width) {
$newheight=intval(($thumb_width * $height) / $width);
$newwidth=$thumb_width;
}
else {
$newwidth=intval(($thumb_width * $width) / $height);
$newheight=$thumb_width;
}
$image->thumbnailImage($newwidth,$newheight);
$image->writeImage($thumb);
}
}
$image->destroy();
return $resize_error;
}