1
|
|
|
<?php |
2
|
|
|
////////////////////////////////////////////////////////////// |
3
|
|
|
// phpThumb() by James Heinrich <[email protected]> // |
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
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class phpthumb_ico |
14
|
|
|
*/ |
15
|
|
|
class phpthumb_ico |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
// removed for XOOPS |
19
|
|
|
//function phpthumb_ico() { |
20
|
|
|
// return true; |
21
|
|
|
//} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param $gd_image_array |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
public function GD2ICOstring(&$gd_image_array) |
28
|
|
|
{ |
29
|
|
|
foreach ($gd_image_array as $key => $gd_image) { |
30
|
|
|
$ImageWidths[$key] = imagesx($gd_image); |
31
|
|
|
$ImageHeights[$key] = imagesy($gd_image); |
32
|
|
|
$bpp[$key] = imageistruecolor($gd_image) ? 32 : 24; |
33
|
|
|
$totalcolors[$key] = imagecolorstotal($gd_image); |
34
|
|
|
|
35
|
|
|
$icXOR[$key] = ''; |
36
|
|
|
for ($y = $ImageHeights[$key] - 1; $y >= 0; $y--) { |
37
|
|
|
for ($x = 0; $x < $ImageWidths[$key]; $x++) { |
38
|
|
|
$argb = phpthumb_functions::GetPixelColor($gd_image, $x, $y); |
39
|
|
|
$a = round(255 * ((127 - $argb['alpha']) / 127)); |
40
|
|
|
$r = $argb['red']; |
41
|
|
|
$g = $argb['green']; |
42
|
|
|
$b = $argb['blue']; |
43
|
|
|
|
44
|
|
|
if ($bpp[$key] == 32) { |
45
|
|
|
$icXOR[$key] .= chr($b) . chr($g) . chr($r) . chr($a); |
46
|
|
|
} elseif ($bpp[$key] == 24) { |
47
|
|
|
$icXOR[$key] .= chr($b) . chr($g) . chr($r); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($a < 128) { |
51
|
|
|
@$icANDmask[$key][$y] .= '1'; |
|
|
|
|
52
|
|
|
} else { |
53
|
|
|
@$icANDmask[$key][$y] .= '0'; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
// mask bits are 32-bit aligned per scanline |
57
|
|
|
while (strlen($icANDmask[$key][$y]) % 32) { |
58
|
|
|
$icANDmask[$key][$y] .= '0'; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
$icAND[$key] = ''; |
62
|
|
|
foreach ($icANDmask[$key] as $y => $scanlinemaskbits) { |
63
|
|
|
for ($i = 0, $iMax = strlen($scanlinemaskbits); $i < $iMax; $i += 8) { |
64
|
|
|
$icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits, $i, 8), 8, '0', STR_PAD_LEFT))); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
foreach ($gd_image_array as $key => $gd_image) { |
70
|
|
|
$biSizeImage = $ImageWidths[$key] * $ImageHeights[$key] * ($bpp[$key] / 8); |
71
|
|
|
|
72
|
|
|
// BITMAPINFOHEADER - 40 bytes |
73
|
|
|
$BitmapInfoHeader[$key] = ''; |
74
|
|
|
$BitmapInfoHeader[$key] .= "\x28\x00\x00\x00"; // DWORD biSize; |
75
|
|
|
$BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageWidths[$key], 4); // LONG biWidth; |
76
|
|
|
// The biHeight member specifies the combined |
77
|
|
|
// height of the XOR and AND masks. |
78
|
|
|
$BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageHeights[$key] * 2, 4); // LONG biHeight; |
79
|
|
|
$BitmapInfoHeader[$key] .= "\x01\x00"; // WORD biPlanes; |
80
|
|
|
$BitmapInfoHeader[$key] .= chr($bpp[$key]) . "\x00"; // wBitCount; |
81
|
|
|
$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biCompression; |
82
|
|
|
$BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($biSizeImage, 4); // DWORD biSizeImage; |
83
|
|
|
$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // LONG biXPelsPerMeter; |
84
|
|
|
$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // LONG biYPelsPerMeter; |
85
|
|
|
$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biClrUsed; |
86
|
|
|
$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biClrImportant; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$icondata = "\x00\x00"; // idReserved; // Reserved (must be 0) |
90
|
|
|
$icondata .= "\x01\x00"; // idType; // Resource Type (1 for icons) |
91
|
|
|
$icondata .= phpthumb_functions::LittleEndian2String(count($gd_image_array), 2); // idCount; // How many images? |
92
|
|
|
|
93
|
|
|
$dwImageOffset = 6 + (count($gd_image_array) * 16); |
94
|
|
|
foreach ($gd_image_array as $key => $gd_image) { |
95
|
|
|
// ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of 'em) |
96
|
|
|
|
97
|
|
|
$icondata .= chr($ImageWidths[$key]); // bWidth; // Width, in pixels, of the image |
98
|
|
|
$icondata .= chr($ImageHeights[$key]); // bHeight; // Height, in pixels, of the image |
99
|
|
|
$icondata .= chr($totalcolors[$key]); // bColorCount; // Number of colors in image (0 if >=8bpp) |
100
|
|
|
$icondata .= "\x00"; // bReserved; // Reserved ( must be 0) |
101
|
|
|
|
102
|
|
|
$icondata .= "\x01\x00"; // wPlanes; // Color Planes |
103
|
|
|
$icondata .= chr($bpp[$key]) . "\x00"; // wBitCount; // Bits per pixel |
104
|
|
|
|
105
|
|
|
$dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]); |
106
|
|
|
$icondata .= phpthumb_functions::LittleEndian2String($dwBytesInRes, 4); // dwBytesInRes; // How many bytes in this resource? |
107
|
|
|
|
108
|
|
|
$icondata .= phpthumb_functions::LittleEndian2String($dwImageOffset, 4); // dwImageOffset; // Where in the file is this image? |
109
|
|
|
$dwImageOffset += strlen($BitmapInfoHeader[$key]); |
110
|
|
|
$dwImageOffset += strlen($icXOR[$key]); |
111
|
|
|
$dwImageOffset += strlen($icAND[$key]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
foreach ($gd_image_array as $key => $gd_image) { |
115
|
|
|
$icondata .= $BitmapInfoHeader[$key]; |
116
|
|
|
$icondata .= $icXOR[$key]; |
117
|
|
|
$icondata .= $icAND[$key]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $icondata; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: