1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24
25 26 27 28 29 30 31 32 33 34 35 36 37
38 class WideImage_vendor_de77_BMP
39 {
40 public static function imagebmp(&$img, $filename = false)
41 {
42 $wid = imagesx($img);
43 $hei = imagesy($img);
44 $wid_pad = str_pad('', $wid % 4, "\0");
45
46 $size = 54 + ($wid + $wid_pad) * $hei * 3;
47
48
49 $header['identifier'] = 'BM';
50 $header['file_size'] = self::dword($size);
51 $header['reserved'] = self::dword(0);
52 $header['bitmap_data'] = self::dword(54);
53 $header['header_size'] = self::dword(40);
54 $header['width'] = self::dword($wid);
55 $header['height'] = self::dword($hei);
56 $header['planes'] = self::word(1);
57 $header['bits_per_pixel'] = self::word(24);
58 $header['compression'] = self::dword(0);
59 $header['data_size'] = self::dword(0);
60 $header['h_resolution'] = self::dword(0);
61 $header['v_resolution'] = self::dword(0);
62 $header['colors'] = self::dword(0);
63 $header['important_colors'] = self::dword(0);
64
65 if ($filename)
66 {
67 $f = fopen($filename, "wb");
68 foreach ($header AS $h)
69 {
70 fwrite($f, $h);
71 }
72
73
74 for ($y=$hei-1; $y>=0; $y--)
75 {
76 for ($x=0; $x<$wid; $x++)
77 {
78 $rgb = imagecolorat($img, $x, $y);
79 fwrite($f, self::byte3($rgb));
80 }
81 fwrite($f, $wid_pad);
82 }
83 fclose($f);
84 }
85 else
86 {
87 foreach ($header AS $h)
88 {
89 echo $h;
90 }
91
92
93 for ($y=$hei-1; $y>=0; $y--)
94 {
95 for ($x=0; $x<$wid; $x++)
96 {
97 $rgb = imagecolorat($img, $x, $y);
98 echo self::byte3($rgb);
99 }
100 echo $wid_pad;
101 }
102 }
103 return true;
104 }
105
106 public static function imagecreatefromstring($data)
107 {
108
109 $pos = 0;
110 $header = substr($data, 0, 54);
111 $pos = 54;
112
113 if (strlen($header) < 54)
114 return false;
115
116 $header = unpack( 'c2identifier/Vfile_size/Vreserved/Vbitmap_data/Vheader_size/' .
117 'Vwidth/Vheight/vplanes/vbits_per_pixel/Vcompression/Vdata_size/'.
118 'Vh_resolution/Vv_resolution/Vcolors/Vimportant_colors', $header);
119
120 if ($header['identifier1'] != 66 or $header['identifier2'] != 77)
121 {
122 return false;
123
124 }
125
126 if (!in_array($header['bits_per_pixel'], array(24, 32, 8, 4, 1)))
127 {
128 return false;
129
130 }
131
132 $bps = $header['bits_per_pixel'];
133 $wid2 = ceil(($bps/8 * $header['width']) / 4) * 4;
134 $colors = pow(2, $bps);
135
136 $wid = $header['width'];
137 $hei = $header['height'];
138
139 $img = imagecreatetruecolor($header['width'], $header['height']);
140
141
142 if ($bps < 9)
143 {
144 for ($i=0; $i<$colors; $i++)
145 {
146 $palette[] = self::undword(substr($data, $pos, 4));
147 $pos += 4;
148 }
149 }
150 else
151 {
152 if ($bps == 32)
153 {
154 imagealphablending($img, false);
155 imagesavealpha($img, true);
156 }
157 $palette = array();
158 }
159
160
161 for ($y=$hei-1; $y>=0; $y--)
162 {
163 $row = substr($data, $pos, $wid2);
164 $pos += $wid2;
165 $pixels = self::str_split2($row, $bps, $palette);
166 for ($x=0; $x<$wid; $x++)
167 {
168 self::makepixel($img, $x, $y, $pixels[$x], $bps);
169 }
170 }
171
172 return $img;
173 }
174
175 public static function imagecreatefrombmp($filename)
176 {
177 return self::imagecreatefromstring(file_get_contents($filename));
178 }
179
180 private static function str_split2($row, $bps, $palette)
181 {
182 switch ($bps)
183 {
184 case 32:
185 case 24: return str_split($row, $bps/8);
186 case 8: $out = array();
187 $count = strlen($row);
188 for ($i=0; $i<$count; $i++)
189 {
190 $out[] = $palette[ ord($row[$i]) ];
191 }
192 return $out;
193 case 4: $out = array();
194 $count = strlen($row);
195 for ($i=0; $i<$count; $i++)
196 {
197 $roww = ord($row[$i]);
198 $out[] = $palette[ ($roww & 240) >> 4 ];
199 $out[] = $palette[ ($roww & 15) ];
200 }
201 return $out;
202 case 1: $out = array();
203 $count = strlen($row);
204 for ($i=0; $i<$count; $i++)
205 {
206 $roww = ord($row[$i]);
207 $out[] = $palette[ ($roww & 128) >> 7 ];
208 $out[] = $palette[ ($roww & 64) >> 6 ];
209 $out[] = $palette[ ($roww & 32) >> 5 ];
210 $out[] = $palette[ ($roww & 16) >> 4 ];
211 $out[] = $palette[ ($roww & 8) >> 3 ];
212 $out[] = $palette[ ($roww & 4) >> 2 ];
213 $out[] = $palette[ ($roww & 2) >> 1 ];
214 $out[] = $palette[ ($roww & 1) ];
215 }
216 return $out;
217 }
218 }
219
220 private static function makepixel($img, $x, $y, $str, $bps)
221 {
222 switch ($bps)
223 {
224 case 32 : $a = ord($str[0]);
225 $b = ord($str[1]);
226 $c = ord($str[2]);
227 $d = 256 - ord($str[3]);
228 $pixel = $d*256*256*256 + $c*256*256 + $b*256 + $a;
229 imagesetpixel($img, $x, $y, $pixel);
230 break;
231 case 24 : $a = ord($str[0]);
232 $b = ord($str[1]);
233 $c = ord($str[2]);
234 $pixel = $c*256*256 + $b*256 + $a;
235 imagesetpixel($img, $x, $y, $pixel);
236 break;
237 case 8 :
238 case 4 :
239 case 1 : imagesetpixel($img, $x, $y, $str);
240 break;
241 }
242 }
243
244 private static function byte3($n)
245 {
246 return chr($n & 255) . chr(($n >> 8) & 255) . chr(($n >> 16) & 255);
247 }
248
249 private static function undword($n)
250 {
251 $r = unpack("V", $n);
252 return $r[1];
253 }
254
255 private static function dword($n)
256 {
257 return pack("V", $n);
258 }
259
260 private static function word($n)
261 {
262 return pack("v", $n);
263 }
264 }
265