note 95654 deleted from function.imagepng by danbrown
| From: | danbrown@php.net | Date: | Thu, 14 Jan 2010 15:24:38 +0000 |
| Subject: | note 95654 deleted from function.imagepng by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-164978@lists.php.net to get a copy of this message | ||
Note Submitter: hood.id.au
----
matt at mattbostock dot com had a good idea (adding a "background color" to the alpha
channel png so that IE6 will show a colour you pick and not just grey) but his sample code was a
little wrong. I've made some changes to it and the following code works for me, but there are
still some optimisations that could be made.
<?php
$bgColor = array (255,255,255); // white
$pngData = bin2hex(file_get_contents($write_file));
$pngDataOut = substr($pngData, 0, 16);
// TODO: verify this is the correct PNG header
$pngData = substr($pngData, 16);
$fs = 100; // prevent infinite loop
while (--$fs>0 && $pngData!='')
{
$chunkName = substr($pngData, 8, 8);
$chunkSize = substr($pngData, 0, 8);
$chunkSizeNumber = base_convert($chunkSize, 16, 10) * 2;
if ($chunkName=='49444154')
{
// here we have found the first IDAT so we insert our bKGD
$bkgdChunk = '624b4744';
foreach ($bgColor as $bit)
{
$bkgdChunk .= sprintf('%04x',$bit);
}
$bkgdChunk .= sprintf('%08x', crc32(pack('H*', $bkgdChunk))) . '';
$pngDataOut .= '00000006' . $bkgdChunk;
break;
}
// TODO: omit any other bKGD chunks
$pngDataOut .= substr($pngData, 0, 24+$chunkSizeNumber);
$pngData = substr($pngData, 24+$chunkSizeNumber);
}
$pngDataOut .= substr($pngData, 0);
file_put_contents($write_file, pack('H*', $pngDataOut));