/**
* This script will extract a thumbnail that may be imbedded into an image. If such
* a thumbnail has not been found, a new thumbnail will be created.
*
* Since the GD library used in PHP image handling functions does not provide support
* for saving a file as a GIF image, when a thumbnail is created from a GIF file it
* will be saved as a JPG instead..
*/
echo '
| Files Uploaded |
| File Name |
File size |
';
$file = $_FILES['userfile'];
$k = count($file['name']);
$imageDir="images";
for($i=0 ; $i < $k ; $i++)
{
$userfile_name = $file['name'][$i];
if(!isset($file['tmp_name'][$i]) || $file['tmp_name'][$i] == '')
{
continue;
}
if(move_uploaded_file($file['tmp_name'][$i],"$imageDir/". $file['name'][$i]))
{
/*
* let's start by identifying the image type. No doubt the more efficient way
* is to use string functions but who cares?
*/
$parts = split("\.",$file['name'][$i]);
$ext = $parts[count($parts)-1];
$thumb_name = array_slice($parts,0,count($parts)-1);
$ext = strtolower($ext);
switch($ext)
{
case "jpg";
/*
* sometimes we may find that the image already contains an
* embedded thumbnail. Then we simply extract that.
*/
$thumb_data = exif_thumbnail("$imageDir/". $file['name'][$i]);
$thumb_name = join(".",$thumb_name) . "-t.jpg";
if($thumb_data)
{
$fp = fopen("$imageDir/thumb/$thumb_name","wb");
fputs($fp,$thumb_data);
}
else
{
/*
* tough luck here comes work.
*/
$src_img=ImageCreateFromJpeg("$imageDir/$userfile_name");
}
break;
case "gif":
$src_img=ImageCreateFromGif("$imageDir/$userfile_name");
$thumb_name = join(".",$thumb_name) . "-t.jpg";
break;
case "png":
$thumb_name = join(".",$thumb_name) . "-t.png";
$src_img=ImageCreateFromPng("$imageDir/$userfile_name");
break;
}
/* get it's height and width */
$imgSx = imagesx($src_img);
$imgSy = imagesy($src_img);
if($imgSy != 0)
{
/*
* lets calculate the aspect ratio and the height
* and width for the scaled image.
*/
$ratio = $imgSx/$imgSy;
if($ratio > 1)
{
$new_imgSx = 150;
$new_imgSy = 150/$ratio;
}
else
{
$new_imgSx = (float) 150 * $ratio;
$new_imgSy = 150;
}
$dst_img=imagecreatetruecolor($new_imgSx,$new_imgSy);
/* create the scaled instance */
ImageCopyResampled($dst_img,$src_img,0,0,0,0,$new_imgSx,$new_imgSy,$imgSx,$imgSy);
/* write the damned thing to disk */
if($ext == "jpg" || $ext == "gif")
{
imageJpeg($dst_img,"$imageDir/thumb/$thumb_name");
}
else
{
imagePng($dst_img,"$imageDir/thumb/$thumb_name");
}
}
echo '' . $file['name'][$i] ." | \n";
echo '' . $file['size'][$i] ." | \n";
}
else
{
echo "sorry hoss";
}
}
?>