2009
06.04

For quite a long time now ive been seeing lots of image upload scripts which boast to be able to upload multiple images at a time! 90% dont work.

So heres one I created and I know works. Firstly the below code is the functions to handle the images,  you do not need to touch anything here, just make sure its on your page.

[sourcecode='php']

//Dave Hewards image script

function resize($img, $thumb_width, $newfilename)
{
$max_width=$thumb_width;

//Check if GD extension is loaded
if (!extension_loaded(’gd’) && !extension_loaded(’gd2′))
{
trigger_error(”GD is not loaded”, E_USER_WARNING);
return false;
}

//Get Image size info
list($width_orig, $height_orig, $image_type) = getimagesize($img);

switch ($image_type)
{
case 1: $im = imagecreatefromgif($img); break;
case 2: $im = imagecreatefromjpeg($img);  break;
case 3: $im = imagecreatefrompng($img); break;
default:  trigger_error(’Unsupported filetype!’, E_USER_WARNING);  break;
}

/*** calculate the aspect ratio ***/
$aspect_ratio = (float) $height_orig / $width_orig;

/*** calculate the thumbnail width based on the height ***/
$thumb_height = round($thumb_width * $aspect_ratio);

while($thumb_height>$max_width)
{
$thumb_width-=10;
$thumb_height = round($thumb_width * $aspect_ratio);
}

$newImg = imagecreatetruecolor($thumb_width, $thumb_height);

/* Check if this image is PNG or GIF, then set if Transparent*/
if(($image_type == 1) OR ($image_type==3))
{
imagealphablending($newImg, false);
imagesavealpha($newImg,true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefilledrectangle($newImg, 0, 0, $thumb_width, $thumb_height, $transparent);
}
imagecopyresampled($newImg, $im, 0, 0, 0, 0, $thumb_width, $thumb_height, $width_orig, $height_orig);

//Generate the file, and rename it to $newfilename
switch ($image_type)
{
case 1: imagegif($newImg,$newfilename); break;
case 2: imagejpeg($newImg,$newfilename);  break;
case 3: imagepng($newImg,$newfilename); break;
default:  trigger_error(’Failed resize image!’, E_USER_WARNING);  break;
}

return $newfilename;
}
[/sourcecode]

The next part is the important part which you will need to change some variables on to customise them for your own site.

[sourcecode='php']

//This stuff is outside of the function. It operates with our images
if(isset($_POST['submitimages'])){
$success=0;
$imgNumb=1; //This the “pointer” to images
$DestinationDir=”/sites/mysite.com/http/uploads/profiles/full/”;  //Place the destination dir here
$ThumbDir=”/sites/mysite.com/http/uploads/profiles/thumbs/”;  //Place the thumb dir here

do{
if($_FILES["img$imgNumb"][tmp_name]!=”){
$success++;
$Unique=microtime(); // We want unique names, right?
$destination=$DestinationDir.md5($Unique).”.jpg”;
$thumb=$ThumbDir.md5($Unique).”.jpg”;

$IMG=getimagesize($_FILES["img$imgNumb"][tmp_name]);
$finalimage = resize($_FILES["img$imgNumb"][tmp_name], 199, $destination);
//use the filename variable below to record the image name entered so that you can then use this variable to update your database if

$filename = md5($Unique).”.jpg”;

$field = ‘img’.$imgNumb;

//mysql update your database fields

mysql_query(”update profile set $field = ‘$filename’ where playerid=’$id’”);

}

$imgNumb++;
} while($_FILES["img$imgNumb"][name]);

if($success>=1){
$message = “Images uploaded!”;

}
}

[/sourcecode]

Finally you will need a little bit of HTML code for your form for the user to submit his/her new images to the site.

[sourcecode='html']

Change Profile Images:
Image 1:
Image 2:
Image 3:

[/sourcecode]

This code is tried and tested and it works so please enjoy and comment if you have some suggestions for ways to improve it further.

2 comments so far

Add Your Comment
  1. Hi, this code is working great. I am just wondering why $DestinationDir is populated with thumbnails and nothing goes to the $thumb folder?

    Can this script create one thumb and one image with set size?

    Thanks
    Stefan

  2. Hi stefan, you should be able to do this simply by using the variable in the second part called $finalimage.