The pnggif.php file, something like the code snippet below, will send the browser somefile.gif if it exists (where somefile.png is the PNG in question). This means you can create a custom GIF replacement for the PNG files. If it does not already exist, the PHP file uses GD2 to automatically create and save the GIF file from the PNG file using transparency dither, because I’m awesome. (Admittedly it should use a better algorithm; this is a simple noise/random dither.) PHP file pnggif.php
:
<?php
/** Server-Side PNG-GIF Conversion for MSIE
*
* Takes a PNG image and if MSIE 5 or 6 is detected, throws a GIF back.
* This is supposed to go in your /images/ directory. It, when paired with
* the proper .htaccess file, will intercept your browser's calls for
* any .pnggif files, which don't really exist on your server, and will
* give your browser the .png file (which DOES really exist on your server)
* unless the browser is Internet Explorer (MSIE) 5 or 6, which suck at PNG,
* in which case it makes & caches, if necessary, and serves up a GIF
* version, which IE should be able to deal with.
*
* @author Alan Hogan
* @version 1.00
* @copyright Alan Hogan, 10 November, 2007
* @site http://alanhogan.com
**/
//$myLocation = "/images/";
$myLocation = "/php-tests/images/";
//Get the path of what was actually reQuested
$reqPath = $_ENV['REQUEST_URI'];
//if($strpos($reqPath, $myLocation) === 0) //ASsume true
//ReLative path
$relPath = substr($reqPath,strlen($myLocation), strlen($reqPath)-strlen($myLocation)-7);
//The 7 above is for .pnggif
$pngRel = realpath(".")."/".$relPath.".png";
$gifRel = realpath(".")."/".$relPath.".gif";
if ((strpos($_ENV['HTTP_USER_AGENT'], 'MSIE 6') !== false
|| strpos($_ENV['HTTP_USER_AGENT'], 'MSIE 5') !== false)
&& strpos($_ENV['HTTP_USER_AGENT'], 'Opera') === false
&& strpos($_ENV['HTTP_USER_AGENT'], 'Gecko') === false
&& strpos($_ENV['HTTP_USER_AGENT'], 'Safari') === false) {
// We are dealing with IE less than 7. Hooray for crap.
if(file_exists($gifRel)) {
header("Content-type: image/gif");
readfile($gifRel);
}
elseif(file_exists($pngRel)){
$im = imagecreatefrompng($pngRel);
$transparentColor = imagecolorallocate($im, 0xfe, 0x3, 0xf4 );
//$ditherHistory = array(); //stores number of times it was transparent
$height = imagesy($im);
$width = imagesx($im);
for($x = 0; $x < $width; $x++){
for($y = 0; $y < $height; $y++) {
$alpha = (imagecolorat($im,$x,$y) & 0x7F000000) >> 24;//127 is completely TRANSPARENT, 0 opaque
//DITHER!
if ($alpha > 3 && (
$alpha >=127-3 ||
(rand(0,127))>=(127-$alpha)
)){
imagesetpixel($im,$x,$y,$transparentColor);
}
}
}
imagecolortransparent($im, $transparentColor);
imagegif($im, $gifRel);//save
header("Content-type: image/gif");
readfile($gifRel); //pass thru to browser
} else {
//Error.
die("ERROR sorry, couldn't find ".$pngRel);
}
} else {
//Hooray! Good browser!
if(file_exists($pngRel)){
header("Content-type: image/png");
readfile($pngRel); //pass thru to browser
}
else {
//error
die("ERROR sorry, couldn't find ".$pngRel);
}
}
Make sure to customize the $myLocation variable above, just as with the .htaccess file!
Please note how we not only check for existence of "MSIE 5" or "MSIE 6" in the user agent string, but also for "Opera," as such browsers can identify as IE for moronic websites which exclude non-IE browsers. Still, when “cloaked,” they often still leave a clue to their true identity in their user agent string.