jimIcon.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. // Simple .ICO parsing. The ICO format is insanely complex and this may
  3. // fail to correctly handle some technically valid files, but it works
  4. // on the majority I've found.
  5. //
  6. // jimIcon was written in 2013 by Jim Paris <jim@jtan.com> and is
  7. // released under the terms of the CC0:
  8. //
  9. // To the extent possible under law, the author(s) have dedicated all
  10. // copyright and related and neighboring rights to this software to
  11. // the public domain worldwide. This software is distributed without
  12. // any arranty.
  13. //
  14. // You may have received a copy of the CC0 Public Domain Dedication
  15. // along with this software. If not, see
  16. // http://creativecommons.org/publicdomain/zero/1.0/
  17. class jimIcon {
  18. // Get an image color from a string
  19. function get_color($str, $img) {
  20. $b = ord($str[0]);
  21. $g = ord($str[1]);
  22. $r = ord($str[2]);
  23. if (strlen($str) > 3) {
  24. $a = 127 - (ord($str[3]) / 2);
  25. if ($a != 0 && $a != 127)
  26. $this->had_alpha = 1;
  27. } else {
  28. $a = 0;
  29. }
  30. if ($a != 127)
  31. $this->all_transaprent = 0;
  32. return imagecolorallocatealpha($img, $r, $g, $b, $a);
  33. }
  34. // Given a string with the contents of an .ICO,
  35. // return a GD image of the icon, or false on error.
  36. function fromiconstring($ico) {
  37. $this->error = "(unknown error)";
  38. $this->had_alpha = 0;
  39. // Read header
  40. if (strlen($ico) < 6) {
  41. $this->error = "too short";
  42. return false;
  43. }
  44. $h = unpack("vzero/vtype/vnum", $ico);
  45. // Must be ICO format with at least one image
  46. if ($h["zero"] != 0 || $h["type"] != 1 || $h["num"] == 0) {
  47. // See if we can just parse it with GD directly
  48. // if it's not ICO format; maybe it was a mislabeled
  49. // PNG or something.
  50. $i = @imagecreatefromstring($ico);
  51. if ($i) {
  52. imagesavealpha($i, true);
  53. return $i;
  54. }
  55. $this->error = "not ICO or other image";
  56. return false;
  57. }
  58. // Read directory entries to find the biggest image
  59. $most_pixels = 0;
  60. for ($i = 0; $i < $h["num"]; $i++) {
  61. $entry = substr($ico, 6 + 16 * $i, 16);
  62. if (!$entry || strlen($entry) < 16)
  63. continue;
  64. $e = unpack("Cwidth/" .
  65. "Cheight/" .
  66. "Ccolors/" .
  67. "Czero/" .
  68. "vplanes/" .
  69. "vbpp/" .
  70. "Vsize/" .
  71. "Voffset/",
  72. $entry);
  73. if ($e["width"] == 0)
  74. $e["width"] = 256;
  75. if ($e["height"] == 0)
  76. $e["height"] = 256;
  77. if ($e["zero"] != 0) {
  78. $this->error = "nonzero reserved field";
  79. return false;
  80. }
  81. $pixels = $e["width"] * $e["height"];
  82. if ($pixels > $most_pixels) {
  83. $most_pixels = $pixels;
  84. $most = $e;
  85. }
  86. }
  87. if ($most_pixels == 0) {
  88. $this->error = "no pixels";
  89. return false;
  90. }
  91. $e = $most;
  92. // Extract image data
  93. $data = substr($ico, $e["offset"], $e["size"]);
  94. if (!$data || strlen($data) != $e["size"]) {
  95. $this->error = "bad image data";
  96. return false;
  97. }
  98. // See if we can parse it (might be PNG format here)
  99. $i = @imagecreatefromstring($data);
  100. if ($i) {
  101. imagesavealpha($img, true);
  102. return $i;
  103. }
  104. // Must be a BMP. Parse it ourselves.
  105. $img = imagecreatetruecolor($e["width"], $e["height"]);
  106. imagesavealpha($img, true);
  107. $bg = imagecolorallocatealpha($img, 255, 0, 0, 127);
  108. imagefill($img, 0, 0, $bg);
  109. // Skip over the BITMAPCOREHEADER or BITMAPINFOHEADER;
  110. // we'll just assume the palette and pixel data follow
  111. // in the most obvious format as described by the icon
  112. // directory entry.
  113. $bitmapinfo = unpack("Vsize", $data);
  114. if ($bitmapinfo["size"] == 40) {
  115. $info = unpack("Vsize/" .
  116. "Vwidth/" .
  117. "Vheight/" .
  118. "vplanes/" .
  119. "vbpp/" .
  120. "Vcompress/" .
  121. "Vsize/" .
  122. "Vxres/" .
  123. "Vyres/" .
  124. "Vpalcolors/" .
  125. "Vimpcolors/", $data);
  126. if ($e["bpp"] == 0) {
  127. $e["bpp"] = $info["bpp"];
  128. }
  129. }
  130. $data = substr($data, $bitmapinfo["size"]);
  131. $height = $e["height"];
  132. $width = $e["width"];
  133. $bpp = $e["bpp"];
  134. // For indexed images, we only support 1, 4, or 8 BPP
  135. switch ($bpp) {
  136. case 1:
  137. case 4:
  138. case 8:
  139. $indexed = 1;
  140. break;
  141. case 24:
  142. case 32:
  143. $indexed = 0;
  144. break;
  145. default:
  146. $this->error = "bad BPP $bpp";
  147. return false;
  148. }
  149. $offset = 0;
  150. if ($indexed) {
  151. $palette = array();
  152. $this->all_transparent = 1;
  153. for ($i = 0; $i < (1 << $bpp); $i++) {
  154. $entry = substr($data, $i * 4, 4);
  155. $palette[$i] = $this->get_color($entry, $img);
  156. }
  157. $offset = $i * 4;
  158. // Hack for some icons: if everything was transparent,
  159. // discard alpha channel.
  160. if ($this->all_transparent) {
  161. for ($i = 0; $i < (1 << $bpp); $i++) {
  162. $palette[$i] &= 0xffffff;
  163. }
  164. }
  165. }
  166. // Assume image data follows in bottom-up order.
  167. // First the "XOR" image
  168. if ((strlen($data) - $offset) < ($bpp * $height * $width / 8)) {
  169. $this->error = "short data";
  170. return false;
  171. }
  172. $XOR = array();
  173. for ($y = $height - 1; $y >= 0; $y--) {
  174. $x = 0;
  175. while ($x < $width) {
  176. if (!$indexed) {
  177. $bytes = $bpp / 8;
  178. $entry = substr($data, $offset, $bytes);
  179. $pixel = $this->get_color($entry, $img);
  180. $XOR[$y][$x] = $pixel;
  181. $x++;
  182. $offset += $bytes;
  183. } elseif ($bpp == 1) {
  184. $p = ord($data[$offset]);
  185. for ($b = 0x80; $b > 0; $b >>= 1) {
  186. if ($p & $b) {
  187. $pixel = $palette[1];
  188. } else {
  189. $pixel = $palette[0];
  190. }
  191. $XOR[$y][$x] = $pixel;
  192. $x++;
  193. }
  194. $offset++;
  195. } elseif ($bpp == 4) {
  196. $p = ord($data[$offset]);
  197. $pixel1 = $palette[$p >> 4];
  198. $pixel2 = $palette[$p & 0x0f];
  199. $XOR[$y][$x] = $pixel1;
  200. $XOR[$y][$x+1] = $pixel2;
  201. $x += 2;
  202. $offset++;
  203. } elseif ($bpp == 8) {
  204. $pixel = $palette[ord($data[$offset])];
  205. $XOR[$y][$x] = $pixel;
  206. $x += 1;
  207. $offset++;
  208. } else {
  209. $this->error = "bad BPP";
  210. return false;
  211. }
  212. }
  213. // End of row padding
  214. while ($offset & 3)
  215. $offset++;
  216. }
  217. // Now the "AND" image, which is 1 bit per pixel. Ignore
  218. // if some of our image data already had alpha values,
  219. // or if there isn't enough data left.
  220. if ($this->had_alpha ||
  221. ((strlen($data) - $offset) < ($height * $width / 8))) {
  222. // Just return what we've got
  223. for ($y = 0; $y < $height; $y++) {
  224. for ($x = 0; $x < $width; $x++) {
  225. imagesetpixel($img, $x, $y,
  226. $XOR[$y][$x]);
  227. }
  228. }
  229. return $img;
  230. }
  231. // Mask what we have with the "AND" image
  232. for ($y = $height - 1; $y >= 0; $y--) {
  233. $x = 0;
  234. while ($x < $width) {
  235. for ($b = 0x80;
  236. $b > 0 && $x < $width; $b >>= 1) {
  237. if (!(ord($data[$offset]) & $b)) {
  238. imagesetpixel($img, $x, $y,
  239. $XOR[$y][$x]);
  240. }
  241. $x++;
  242. }
  243. $offset++;
  244. }
  245. // End of row padding
  246. while ($offset & 3)
  247. $offset++;
  248. }
  249. return $img;
  250. }
  251. }
  252. ?>