2 //////////////////////////////////////////////////////////////
3 /// phpThumb() by James Heinrich <info@silisoftware.com> //
4 // available at http://phpthumb.sourceforge.net ///
5 //////////////////////////////////////////////////////////////
7 // phpthumb.ico.php - .ICO output format functions //
9 //////////////////////////////////////////////////////////////
14 function phpthumb_ico() {
19 function GD2ICOstring(&$gd_image_array) {
20 foreach ($gd_image_array as $key => $gd_image) {
22 $ImageWidths[$key] = ImageSX($gd_image);
23 $ImageHeights[$key] = ImageSY($gd_image);
24 $bpp[$key] = ImageIsTrueColor($gd_image) ? 32 : 24;
25 $totalcolors[$key] = ImageColorsTotal($gd_image);
28 for ($y = $ImageHeights[$key] - 1; $y >= 0; $y--) {
29 for ($x = 0; $x < $ImageWidths[$key]; $x++) {
30 $argb = phpthumb_functions::GetPixelColor($gd_image, $x, $y);
31 $a = round(255 * ((127 - $argb['alpha']) / 127));
36 if ($bpp[$key] == 32) {
37 $icXOR[$key] .= chr($b).chr($g).chr($r).chr($a);
38 } elseif ($bpp[$key] == 24) {
39 $icXOR[$key] .= chr($b).chr($g).chr($r);
43 @$icANDmask[$key][$y] .= '1';
45 @$icANDmask[$key][$y] .= '0';
48 // mask bits are 32-bit aligned per scanline
49 while (strlen($icANDmask[$key][$y]) % 32) {
50 $icANDmask[$key][$y] .= '0';
54 foreach ($icANDmask[$key] as $y => $scanlinemaskbits) {
55 for ($i = 0; $i < strlen($scanlinemaskbits); $i += 8) {
56 $icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits, $i, 8), 8, '0', STR_PAD_LEFT)));
62 foreach ($gd_image_array as $key => $gd_image) {
63 $biSizeImage = $ImageWidths[$key] * $ImageHeights[$key] * ($bpp[$key] / 8);
65 // BITMAPINFOHEADER - 40 bytes
66 $BitmapInfoHeader[$key] = '';
67 $BitmapInfoHeader[$key] .= "\x28\x00\x00\x00"; // DWORD biSize;
68 $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageWidths[$key], 4); // LONG biWidth;
69 // The biHeight member specifies the combined
70 // height of the XOR and AND masks.
71 $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageHeights[$key] * 2, 4); // LONG biHeight;
72 $BitmapInfoHeader[$key] .= "\x01\x00"; // WORD biPlanes;
73 $BitmapInfoHeader[$key] .= chr($bpp[$key])."\x00"; // wBitCount;
74 $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biCompression;
75 $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($biSizeImage, 4); // DWORD biSizeImage;
76 $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // LONG biXPelsPerMeter;
77 $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // LONG biYPelsPerMeter;
78 $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biClrUsed;
79 $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biClrImportant;
83 $icondata = "\x00\x00"; // idReserved; // Reserved (must be 0)
84 $icondata .= "\x01\x00"; // idType; // Resource Type (1 for icons)
85 $icondata .= phpthumb_functions::LittleEndian2String(count($gd_image_array), 2); // idCount; // How many images?
87 $dwImageOffset = 6 + (count($gd_image_array) * 16);
88 foreach ($gd_image_array as $key => $gd_image) {
89 // ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of 'em)
91 $icondata .= chr($ImageWidths[$key]); // bWidth; // Width, in pixels, of the image
92 $icondata .= chr($ImageHeights[$key]); // bHeight; // Height, in pixels, of the image
93 $icondata .= chr($totalcolors[$key]); // bColorCount; // Number of colors in image (0 if >=8bpp)
94 $icondata .= "\x00"; // bReserved; // Reserved ( must be 0)
96 $icondata .= "\x01\x00"; // wPlanes; // Color Planes
97 $icondata .= chr($bpp[$key])."\x00"; // wBitCount; // Bits per pixel
99 $dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]);
100 $icondata .= phpthumb_functions::LittleEndian2String($dwBytesInRes, 4); // dwBytesInRes; // How many bytes in this resource?
102 $icondata .= phpthumb_functions::LittleEndian2String($dwImageOffset, 4); // dwImageOffset; // Where in the file is this image?
103 $dwImageOffset += strlen($BitmapInfoHeader[$key]);
104 $dwImageOffset += strlen($icXOR[$key]);
105 $dwImageOffset += strlen($icAND[$key]);
108 foreach ($gd_image_array as $key => $gd_image) {
109 $icondata .= $BitmapInfoHeader[$key];
110 $icondata .= $icXOR[$key];
111 $icondata .= $icAND[$key];