note 70082 deleted from function.imagecreatefromgif by didou

From: Date: Sat, 13 Jan 2007 20:47:23 +0000
Subject: note 70082 deleted from function.imagecreatefromgif by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-121606@lists.php.net to get a copy of this message
Note Submitter: moxley at moxleydata dot com ---- I wanted to find out if a GIF is Black & White or Color, but I didn't want to wait around for imagecreatefromgif() to parse a 200k file (about 1 second) to get the color map, so I wrote this function to get a list of all the colors in the GIF. Hope it is useful for you. <?php function getGIFColorMap($file) { $fp = fopen($file, 'r'); $buf = fread($fp, 1024); fclose($fp); // Calculate number of colors // buf[10] is the color info byte $color_byte = ord($buf[10]); $has_color_map = ($color_byte >> 7) & 1; $color_res = (($color_byte >> 4) & 7) + 1; $bits_per_pixel = ($color_byte & 7) + 1; if (!$has_color_map) return null; // buf[13] is the beginning of the color map $color_map_index = 13; $map = array(); for ($i=0; $i <= $bits_per_pixel; $i++) { $index = $color_map_index + $i*3; $r = ord($buf[$index]); $g = ord($buf[$index + 1]); $b = ord($buf[$index + 2]); $map[$i] = array($r, $g, $b); } return $map; } $colors = getGIFColorMap("big.gif"); foreach ($colors as $i=>$color) { echo "color[$i]: (red=$color[0], blue=$color[1], green=$color[2])\n"; } ?>

« previous php.notes (#121606) next »