22 {
23
24 $info = &$this->getid3->info;
25
26
27 $info[
'png'] = array();
28 $thisfile_png = &
$info[
'png'];
29
30 $info[
'fileformat'] =
'png';
31 $info[
'video'][
'dataformat'] =
'png';
32 $info[
'video'][
'lossless'] =
false;
33
35 $PNGfiledata = $this->
fread($this->getid3->fread_buffer_size());
36 $offset = 0;
37
38 $PNGidentifier = substr($PNGfiledata, $offset, 8);
39 $offset += 8;
40
41 if ($PNGidentifier != "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A") {
43 unset(
$info[
'fileformat']);
44 return false;
45 }
46
47 while ((($this->
ftell() - (strlen($PNGfiledata) - $offset)) <
$info[
'filesize'])) {
49 if ($chunk['data_length'] === false) {
50 $this->
error(
'Failed to read data_length at offset '.$offset);
51 return false;
52 }
53 $offset += 4;
54 $truncated_data = false;
55 while (((strlen($PNGfiledata) - $offset) < ($chunk[
'data_length'] + 4)) && ($this->
ftell() <
$info[
'filesize'])) {
56 if (strlen($PNGfiledata) < $this->max_data_bytes) {
57 $PNGfiledata .= $this->
fread($this->getid3->fread_buffer_size());
58 } else {
59 $this->
warning(
'At offset '.$offset.
' chunk "'.substr($PNGfiledata, $offset, 4).
'" exceeded max_data_bytes value of '.$this->max_data_bytes.
', data chunk will be truncated at '.(strlen($PNGfiledata) - 8).
' bytes');
60 break;
61 }
62 }
63 $chunk['type_text'] = substr($PNGfiledata, $offset, 4);
64 $offset += 4;
66 $chunk['data'] = substr($PNGfiledata, $offset, $chunk['data_length']);
67 $offset += $chunk['data_length'];
69 $offset += 4;
70
71 $chunk['flags']['ancilliary'] = (bool) ($chunk['type_raw'] & 0x20000000);
72 $chunk['flags']['private'] = (bool) ($chunk['type_raw'] & 0x00200000);
73 $chunk['flags']['reserved'] = (bool) ($chunk['type_raw'] & 0x00002000);
74 $chunk['flags']['safe_to_copy'] = (bool) ($chunk['type_raw'] & 0x00000020);
75
76
77 $thisfile_png[$chunk['type_text']] = array();
78 $thisfile_png_chunk_type_text = &$thisfile_png[$chunk['type_text']];
79
80 switch ($chunk['type_text']) {
81
82 case 'IHDR':
83 $thisfile_png_chunk_type_text['header'] = $chunk;
91
92 $thisfile_png_chunk_type_text[
'compression_method_text'] = $this->
PNGcompressionMethodLookup($thisfile_png_chunk_type_text[
'raw'][
'compression_method']);
93 $thisfile_png_chunk_type_text['color_type']['palette'] = (bool) ($thisfile_png_chunk_type_text['raw']['color_type'] & 0x01);
94 $thisfile_png_chunk_type_text['color_type']['true_color'] = (bool) ($thisfile_png_chunk_type_text['raw']['color_type'] & 0x02);
95 $thisfile_png_chunk_type_text['color_type']['alpha'] = (bool) ($thisfile_png_chunk_type_text['raw']['color_type'] & 0x04);
96
97 $info[
'video'][
'resolution_x'] = $thisfile_png_chunk_type_text[
'width'];
98 $info[
'video'][
'resolution_y'] = $thisfile_png_chunk_type_text[
'height'];
99
100 $info[
'video'][
'bits_per_sample'] = $this->
IHDRcalculateBitsPerSample($thisfile_png_chunk_type_text[
'raw'][
'color_type'], $thisfile_png_chunk_type_text[
'raw'][
'bit_depth']);
101 break;
102
103
104 case 'PLTE':
105 $thisfile_png_chunk_type_text['header'] = $chunk;
106 $paletteoffset = 0;
107 for (
$i = 0;
$i <= 255;
$i++) {
108
109
110
115 }
116 break;
117
118
119 case 'tRNS':
120 $thisfile_png_chunk_type_text['header'] = $chunk;
121 switch ($thisfile_png['IHDR']['raw']['color_type']) {
122 case 0:
124 break;
125
126 case 2:
130 break;
131
132 case 3:
133 for (
$i = 0;
$i < strlen($chunk[
'data']);
$i++) {
135 }
136 break;
137
138 case 4:
139 case 6:
140 $this->
error(
'Invalid color_type in tRNS chunk: '.$thisfile_png[
'IHDR'][
'raw'][
'color_type']);
141
142 default:
143 $this->
warning(
'Unhandled color_type in tRNS chunk: '.$thisfile_png[
'IHDR'][
'raw'][
'color_type']);
144 break;
145 }
146 break;
147
148
149 case 'gAMA':
150 $thisfile_png_chunk_type_text['header'] = $chunk;
152 break;
153
154
155 case 'cHRM':
156 $thisfile_png_chunk_type_text['header'] = $chunk;
165 break;
166
167
168 case 'sRGB':
169 $thisfile_png_chunk_type_text['header'] = $chunk;
171 $thisfile_png_chunk_type_text[
'reindering_intent_text'] = $this->
PNGsRGBintentLookup($thisfile_png_chunk_type_text[
'reindering_intent']);
172 break;
173
174
175 case 'iCCP':
176 $thisfile_png_chunk_type_text['header'] = $chunk;
177 list($profilename, $compressiondata) = explode("\x00", $chunk['data'], 2);
178 $thisfile_png_chunk_type_text['profile_name'] = $profilename;
180 $thisfile_png_chunk_type_text['compression_profile'] = substr($compressiondata, 1);
181
182 $thisfile_png_chunk_type_text[
'compression_method_text'] = $this->
PNGcompressionMethodLookup($thisfile_png_chunk_type_text[
'compression_method']);
183 break;
184
185
186 case 'tEXt':
187 $thisfile_png_chunk_type_text['header'] = $chunk;
188 list($keyword,
$text) = explode(
"\x00", $chunk[
'data'], 2);
189 $thisfile_png_chunk_type_text['keyword'] = $keyword;
190 $thisfile_png_chunk_type_text[
'text'] =
$text;
191
192 $thisfile_png['comments'][$thisfile_png_chunk_type_text['keyword']][] = $thisfile_png_chunk_type_text['text'];
193 break;
194
195
196 case 'zTXt':
197 $thisfile_png_chunk_type_text['header'] = $chunk;
198 list($keyword, $otherdata) = explode("\x00", $chunk['data'], 2);
199 $thisfile_png_chunk_type_text['keyword'] = $keyword;
201 $thisfile_png_chunk_type_text['compressed_text'] = substr($otherdata, 1);
202 $thisfile_png_chunk_type_text[
'compression_method_text'] = $this->
PNGcompressionMethodLookup($thisfile_png_chunk_type_text[
'compression_method']);
203 switch ($thisfile_png_chunk_type_text['compression_method']) {
204 case 0:
205 $thisfile_png_chunk_type_text['text'] = gzuncompress($thisfile_png_chunk_type_text['compressed_text']);
206 break;
207
208 default:
209
210 break;
211 }
212
213 if (isset($thisfile_png_chunk_type_text['text'])) {
214 $thisfile_png['comments'][$thisfile_png_chunk_type_text['keyword']][] = $thisfile_png_chunk_type_text['text'];
215 }
216 break;
217
218
219 case 'iTXt':
220 $thisfile_png_chunk_type_text['header'] = $chunk;
221 list($keyword, $otherdata) = explode("\x00", $chunk['data'], 2);
222 $thisfile_png_chunk_type_text['keyword'] = $keyword;
225 $thisfile_png_chunk_type_text[
'compression_method_text'] = $this->
PNGcompressionMethodLookup($thisfile_png_chunk_type_text[
'compression_method']);
226 list($languagetag, $translatedkeyword,
$text) = explode(
"\x00", substr($otherdata, 2), 3);
227 $thisfile_png_chunk_type_text['language_tag'] = $languagetag;
228 $thisfile_png_chunk_type_text['translated_keyword'] = $translatedkeyword;
229
230 if ($thisfile_png_chunk_type_text['compression']) {
231
232 switch ($thisfile_png_chunk_type_text['compression_method']) {
233 case 0:
234 $thisfile_png_chunk_type_text[
'text'] = gzuncompress(
$text);
235 break;
236
237 default:
238
239 break;
240 }
241
242 } else {
243
244 $thisfile_png_chunk_type_text[
'text'] =
$text;
245
246 }
247
248 if (isset($thisfile_png_chunk_type_text['text'])) {
249 $thisfile_png['comments'][$thisfile_png_chunk_type_text['keyword']][] = $thisfile_png_chunk_type_text['text'];
250 }
251 break;
252
253
254 case 'bKGD':
255 $thisfile_png_chunk_type_text['header'] = $chunk;
256 switch ($thisfile_png['IHDR']['raw']['color_type']) {
257 case 0:
258 case 4:
260 break;
261
262 case 2:
263 case 6:
264 $thisfile_png_chunk_type_text[
'background_red'] =
getid3_lib::BigEndian2Int(substr($chunk[
'data'], 0 * $thisfile_png[
'IHDR'][
'raw'][
'bit_depth'], $thisfile_png[
'IHDR'][
'raw'][
'bit_depth']));
265 $thisfile_png_chunk_type_text[
'background_green'] =
getid3_lib::BigEndian2Int(substr($chunk[
'data'], 1 * $thisfile_png[
'IHDR'][
'raw'][
'bit_depth'], $thisfile_png[
'IHDR'][
'raw'][
'bit_depth']));
266 $thisfile_png_chunk_type_text[
'background_blue'] =
getid3_lib::BigEndian2Int(substr($chunk[
'data'], 2 * $thisfile_png[
'IHDR'][
'raw'][
'bit_depth'], $thisfile_png[
'IHDR'][
'raw'][
'bit_depth']));
267 break;
268
269 case 3:
271 break;
272
273 default:
274 break;
275 }
276 break;
277
278
279 case 'pHYs':
280 $thisfile_png_chunk_type_text['header'] = $chunk;
284 $thisfile_png_chunk_type_text[
'unit'] = $this->
PNGpHYsUnitLookup($thisfile_png_chunk_type_text[
'unit_specifier']);
285 break;
286
287
288 case 'sBIT':
289 $thisfile_png_chunk_type_text['header'] = $chunk;
290 switch ($thisfile_png['IHDR']['raw']['color_type']) {
291 case 0:
293 break;
294
295 case 2:
296 case 3:
300 break;
301
302 case 4:
305 break;
306
307 case 6:
312 break;
313
314 default:
315 break;
316 }
317 break;
318
319
320 case 'sPLT':
321 $thisfile_png_chunk_type_text['header'] = $chunk;
322 list($palettename, $otherdata) = explode("\x00", $chunk['data'], 2);
323 $thisfile_png_chunk_type_text['palette_name'] = $palettename;
324 $sPLToffset = 0;
326 $sPLToffset += 1;
327 $thisfile_png_chunk_type_text['sample_depth_bytes'] = $thisfile_png_chunk_type_text['sample_depth_bits'] / 8;
328 $paletteCounter = 0;
329 while ($sPLToffset < strlen($otherdata)) {
330 $thisfile_png_chunk_type_text[
'red'][$paletteCounter] =
getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, $thisfile_png_chunk_type_text[
'sample_depth_bytes']));
331 $sPLToffset += $thisfile_png_chunk_type_text['sample_depth_bytes'];
332 $thisfile_png_chunk_type_text[
'green'][$paletteCounter] =
getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, $thisfile_png_chunk_type_text[
'sample_depth_bytes']));
333 $sPLToffset += $thisfile_png_chunk_type_text['sample_depth_bytes'];
334 $thisfile_png_chunk_type_text[
'blue'][$paletteCounter] =
getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, $thisfile_png_chunk_type_text[
'sample_depth_bytes']));
335 $sPLToffset += $thisfile_png_chunk_type_text['sample_depth_bytes'];
336 $thisfile_png_chunk_type_text[
'alpha'][$paletteCounter] =
getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, $thisfile_png_chunk_type_text[
'sample_depth_bytes']));
337 $sPLToffset += $thisfile_png_chunk_type_text['sample_depth_bytes'];
338 $thisfile_png_chunk_type_text[
'frequency'][$paletteCounter] =
getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, 2));
339 $sPLToffset += 2;
340 $paletteCounter++;
341 }
342 break;
343
344
345 case 'hIST':
346 $thisfile_png_chunk_type_text['header'] = $chunk;
347 $hISTcounter = 0;
348 while ($hISTcounter < strlen($chunk['data'])) {
350 $hISTcounter += 2;
351 }
352 break;
353
354
355 case 'tIME':
356 $thisfile_png_chunk_type_text['header'] = $chunk;
363 $thisfile_png_chunk_type_text['unix'] = gmmktime($thisfile_png_chunk_type_text['hour'], $thisfile_png_chunk_type_text['minute'], $thisfile_png_chunk_type_text['second'], $thisfile_png_chunk_type_text['month'], $thisfile_png_chunk_type_text['day'], $thisfile_png_chunk_type_text['year']);
364 break;
365
366
367 case 'oFFs':
368 $thisfile_png_chunk_type_text['header'] = $chunk;
372 $thisfile_png_chunk_type_text[
'unit'] = $this->
PNGoFFsUnitLookup($thisfile_png_chunk_type_text[
'unit_specifier']);
373 break;
374
375
376 case 'pCAL':
377 $thisfile_png_chunk_type_text['header'] = $chunk;
378 list($calibrationname, $otherdata) = explode("\x00", $chunk['data'], 2);
379 $thisfile_png_chunk_type_text['calibration_name'] = $calibrationname;
380 $pCALoffset = 0;
381 $thisfile_png_chunk_type_text[
'original_zero'] =
getid3_lib::BigEndian2Int(substr($chunk[
'data'], $pCALoffset, 4),
false,
true);
382 $pCALoffset += 4;
383 $thisfile_png_chunk_type_text[
'original_max'] =
getid3_lib::BigEndian2Int(substr($chunk[
'data'], $pCALoffset, 4),
false,
true);
384 $pCALoffset += 4;
386 $pCALoffset += 1;
387 $thisfile_png_chunk_type_text[
'equation_type_text'] = $this->
PNGpCALequationTypeLookup($thisfile_png_chunk_type_text[
'equation_type']);
389 $pCALoffset += 1;
390 $thisfile_png_chunk_type_text['parameters'] = explode("\x00", substr($chunk['data'], $pCALoffset));
391 break;
392
393
394 case 'sCAL':
395 $thisfile_png_chunk_type_text['header'] = $chunk;
397 $thisfile_png_chunk_type_text[
'unit'] = $this->
PNGsCALUnitLookup($thisfile_png_chunk_type_text[
'unit_specifier']);
398 list($pixelwidth, $pixelheight) = explode("\x00", substr($chunk['data'], 1));
399 $thisfile_png_chunk_type_text['pixel_width'] = $pixelwidth;
400 $thisfile_png_chunk_type_text['pixel_height'] = $pixelheight;
401 break;
402
403
404 case 'gIFg':
405 $gIFgCounter = 0;
406 if (isset($thisfile_png_chunk_type_text) && is_array($thisfile_png_chunk_type_text)) {
407 $gIFgCounter = count($thisfile_png_chunk_type_text);
408 }
409 $thisfile_png_chunk_type_text[$gIFgCounter]['header'] = $chunk;
410 $thisfile_png_chunk_type_text[$gIFgCounter][
'disposal_method'] =
getid3_lib::BigEndian2Int(substr($chunk[
'data'], 0, 1));
411 $thisfile_png_chunk_type_text[$gIFgCounter][
'user_input_flag'] =
getid3_lib::BigEndian2Int(substr($chunk[
'data'], 1, 1));
413 break;
414
415
416 case 'gIFx':
417 $gIFxCounter = 0;
418 if (isset($thisfile_png_chunk_type_text) && is_array($thisfile_png_chunk_type_text)) {
419 $gIFxCounter = count($thisfile_png_chunk_type_text);
420 }
421 $thisfile_png_chunk_type_text[$gIFxCounter]['header'] = $chunk;
422 $thisfile_png_chunk_type_text[$gIFxCounter]['application_identifier'] = substr($chunk['data'], 0, 8);
423 $thisfile_png_chunk_type_text[$gIFxCounter]['authentication_code'] = substr($chunk['data'], 8, 3);
424 $thisfile_png_chunk_type_text[$gIFxCounter]['application_data'] = substr($chunk['data'], 11);
425 break;
426
427
428 case 'IDAT':
429 $idatinformationfieldindex = 0;
430 if (isset($thisfile_png['IDAT']) && is_array($thisfile_png['IDAT'])) {
431 $idatinformationfieldindex = count($thisfile_png['IDAT']);
432 }
433 unset($chunk['data']);
434 $thisfile_png_chunk_type_text[$idatinformationfieldindex]['header'] = $chunk;
435 break;
436
437
438 case 'IEND':
439 $thisfile_png_chunk_type_text['header'] = $chunk;
440 break;
441
442
443 default:
444
445 $thisfile_png_chunk_type_text['header'] = $chunk;
446 $this->
warning(
'Unhandled chunk type: '.$chunk[
'type_text']);
447 break;
448 }
449 }
450
451 return true;
452 }
fseek($bytes, $whence=SEEK_SET)
static PrintHexBytes($string, $hex=true, $spaces=true, $htmlencoding='UTF-8')
static BigEndian2Int($byteword, $synchsafe=false, $signed=false)
PNGsRGBintentLookup($sRGB)
IHDRcalculateBitsPerSample($color_type, $bit_depth)
PNGpHYsUnitLookup($unitid)
PNGpCALequationTypeLookup($equationtype)
PNGcompressionMethodLookup($compressionmethod)
PNGoFFsUnitLookup($unitid)
PNGsCALUnitLookup($unitid)