I'm feeling particularly altruistic today, so here is the code to accompany the text I submitted about a script that 'intelligently' detects if an uploaded GIF has transparency or not, and creates a thumbnail accordingly:
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
{
$note = "Image was successfully uploaded.\n";
// Now create thumbnail version of the uploaded image and save it to
// folder "$fixPath/images/thumbs". First, we need to create a
// resource "handle" for a copy of the uploaded image
$img_error = false;
list($width, $height, $type) = getimagesize($uploadfile);
switch($type) // Allowed types: image/gif,image/jpeg,image/x-png
{
case 1: // GIF
$src = @imagecreatefromgif($uploadfile);
if (!$src)
$img_error = true;
break;
case 2: // JPEG
.
.
.
case 3: // PNG
.
.
.
default:
$img_error = true;
}
// resource handle for source image now in $src
// Now calculate scaling factor and put in $sf
if(!$img_error)
{
if($width >= $height && $width > 100)
$sf = $width / 100;
elseif($height > 100)
$sf = $height / 100;
else
$sf = 1;
$newwidth = intval($width / $sf);
$newheight = intval($height / $sf);
switch($type) // Allowed types: image/gif,image/jpeg,image/x-png
{
case 1: // GIF
$tpcolor = imagecolorat($src, 0, 0);
// in the real world, you'd better test all four corners, not just one!
$dest = imagecreate($newwidth, $newheight);
// $dest automatically has a black fill...
imagepalettecopy($dest, $src);
imagecopyresized($dest, $src, 0, 0, 0, 0, $newwidth, $newheight,
$width, $height);
$pixel_over_black = imagecolorat($dest, 0, 0);
// ...but now make the fill white...
$bg = imagecolorallocate($dest, 255, 255, 255);
imagefilledrectangle($dest, 0, 0, $newwidth, $newheight,
$bg);
imagecopyresized($dest, $src, 0, 0, 0, 0, $newwidth, $newheight,
$width, $height);
$pixel_over_white = imagecolorat($dest, 0, 0);
// ...to test if transparency causes the fill color to show through:
if($pixel_over_black != $pixel_over_white)
{
// Background IS transparent
imagefilledrectangle($dest, 0, 0, $newwidth, $newheight,
$tpcolor);
imagecopyresized($dest, $src, 0, 0, 0, 0, $newwidth,
$newheight, $width, $height);
imagecolortransparent($dest, $tpcolor);
imagegif($dest, "$fixPath/images/thumbs/$userfile");
}
else // Background (most probably) NOT transparent
imagegif($dest, "$fixPath/images/thumbs/$userfile");
break;
case 2: // JPEG
.
.
.
case 3: // PNG
.
.
.
break;
}
imagedestroy($src);
imagedestroy($dest);
}
else
$note = "Error: Uploaded image but could not create thumbnail";
header("location: $fixPath/index.php?next=$next&nb=$nb&userfile=$userfile");
}
else
$note = "Could not find anything to upload\n";