Image Upload and Resize

Quick Links

Sneak past the php upload limits.

J2ME Development with the 6600.

The PHP Java Extension Logo

One of the most common uses of HTTP file uploads is to upload images from a client's computer to the web server. This is almost invariably followed by the generation of a thumbnail. As the author of Mega Upload I have often been asked if I can recommend such a thumbnail generation script or have one of my own. Now I do.

It's possible to generate thumbnails either by using the GD extension or with ImageMagick. ImageMagic certainly is the more sophisticated and verstaile system but that's not what we are going to look at. We will generate the thumbnails with GD since it's easier.

The first step is to determine what type of file has been uploaded. The simplest (though not the most defensive) approach is to inspect the file extension. If we have an jpeg in our hands, chances are the jpeg may laready contain an embedded thumbnail.

	$thumb_data = exif_thumbnail("$imageDir/". $file['name'][$i]);
	$thumb_name = join(".",$thumb_name) .  "-t.jpg";
		$fp = fopen("$imageDir/thumb/$thumb_name","wb");

Why upload and resize when you can resize and upload?

If the image happens to be a GIF, the GD library will refuse to produce a gif file as output, however it does not object to reading from a GIF file. So when a GIF file is uploaded we will create a jpeg thumbnail.

One last point worth mention is that imagecreate() is dicey and you should use imagecreatetruecolor() instead. Similarly it's better to use imagecopyresampled() instead of imagecopyresized(). So we have:



Surely you are bored with it by now, so here is the full script copy it to your server and have fun. You can use it with or without Mega Upload


Mega Upload :: perl edition  :   JSP edition  :   PHP edition  :   Documentation

Copyright © Raditha Dissanayake 2013