Smart Multi-Uploader and Thumbnail Creator from GIF/ JPEG/ PNG with PHP

Submitted by n8coder on Sun, 05/17/2009 - 16:37

Image uploader and thumbnail generatorThis is a multiple image uploader with flexible thumbnail generator. You can set how many images you want to upload at once. This script creates thumbnails automagically from GIF / JPEG and PGN images. You can also disable thumbnails completely. You can change easily path/to/upload and thumbnails max width and height. Script dedect automagically which dimestion to use for resize. It checks image-type and max-image-size. Comments and featurequest are welcome!

<?php

/**
* Smart Image Uploader by @cafewebmaster.com
* Free for private use
* Please support us with donations or backlinks
*/

$upload_image_limit = 5; // How many images you want to upload at once?
$upload_dir = ""; // default script location, use relative or absolute path
$enable_thumbnails = 1 ; // set 0 to disable thumbnail creation

##################### THUMBNAIL CREATER FROM GIF / JPG / PNG

function make_thumbnails($updir, $img){

$thumbnail_width = 80;
$thumbnail_height = 60;
$thumb_preword = "thumb_";

$arr_image_details = GetImageSize("$updir"."$img");
$original_width = $arr_image_details[0];
$original_height = $arr_image_details[1];

if( $original_width > $original_height ){
$new_width = $thumbnail_width;
$new_height = intval($original_height*$new_width/$original_width);
} else {
$new_height = $thumbnail_height;
$new_width = intval($original_width*$new_height/$original_height);
}

$dest_x = intval(($thumbnail_width - $new_width) / 2);
$dest_y = intval(($thumbnail_height - $new_height) / 2);

if($arr_image_details[2]==1) { $imgt = "ImageGIF"; $imgcreatefrom = "ImageCreateFromGIF";  }
if($arr_image_details[2]==2) { $imgt = "ImageJPEG"; $imgcreatefrom = "ImageCreateFromJPEG";  }
if($arr_image_details[2]==3) { $imgt = "ImagePNG"; $imgcreatefrom = "ImageCreateFromPNG";  }

if( $imgt ) {
$old_image = $imgcreatefrom("$updir"."$img");
$new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imageCopyResized($new_image,$old_image,$dest_x,
$dest_y,0,0,$new_width,$new_height,$original_width,$original_height);
$imgt($new_image,"$updir"."$thumb_preword"."$img");
}

}

################################# UPLOAD IMAGES

foreach($_FILES as $k => $v){

$img_type = "";

### $htmo .= "$k => $v<hr />"; ### print_r($_FILES);

if( !$_FILES[$k]['error'] && preg_match("#^image/#i", $_FILES[$k]['type']) && $_FILES[$k]['size'] < 1000000){

$img_type = ($_FILES[$k]['type'] == "image/jpeg") ? ".jpg" : $img_type ;
$img_type = ($_FILES[$k]['type'] == "image/gif") ? ".gif" : $img_type ;
$img_type = ($_FILES[$k]['type'] == "image/png") ? ".png" : $img_type ;

$img_rname = $_FILES[$k]['name'];
$img_path = $upload_dir.$img_rname;

copy( $_FILES[$k]['tmp_name'], $img_path );
if($enable_thumbnails) make_thumbnails($upload_dir, $img_rname);
$feedback .= "Image and thumbnail created $img_rname<br />";

}
}

############################### HTML FORM
while($i++ < $upload_image_limit){
$form_img .= '<label>Image '.$i.': </label> <input type="file" name="uplimg'.$i.'"><br />';
}

$htmo .= '
<p>'.$feedback.'</p>
<form method="post" enctype="multipart/form-data">
'.$form_img.' <br />
<input type="submit" value="Upload Images!" style="margin-left: 50px;" />
Powered by <a href="http://cafewebmaster.com">Cafe Webmaster</a>
</form>
';

echo $htmo;

I was busy developing a site that allowed members to upload images for there own galleries. I was really struggling to create gif thumbnails and this script did everything I wanted and was so easy to implement. THANX

Submitted by Anonymous (not verified) on Wed, 06/03/2009 - 03:13

Permalink

Hi! It works great and is easy to use, but the resized images are of really low quality. What am i doing wrong?

Submitted by mehmet ali (not verified) on Wed, 06/17/2009 - 01:04

Permalink

Hi. Firstly thanks for your great code. But i have a problem. With this code , my upload's thumbnail pictures are to low quality. Normal size pictures is the same as before uploading but small pictures is worse.

What must i do to be have hşgh quality thumb pictures.

Thanks to alot.
İf any body wawnt to help me , he can mail me :
malisabuncuoglu@gmail.com

See you soon

Submitted by Anonymous (not verified) on Thu, 06/25/2009 - 14:30

Permalink

just tested with 4 images, 1 gif + 1 png + 2 jpeg and it works. all images uploaded and thumbnails created. check your server + permissions.

Submitted by JJ Web Guy (not verified) on Sat, 10/03/2009 - 13:53

Permalink

How would you go about getting the name of each photo so it can be added to a mysql database? I know how to add single items from a form into a mysql database, but I'm not sure how it would work with the "for" loop you have.

Add new comment