note 70082 added to function.imagecreatefromgif

From: Date: Mon, 02 Oct 2006 17:48:38 +0000
Subject: note 70082 added to function.imagecreatefromgif
Groups: php.notes 
Request: Send a blank email to php-notes+get-117955@lists.php.net to get a copy of this message
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"; } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 12.111.169.242 ---- Manual Page -- http://www.php.net/manual/en/function.imagecreatefromgif.php Edit -- https://master.php.net/note/edit/70082 Del: integrated -- https://master.php.net/note/delete/70082/integrated Del: useless -- https://master.php.net/note/delete/70082/useless Del: bad code -- https://master.php.net/note/delete/70082/bad+code Del: spam -- https://master.php.net/note/delete/70082/spam Del: non-english -- https://master.php.net/note/delete/70082/non-english Del: in docs -- https://master.php.net/note/delete/70082/in+docs Del: other reasons-- https://master.php.net/note/delete/70082 Reject -- https://master.php.net/note/reject/70082 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#117955) next »