3 // Simple .ICO parsing. The ICO format is insanely complex and this may
4 // fail to correctly handle some technically valid files, but it works
5 // on the majority I've found.
7 // jimIcon was written in 2013 by Jim Paris <jim@jtan.com> and is
8 // released under the terms of the CC0:
10 // To the extent possible under law, the author(s) have dedicated all
11 // copyright and related and neighboring rights to this software to
12 // the public domain worldwide. This software is distributed without
15 // You may have received a copy of the CC0 Public Domain Dedication
16 // along with this software. If not, see
17 // http://creativecommons.org/publicdomain/zero/1.0/
20 // Get an image color from a string
21 function get_color($str, $img) {
25 if (strlen($str) > 3) {
26 $a = 127 - (ord($str[3]) / 2);
27 if ($a != 0 && $a != 127)
33 $this->all_transaprent = 0;
34 return imagecolorallocatealpha($img, $r, $g, $b, $a);
37 // Given a string with the contents of an .ICO,
38 // return a GD image of the icon, or false on error.
39 function fromiconstring($ico) {
40 $this->error = "(unknown error)";
44 if (strlen($ico) < 6) {
45 $this->error = "too short";
48 $h = unpack("vzero/vtype/vnum", $ico);
50 // Must be ICO format with at least one image
51 if ($h["zero"] != 0 || $h["type"] != 1 || $h["num"] == 0) {
52 // See if we can just parse it with GD directly
53 // if it's not ICO format; maybe it was a mislabeled
55 $i = @imagecreatefromstring($ico);
57 imagesavealpha($i, true);
60 $this->error = "not ICO or other image";
64 // Read directory entries to find the biggest image
66 for ($i = 0; $i < $h["num"]; $i++) {
67 $entry = substr($ico, 6 + 16 * $i, 16);
68 if (!$entry || strlen($entry) < 16)
70 $e = unpack("Cwidth/" .
81 if ($e["height"] == 0)
83 if ($e["zero"] != 0) {
84 $this->error = "nonzero reserved field";
87 $pixels = $e["width"] * $e["height"];
88 if ($pixels > $most_pixels) {
89 $most_pixels = $pixels;
93 if ($most_pixels == 0) {
94 $this->error = "no pixels";
100 $data = substr($ico, $e["offset"], $e["size"]);
101 if (!$data || strlen($data) != $e["size"]) {
102 $this->error = "bad image data";
106 // See if we can parse it (might be PNG format here)
107 $i = @imagecreatefromstring($data);
109 imagesavealpha($img, true);
113 // Must be a BMP. Parse it ourselves.
114 $img = imagecreatetruecolor($e["width"], $e["height"]);
115 imagesavealpha($img, true);
116 $bg = imagecolorallocatealpha($img, 255, 0, 0, 127);
117 imagefill($img, 0, 0, $bg);
119 // Skip over the BITMAPCOREHEADER or BITMAPINFOHEADER;
120 // we'll just assume the palette and pixel data follow
121 // in the most obvious format as described by the icon
123 $bitmapinfo = unpack("Vsize", $data);
124 if ($bitmapinfo["size"] == 40) {
125 $info = unpack("Vsize/" .
135 "Vimpcolors/", $data);
136 if ($e["bpp"] == 0) {
137 $e["bpp"] = $info["bpp"];
140 $data = substr($data, $bitmapinfo["size"]);
142 $height = $e["height"];
143 $width = $e["width"];
146 // For indexed images, we only support 1, 4, or 8 BPP
158 $this->error = "bad BPP $bpp";
165 $this->all_transparent = 1;
166 for ($i = 0; $i < (1 << $bpp); $i++) {
167 $entry = substr($data, $i * 4, 4);
168 $palette[$i] = $this->get_color($entry, $img);
172 // Hack for some icons: if everything was transparent,
173 // discard alpha channel.
174 if ($this->all_transparent) {
175 for ($i = 0; $i < (1 << $bpp); $i++) {
176 $palette[$i] &= 0xffffff;
181 // Assume image data follows in bottom-up order.
182 // First the "XOR" image
183 if ((strlen($data) - $offset) < ($bpp * $height * $width / 8)) {
184 $this->error = "short data";
188 for ($y = $height - 1; $y >= 0; $y--) {
190 while ($x < $width) {
193 $entry = substr($data, $offset, $bytes);
194 $pixel = $this->get_color($entry, $img);
195 $XOR[$y][$x] = $pixel;
198 } elseif ($bpp == 1) {
199 $p = ord($data[$offset]);
200 for ($b = 0x80; $b > 0; $b >>= 1) {
202 $pixel = $palette[1];
204 $pixel = $palette[0];
206 $XOR[$y][$x] = $pixel;
210 } elseif ($bpp == 4) {
211 $p = ord($data[$offset]);
212 $pixel1 = $palette[$p >> 4];
213 $pixel2 = $palette[$p & 0x0f];
214 $XOR[$y][$x] = $pixel1;
215 $XOR[$y][$x+1] = $pixel2;
218 } elseif ($bpp == 8) {
219 $pixel = $palette[ord($data[$offset])];
220 $XOR[$y][$x] = $pixel;
224 $this->error = "bad BPP";
228 // End of row padding
233 // Now the "AND" image, which is 1 bit per pixel. Ignore
234 // if some of our image data already had alpha values,
235 // or if there isn't enough data left.
236 if ($this->had_alpha ||
237 ((strlen($data) - $offset) < ($height * $width / 8))) {
238 // Just return what we've got
239 for ($y = 0; $y < $height; $y++) {
240 for ($x = 0; $x < $width; $x++) {
241 imagesetpixel($img, $x, $y,
248 // Mask what we have with the "AND" image
249 for ($y = $height - 1; $y >= 0; $y--) {
251 while ($x < $width) {
253 $b > 0 && $x < $width; $b >>= 1) {
254 if (!(ord($data[$offset]) & $b)) {
255 imagesetpixel($img, $x, $y,
263 // End of row padding