1: <?php
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13: class phpthumb_ico {
14:
15: function GD2ICOstring(&$gd_image_array) {
16: $ImageWidths = array();
17: $ImageHeights = array();
18: $bpp = array();
19: $totalcolors = array();
20: $icXOR = array();
21: $icAND = array();
22: $icANDmask = array();
23: foreach ($gd_image_array as $key => $gd_image) {
24:
25: $ImageWidths[$key] = imagesx($gd_image);
26: $ImageHeights[$key] = imagesy($gd_image);
27: $bpp[$key] = imageistruecolor($gd_image) ? 32 : 24;
28: $totalcolors[$key] = imagecolorstotal($gd_image);
29:
30: $icXOR[$key] = '';
31: for ($y = $ImageHeights[$key] - 1; $y >= 0; $y--) {
32: for ($x = 0; $x < $ImageWidths[$key]; $x++) {
33: $argb = phpthumb_functions::GetPixelColor($gd_image, $x, $y);
34: $a = round(255 * ((127 - $argb['alpha']) / 127));
35: $r = $argb['red'];
36: $g = $argb['green'];
37: $b = $argb['blue'];
38:
39: if ($bpp[$key] == 32) {
40: $icXOR[$key] .= chr($b).chr($g).chr($r).chr($a);
41: } elseif ($bpp[$key] == 24) {
42: $icXOR[$key] .= chr($b).chr($g).chr($r);
43: }
44:
45: if ($a < 128) {
46: @$icANDmask[$key][$y] .= '1';
47: } else {
48: @$icANDmask[$key][$y] .= '0';
49: }
50: }
51:
52: while (strlen($icANDmask[$key][$y]) % 32) {
53: $icANDmask[$key][$y] .= '0';
54: }
55: }
56: $icAND[$key] = '';
57: foreach ($icANDmask[$key] as $y => $scanlinemaskbits) {
58: for ($i = 0; $i < strlen($scanlinemaskbits); $i += 8) {
59: $icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits, $i, 8), 8, '0', STR_PAD_LEFT)));
60: }
61: }
62:
63: }
64:
65: foreach ($gd_image_array as $key => $gd_image) {
66: $biSizeImage = $ImageWidths[$key] * $ImageHeights[$key] * ($bpp[$key] / 8);
67:
68:
69: $BitmapInfoHeader[$key] = '';
70: $BitmapInfoHeader[$key] .= "\x28\x00\x00\x00";
71: $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageWidths[$key], 4);
72:
73:
74: $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageHeights[$key] * 2, 4);
75: $BitmapInfoHeader[$key] .= "\x01\x00";
76: $BitmapInfoHeader[$key] .= chr($bpp[$key])."\x00";
77: $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";
78: $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($biSizeImage, 4);
79: $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";
80: $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";
81: $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";
82: $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";
83: }
84:
85:
86: $icondata = "\x00\x00";
87: $icondata .= "\x01\x00";
88: $icondata .= phpthumb_functions::LittleEndian2String(count($gd_image_array), 2);
89:
90: $dwImageOffset = 6 + (count($gd_image_array) * 16);
91: foreach ($gd_image_array as $key => $gd_image) {
92:
93:
94: $icondata .= chr($ImageWidths[$key]);
95: $icondata .= chr($ImageHeights[$key]);
96: $icondata .= chr($totalcolors[$key]);
97: $icondata .= "\x00";
98:
99: $icondata .= "\x01\x00";
100: $icondata .= chr($bpp[$key])."\x00";
101:
102: $dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]);
103: $icondata .= phpthumb_functions::LittleEndian2String($dwBytesInRes, 4);
104:
105: $icondata .= phpthumb_functions::LittleEndian2String($dwImageOffset, 4);
106: $dwImageOffset += strlen($BitmapInfoHeader[$key]);
107: $dwImageOffset += strlen($icXOR[$key]);
108: $dwImageOffset += strlen($icAND[$key]);
109: }
110:
111: foreach ($gd_image_array as $key => $gd_image) {
112: $icondata .= $BitmapInfoHeader[$key];
113: $icondata .= $icXOR[$key];
114: $icondata .= $icAND[$key];
115: }
116:
117: return $icondata;
118: }
119:
120: }
121: