note 95654 added to function.imagepng

From: Date: Thu, 14 Jan 2010 06:41:16 +0000
Subject: note 95654 added to function.imagepng
Groups: php.notes 
Request: Send a blank email to php-notes+get-164958@lists.php.net to get a copy of this message
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)); ---- Server IP: 117.55.229.1 Probable Submitter: 59.167.220.210 ---- Manual Page -- http://www.php.net/manual/en/function.imagepng.php Edit -- https://master.php.net/note/edit/95654 Del: integrated -- https://master.php.net/note/delete/95654/integrated Del: useless -- https://master.php.net/note/delete/95654/useless Del: bad code -- https://master.php.net/note/delete/95654/bad+code Del: spam -- https://master.php.net/note/delete/95654/spam Del: non-english -- https://master.php.net/note/delete/95654/non-english Del: in docs -- https://master.php.net/note/delete/95654/in+docs Del: other reasons-- https://master.php.net/note/delete/95654 Reject -- https://master.php.net/note/reject/95654 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#164958) next »