How to upload & resize images in PHP with thumb creation

by Gabi Solomon

Many times in my coding projects i had to do an upload form that will upload an image. This by it self is not so hard to do, and i bet many of you can write the code in under a minute :) ). But as we all know webspace is not free ( excluding free hosting ) and certainly not unlimited, so we must try and make the best use out of it and most of the time you will need to resize the images uploaded to a certain size, and maybe even make a thumb image.

This where the fun starts, every coder has his own methods of dealing with image resizing, as do I :D .
I came across a class on phpclasses.org called class.upload.php and fall in love with it :) ).

It is the ideal class to quickly integrate file upload and image manipulation in your site. That’s all you need for a gallery script for instance.

It manages the uploaded file and allows you to do whatever you want with the file as many times as you want. If the file is an image, you can convert and resize it, rotate it, crop it in many ways; You can also add borders, frames, bevels, add of text labels and watermarks or apply graphic filters such as contrast or brightness correction, colorization, negative, threshold, greyscale, reflections and more. Transparency and true color are fully supported. JPEG, PNG, GIF and BMP are supported.

Security features and file management functions are provided. The class can also work on local files, which is useful for batch processing images online, and can circumvent open_basedir restrictions. Uploaded files can also be output directly to the browser.

The class is mature and well documented, already widely used around the world. From version 0.25, the class is fully internationalized. It is also compatible with PHP 4 and 5.

Example of usuage

$foo = new Upload($_FILES['form_field']);
if ($foo->uploaded) {
// save uploaded image with no changes
if ($foo->processed) {
echo ‘original image copied’;
} else {
echo ‘error : ‘ . $foo->error;
// save uploaded image with a new name
$foo->file_new_name_body = ‘foo’;
if ($foo->processed) {
echo ‘image renamed “foo” copied’;
} else {
echo ‘error : ‘ . $foo->error;
// save uploaded image with a new name,
// resized to 100px wide
$foo->file_new_name_body = ‘image_resized’;
$foo->image_resize = true;
$foo->image_convert = gif;
$foo->image_x = 100;
$foo->image_ratio_y = true;
if ($foo->processed) {
echo ‘image renamed, resized x=100
and converted to GIF’;
} else {
echo ‘error : ‘ . $foo->error;

Small problems i had with this class

Even though it is a great class that i use every time i need to do an upload, i had a few problems with it, and have toke me a while to solve. As a suggestion for debug do a print of “$foo->log” variable to see what happened during the preparation of the image.

1. Handling uploads from flash uploader

It took me a while to solve this one, since images uploaded with flash were not being re sized, but after a some debugging and searching i managed to figure out the cause: the mime type for the file was application/octet-stream so the class didnt treat it as an image.
You can find it on the class forum

2. Handling very large images

This one really put me to the test, every time i uploaded an image that was large ( above 1,5 Mb ) it just show an empty page, not an error … nothing. Couldn’t figure it out. I looked in the php error log … nothing. After like 2 hours of almost throwing my monitor out the window i managed to find a solution. It was because of the server, more exactly the memory limit. During the processing of large images, the script would use a lot of memory which would cause the script to stop once it reached the limit with no error message.
Add this in your script:
ini_set ( “memory_limit”, “40M”)

Or in a .htaccess file:
php_value memory_limit 40M

That is it i dindt had any more troubles with this class, it is a pretty stable class witch didnt gave me to much trouble.

I hope you will enjoy it too.