phpthumb.ico.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. //////////////////////////////////////////////////////////////
  3. // phpThumb() by James Heinrich <info@silisoftware.com> //
  4. // available at http://phpthumb.sourceforge.net //
  5. // and/or https://github.com/JamesHeinrich/phpThumb //
  6. //////////////////////////////////////////////////////////////
  7. /// //
  8. // phpthumb.ico.php - .ICO output format functions //
  9. // ///
  10. //////////////////////////////////////////////////////////////
  11. class phpthumb_ico {
  12. public function GD2ICOstring(&$gd_image_array) {
  13. $ImageWidths = array();
  14. $ImageHeights = array();
  15. $bpp = array();
  16. $totalcolors = array();
  17. $icXOR = array();
  18. $icAND = array();
  19. $icANDmask = array();
  20. foreach ($gd_image_array as $key => $gd_image) {
  21. $ImageWidths[$key] = imagesx($gd_image);
  22. $ImageHeights[$key] = imagesy($gd_image);
  23. $bpp[$key] = imageistruecolor($gd_image) ? 32 : 24;
  24. $totalcolors[$key] = imagecolorstotal($gd_image);
  25. $icXOR[$key] = '';
  26. for ($y = $ImageHeights[$key] - 1; $y >= 0; $y--) {
  27. for ($x = 0; $x < $ImageWidths[$key]; $x++) {
  28. $argb = phpthumb_functions::GetPixelColor($gd_image, $x, $y);
  29. $a = round(255 * ((127 - $argb['alpha']) / 127));
  30. $r = $argb['red'];
  31. $g = $argb['green'];
  32. $b = $argb['blue'];
  33. if ($bpp[$key] == 32) {
  34. $icXOR[$key] .= chr($b).chr($g).chr($r).chr($a);
  35. } elseif ($bpp[$key] == 24) {
  36. $icXOR[$key] .= chr($b).chr($g).chr($r);
  37. }
  38. if ($a < 128) {
  39. @$icANDmask[$key][$y] .= '1';
  40. } else {
  41. @$icANDmask[$key][$y] .= '0';
  42. }
  43. }
  44. // mask bits are 32-bit aligned per scanline
  45. while (strlen($icANDmask[$key][$y]) % 32) {
  46. $icANDmask[$key][$y] .= '0';
  47. }
  48. }
  49. $icAND[$key] = '';
  50. foreach ($icANDmask[$key] as $y => $scanlinemaskbits) {
  51. for ($i = 0, $iMax = strlen($scanlinemaskbits); $i < $iMax; $i += 8) {
  52. $icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits, $i, 8), 8, '0', STR_PAD_LEFT)));
  53. }
  54. }
  55. }
  56. foreach ($gd_image_array as $key => $gd_image) {
  57. $biSizeImage = $ImageWidths[$key] * $ImageHeights[$key] * ($bpp[$key] / 8);
  58. // BITMAPINFOHEADER - 40 bytes
  59. $BitmapInfoHeader[$key] = '';
  60. $BitmapInfoHeader[$key] .= "\x28\x00\x00\x00"; // DWORD biSize;
  61. $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageWidths[$key], 4); // LONG biWidth;
  62. // The biHeight member specifies the combined
  63. // height of the XOR and AND masks.
  64. $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageHeights[$key] * 2, 4); // LONG biHeight;
  65. $BitmapInfoHeader[$key] .= "\x01\x00"; // WORD biPlanes;
  66. $BitmapInfoHeader[$key] .= chr($bpp[$key])."\x00"; // wBitCount;
  67. $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biCompression;
  68. $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($biSizeImage, 4); // DWORD biSizeImage;
  69. $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // LONG biXPelsPerMeter;
  70. $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // LONG biYPelsPerMeter;
  71. $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biClrUsed;
  72. $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biClrImportant;
  73. }
  74. $icondata = "\x00\x00"; // idReserved; // Reserved (must be 0)
  75. $icondata .= "\x01\x00"; // idType; // Resource Type (1 for icons)
  76. $icondata .= phpthumb_functions::LittleEndian2String(count($gd_image_array), 2); // idCount; // How many images?
  77. $dwImageOffset = 6 + (count($gd_image_array) * 16);
  78. foreach ($gd_image_array as $key => $gd_image) {
  79. // ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of 'em)
  80. $icondata .= chr($ImageWidths[$key]); // bWidth; // Width, in pixels, of the image
  81. $icondata .= chr($ImageHeights[$key]); // bHeight; // Height, in pixels, of the image
  82. $icondata .= chr($totalcolors[$key]); // bColorCount; // Number of colors in image (0 if >=8bpp)
  83. $icondata .= "\x00"; // bReserved; // Reserved ( must be 0)
  84. $icondata .= "\x01\x00"; // wPlanes; // Color Planes
  85. $icondata .= chr($bpp[$key])."\x00"; // wBitCount; // Bits per pixel
  86. $dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]);
  87. $icondata .= phpthumb_functions::LittleEndian2String($dwBytesInRes, 4); // dwBytesInRes; // How many bytes in this resource?
  88. $icondata .= phpthumb_functions::LittleEndian2String($dwImageOffset, 4); // dwImageOffset; // Where in the file is this image?
  89. $dwImageOffset += strlen($BitmapInfoHeader[$key]);
  90. $dwImageOffset += strlen($icXOR[$key]);
  91. $dwImageOffset += strlen($icAND[$key]);
  92. }
  93. foreach ($gd_image_array as $key => $gd_image) {
  94. $icondata .= $BitmapInfoHeader[$key];
  95. $icondata .= $icXOR[$key];
  96. $icondata .= $icAND[$key];
  97. }
  98. return $icondata;
  99. }
  100. }