module.graphic.png.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at http://getid3.sourceforge.net //
  5. // or http://www.getid3.org //
  6. // also https://github.com/JamesHeinrich/getID3 //
  7. /////////////////////////////////////////////////////////////////
  8. // See readme.txt for more details //
  9. /////////////////////////////////////////////////////////////////
  10. // //
  11. // module.graphic.png.php //
  12. // module for analyzing PNG Image files //
  13. // dependencies: NONE //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. class getid3_png extends getid3_handler
  17. {
  18. public $max_data_bytes = 10000000; // if data chunk is larger than this do not read it completely (getID3 only needs the first few dozen bytes for parsing)
  19. public function Analyze() {
  20. $info = &$this->getid3->info;
  21. // shortcut
  22. $info['png'] = array();
  23. $thisfile_png = &$info['png'];
  24. $info['fileformat'] = 'png';
  25. $info['video']['dataformat'] = 'png';
  26. $info['video']['lossless'] = false;
  27. $this->fseek($info['avdataoffset']);
  28. $PNGfiledata = $this->fread($this->getid3->fread_buffer_size());
  29. $offset = 0;
  30. $PNGidentifier = substr($PNGfiledata, $offset, 8); // $89 $50 $4E $47 $0D $0A $1A $0A
  31. $offset += 8;
  32. if ($PNGidentifier != "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A") {
  33. $info['error'][] = 'First 8 bytes of file ('.getid3_lib::PrintHexBytes($PNGidentifier).') did not match expected PNG identifier';
  34. unset($info['fileformat']);
  35. return false;
  36. }
  37. while ((($this->ftell() - (strlen($PNGfiledata) - $offset)) < $info['filesize'])) {
  38. $chunk['data_length'] = getid3_lib::BigEndian2Int(substr($PNGfiledata, $offset, 4));
  39. if ($chunk['data_length'] === false) {
  40. $info['error'][] = 'Failed to read data_length at offset '.$offset;
  41. return false;
  42. }
  43. $offset += 4;
  44. $truncated_data = false;
  45. while (((strlen($PNGfiledata) - $offset) < ($chunk['data_length'] + 4)) && ($this->ftell() < $info['filesize'])) {
  46. if (strlen($PNGfiledata) < $this->max_data_bytes) {
  47. $PNGfiledata .= $this->fread($this->getid3->fread_buffer_size());
  48. } else {
  49. $info['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';
  50. break;
  51. }
  52. }
  53. $chunk['type_text'] = substr($PNGfiledata, $offset, 4);
  54. $offset += 4;
  55. $chunk['type_raw'] = getid3_lib::BigEndian2Int($chunk['type_text']);
  56. $chunk['data'] = substr($PNGfiledata, $offset, $chunk['data_length']);
  57. $offset += $chunk['data_length'];
  58. $chunk['crc'] = getid3_lib::BigEndian2Int(substr($PNGfiledata, $offset, 4));
  59. $offset += 4;
  60. $chunk['flags']['ancilliary'] = (bool) ($chunk['type_raw'] & 0x20000000);
  61. $chunk['flags']['private'] = (bool) ($chunk['type_raw'] & 0x00200000);
  62. $chunk['flags']['reserved'] = (bool) ($chunk['type_raw'] & 0x00002000);
  63. $chunk['flags']['safe_to_copy'] = (bool) ($chunk['type_raw'] & 0x00000020);
  64. // shortcut
  65. $thisfile_png[$chunk['type_text']] = array();
  66. $thisfile_png_chunk_type_text = &$thisfile_png[$chunk['type_text']];
  67. switch ($chunk['type_text']) {
  68. case 'IHDR': // Image Header
  69. $thisfile_png_chunk_type_text['header'] = $chunk;
  70. $thisfile_png_chunk_type_text['width'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 4));
  71. $thisfile_png_chunk_type_text['height'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 4));
  72. $thisfile_png_chunk_type_text['raw']['bit_depth'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 8, 1));
  73. $thisfile_png_chunk_type_text['raw']['color_type'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 9, 1));
  74. $thisfile_png_chunk_type_text['raw']['compression_method'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 10, 1));
  75. $thisfile_png_chunk_type_text['raw']['filter_method'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 11, 1));
  76. $thisfile_png_chunk_type_text['raw']['interlace_method'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 12, 1));
  77. $thisfile_png_chunk_type_text['compression_method_text'] = $this->PNGcompressionMethodLookup($thisfile_png_chunk_type_text['raw']['compression_method']);
  78. $thisfile_png_chunk_type_text['color_type']['palette'] = (bool) ($thisfile_png_chunk_type_text['raw']['color_type'] & 0x01);
  79. $thisfile_png_chunk_type_text['color_type']['true_color'] = (bool) ($thisfile_png_chunk_type_text['raw']['color_type'] & 0x02);
  80. $thisfile_png_chunk_type_text['color_type']['alpha'] = (bool) ($thisfile_png_chunk_type_text['raw']['color_type'] & 0x04);
  81. $info['video']['resolution_x'] = $thisfile_png_chunk_type_text['width'];
  82. $info['video']['resolution_y'] = $thisfile_png_chunk_type_text['height'];
  83. $info['video']['bits_per_sample'] = $this->IHDRcalculateBitsPerSample($thisfile_png_chunk_type_text['raw']['color_type'], $thisfile_png_chunk_type_text['raw']['bit_depth']);
  84. break;
  85. case 'PLTE': // Palette
  86. $thisfile_png_chunk_type_text['header'] = $chunk;
  87. $paletteoffset = 0;
  88. for ($i = 0; $i <= 255; $i++) {
  89. //$thisfile_png_chunk_type_text['red'][$i] = getid3_lib::BigEndian2Int(substr($chunk['data'], $paletteoffset++, 1));
  90. //$thisfile_png_chunk_type_text['green'][$i] = getid3_lib::BigEndian2Int(substr($chunk['data'], $paletteoffset++, 1));
  91. //$thisfile_png_chunk_type_text['blue'][$i] = getid3_lib::BigEndian2Int(substr($chunk['data'], $paletteoffset++, 1));
  92. $red = getid3_lib::BigEndian2Int(substr($chunk['data'], $paletteoffset++, 1));
  93. $green = getid3_lib::BigEndian2Int(substr($chunk['data'], $paletteoffset++, 1));
  94. $blue = getid3_lib::BigEndian2Int(substr($chunk['data'], $paletteoffset++, 1));
  95. $thisfile_png_chunk_type_text[$i] = (($red << 16) | ($green << 8) | ($blue));
  96. }
  97. break;
  98. case 'tRNS': // Transparency
  99. $thisfile_png_chunk_type_text['header'] = $chunk;
  100. switch ($thisfile_png['IHDR']['raw']['color_type']) {
  101. case 0:
  102. $thisfile_png_chunk_type_text['transparent_color_gray'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 2));
  103. break;
  104. case 2:
  105. $thisfile_png_chunk_type_text['transparent_color_red'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 2));
  106. $thisfile_png_chunk_type_text['transparent_color_green'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 2, 2));
  107. $thisfile_png_chunk_type_text['transparent_color_blue'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 2));
  108. break;
  109. case 3:
  110. for ($i = 0; $i < strlen($chunk['data']); $i++) {
  111. $thisfile_png_chunk_type_text['palette_opacity'][$i] = getid3_lib::BigEndian2Int(substr($chunk['data'], $i, 1));
  112. }
  113. break;
  114. case 4:
  115. case 6:
  116. $info['error'][] = 'Invalid color_type in tRNS chunk: '.$thisfile_png['IHDR']['raw']['color_type'];
  117. default:
  118. $info['warning'][] = 'Unhandled color_type in tRNS chunk: '.$thisfile_png['IHDR']['raw']['color_type'];
  119. break;
  120. }
  121. break;
  122. case 'gAMA': // Image Gamma
  123. $thisfile_png_chunk_type_text['header'] = $chunk;
  124. $thisfile_png_chunk_type_text['gamma'] = getid3_lib::BigEndian2Int($chunk['data']) / 100000;
  125. break;
  126. case 'cHRM': // Primary Chromaticities
  127. $thisfile_png_chunk_type_text['header'] = $chunk;
  128. $thisfile_png_chunk_type_text['white_x'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 4)) / 100000;
  129. $thisfile_png_chunk_type_text['white_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 4)) / 100000;
  130. $thisfile_png_chunk_type_text['red_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 8, 4)) / 100000;
  131. $thisfile_png_chunk_type_text['red_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 12, 4)) / 100000;
  132. $thisfile_png_chunk_type_text['green_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 16, 4)) / 100000;
  133. $thisfile_png_chunk_type_text['green_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 20, 4)) / 100000;
  134. $thisfile_png_chunk_type_text['blue_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 24, 4)) / 100000;
  135. $thisfile_png_chunk_type_text['blue_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 28, 4)) / 100000;
  136. break;
  137. case 'sRGB': // Standard RGB Color Space
  138. $thisfile_png_chunk_type_text['header'] = $chunk;
  139. $thisfile_png_chunk_type_text['reindering_intent'] = getid3_lib::BigEndian2Int($chunk['data']);
  140. $thisfile_png_chunk_type_text['reindering_intent_text'] = $this->PNGsRGBintentLookup($thisfile_png_chunk_type_text['reindering_intent']);
  141. break;
  142. case 'iCCP': // Embedded ICC Profile
  143. $thisfile_png_chunk_type_text['header'] = $chunk;
  144. list($profilename, $compressiondata) = explode("\x00", $chunk['data'], 2);
  145. $thisfile_png_chunk_type_text['profile_name'] = $profilename;
  146. $thisfile_png_chunk_type_text['compression_method'] = getid3_lib::BigEndian2Int(substr($compressiondata, 0, 1));
  147. $thisfile_png_chunk_type_text['compression_profile'] = substr($compressiondata, 1);
  148. $thisfile_png_chunk_type_text['compression_method_text'] = $this->PNGcompressionMethodLookup($thisfile_png_chunk_type_text['compression_method']);
  149. break;
  150. case 'tEXt': // Textual Data
  151. $thisfile_png_chunk_type_text['header'] = $chunk;
  152. list($keyword, $text) = explode("\x00", $chunk['data'], 2);
  153. $thisfile_png_chunk_type_text['keyword'] = $keyword;
  154. $thisfile_png_chunk_type_text['text'] = $text;
  155. $thisfile_png['comments'][$thisfile_png_chunk_type_text['keyword']][] = $thisfile_png_chunk_type_text['text'];
  156. break;
  157. case 'zTXt': // Compressed Textual Data
  158. $thisfile_png_chunk_type_text['header'] = $chunk;
  159. list($keyword, $otherdata) = explode("\x00", $chunk['data'], 2);
  160. $thisfile_png_chunk_type_text['keyword'] = $keyword;
  161. $thisfile_png_chunk_type_text['compression_method'] = getid3_lib::BigEndian2Int(substr($otherdata, 0, 1));
  162. $thisfile_png_chunk_type_text['compressed_text'] = substr($otherdata, 1);
  163. $thisfile_png_chunk_type_text['compression_method_text'] = $this->PNGcompressionMethodLookup($thisfile_png_chunk_type_text['compression_method']);
  164. switch ($thisfile_png_chunk_type_text['compression_method']) {
  165. case 0:
  166. $thisfile_png_chunk_type_text['text'] = gzuncompress($thisfile_png_chunk_type_text['compressed_text']);
  167. break;
  168. default:
  169. // unknown compression method
  170. break;
  171. }
  172. if (isset($thisfile_png_chunk_type_text['text'])) {
  173. $thisfile_png['comments'][$thisfile_png_chunk_type_text['keyword']][] = $thisfile_png_chunk_type_text['text'];
  174. }
  175. break;
  176. case 'iTXt': // International Textual Data
  177. $thisfile_png_chunk_type_text['header'] = $chunk;
  178. list($keyword, $otherdata) = explode("\x00", $chunk['data'], 2);
  179. $thisfile_png_chunk_type_text['keyword'] = $keyword;
  180. $thisfile_png_chunk_type_text['compression'] = (bool) getid3_lib::BigEndian2Int(substr($otherdata, 0, 1));
  181. $thisfile_png_chunk_type_text['compression_method'] = getid3_lib::BigEndian2Int(substr($otherdata, 1, 1));
  182. $thisfile_png_chunk_type_text['compression_method_text'] = $this->PNGcompressionMethodLookup($thisfile_png_chunk_type_text['compression_method']);
  183. list($languagetag, $translatedkeyword, $text) = explode("\x00", substr($otherdata, 2), 3);
  184. $thisfile_png_chunk_type_text['language_tag'] = $languagetag;
  185. $thisfile_png_chunk_type_text['translated_keyword'] = $translatedkeyword;
  186. if ($thisfile_png_chunk_type_text['compression']) {
  187. switch ($thisfile_png_chunk_type_text['compression_method']) {
  188. case 0:
  189. $thisfile_png_chunk_type_text['text'] = gzuncompress($text);
  190. break;
  191. default:
  192. // unknown compression method
  193. break;
  194. }
  195. } else {
  196. $thisfile_png_chunk_type_text['text'] = $text;
  197. }
  198. if (isset($thisfile_png_chunk_type_text['text'])) {
  199. $thisfile_png['comments'][$thisfile_png_chunk_type_text['keyword']][] = $thisfile_png_chunk_type_text['text'];
  200. }
  201. break;
  202. case 'bKGD': // Background Color
  203. $thisfile_png_chunk_type_text['header'] = $chunk;
  204. switch ($thisfile_png['IHDR']['raw']['color_type']) {
  205. case 0:
  206. case 4:
  207. $thisfile_png_chunk_type_text['background_gray'] = getid3_lib::BigEndian2Int($chunk['data']);
  208. break;
  209. case 2:
  210. case 6:
  211. $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']));
  212. $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']));
  213. $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']));
  214. break;
  215. case 3:
  216. $thisfile_png_chunk_type_text['background_index'] = getid3_lib::BigEndian2Int($chunk['data']);
  217. break;
  218. default:
  219. break;
  220. }
  221. break;
  222. case 'pHYs': // Physical Pixel Dimensions
  223. $thisfile_png_chunk_type_text['header'] = $chunk;
  224. $thisfile_png_chunk_type_text['pixels_per_unit_x'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 4));
  225. $thisfile_png_chunk_type_text['pixels_per_unit_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 4));
  226. $thisfile_png_chunk_type_text['unit_specifier'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 8, 1));
  227. $thisfile_png_chunk_type_text['unit'] = $this->PNGpHYsUnitLookup($thisfile_png_chunk_type_text['unit_specifier']);
  228. break;
  229. case 'sBIT': // Significant Bits
  230. $thisfile_png_chunk_type_text['header'] = $chunk;
  231. switch ($thisfile_png['IHDR']['raw']['color_type']) {
  232. case 0:
  233. $thisfile_png_chunk_type_text['significant_bits_gray'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
  234. break;
  235. case 2:
  236. case 3:
  237. $thisfile_png_chunk_type_text['significant_bits_red'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
  238. $thisfile_png_chunk_type_text['significant_bits_green'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 1, 1));
  239. $thisfile_png_chunk_type_text['significant_bits_blue'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 2, 1));
  240. break;
  241. case 4:
  242. $thisfile_png_chunk_type_text['significant_bits_gray'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
  243. $thisfile_png_chunk_type_text['significant_bits_alpha'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 1, 1));
  244. break;
  245. case 6:
  246. $thisfile_png_chunk_type_text['significant_bits_red'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
  247. $thisfile_png_chunk_type_text['significant_bits_green'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 1, 1));
  248. $thisfile_png_chunk_type_text['significant_bits_blue'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 2, 1));
  249. $thisfile_png_chunk_type_text['significant_bits_alpha'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 3, 1));
  250. break;
  251. default:
  252. break;
  253. }
  254. break;
  255. case 'sPLT': // Suggested Palette
  256. $thisfile_png_chunk_type_text['header'] = $chunk;
  257. list($palettename, $otherdata) = explode("\x00", $chunk['data'], 2);
  258. $thisfile_png_chunk_type_text['palette_name'] = $palettename;
  259. $sPLToffset = 0;
  260. $thisfile_png_chunk_type_text['sample_depth_bits'] = getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, 1));
  261. $sPLToffset += 1;
  262. $thisfile_png_chunk_type_text['sample_depth_bytes'] = $thisfile_png_chunk_type_text['sample_depth_bits'] / 8;
  263. $paletteCounter = 0;
  264. while ($sPLToffset < strlen($otherdata)) {
  265. $thisfile_png_chunk_type_text['red'][$paletteCounter] = getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, $thisfile_png_chunk_type_text['sample_depth_bytes']));
  266. $sPLToffset += $thisfile_png_chunk_type_text['sample_depth_bytes'];
  267. $thisfile_png_chunk_type_text['green'][$paletteCounter] = getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, $thisfile_png_chunk_type_text['sample_depth_bytes']));
  268. $sPLToffset += $thisfile_png_chunk_type_text['sample_depth_bytes'];
  269. $thisfile_png_chunk_type_text['blue'][$paletteCounter] = getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, $thisfile_png_chunk_type_text['sample_depth_bytes']));
  270. $sPLToffset += $thisfile_png_chunk_type_text['sample_depth_bytes'];
  271. $thisfile_png_chunk_type_text['alpha'][$paletteCounter] = getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, $thisfile_png_chunk_type_text['sample_depth_bytes']));
  272. $sPLToffset += $thisfile_png_chunk_type_text['sample_depth_bytes'];
  273. $thisfile_png_chunk_type_text['frequency'][$paletteCounter] = getid3_lib::BigEndian2Int(substr($otherdata, $sPLToffset, 2));
  274. $sPLToffset += 2;
  275. $paletteCounter++;
  276. }
  277. break;
  278. case 'hIST': // Palette Histogram
  279. $thisfile_png_chunk_type_text['header'] = $chunk;
  280. $hISTcounter = 0;
  281. while ($hISTcounter < strlen($chunk['data'])) {
  282. $thisfile_png_chunk_type_text[$hISTcounter] = getid3_lib::BigEndian2Int(substr($chunk['data'], $hISTcounter / 2, 2));
  283. $hISTcounter += 2;
  284. }
  285. break;
  286. case 'tIME': // Image Last-Modification Time
  287. $thisfile_png_chunk_type_text['header'] = $chunk;
  288. $thisfile_png_chunk_type_text['year'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 2));
  289. $thisfile_png_chunk_type_text['month'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 2, 1));
  290. $thisfile_png_chunk_type_text['day'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 3, 1));
  291. $thisfile_png_chunk_type_text['hour'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 1));
  292. $thisfile_png_chunk_type_text['minute'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 5, 1));
  293. $thisfile_png_chunk_type_text['second'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 6, 1));
  294. $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']);
  295. break;
  296. case 'oFFs': // Image Offset
  297. $thisfile_png_chunk_type_text['header'] = $chunk;
  298. $thisfile_png_chunk_type_text['position_x'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 4), false, true);
  299. $thisfile_png_chunk_type_text['position_y'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 4, 4), false, true);
  300. $thisfile_png_chunk_type_text['unit_specifier'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 8, 1));
  301. $thisfile_png_chunk_type_text['unit'] = $this->PNGoFFsUnitLookup($thisfile_png_chunk_type_text['unit_specifier']);
  302. break;
  303. case 'pCAL': // Calibration Of Pixel Values
  304. $thisfile_png_chunk_type_text['header'] = $chunk;
  305. list($calibrationname, $otherdata) = explode("\x00", $chunk['data'], 2);
  306. $thisfile_png_chunk_type_text['calibration_name'] = $calibrationname;
  307. $pCALoffset = 0;
  308. $thisfile_png_chunk_type_text['original_zero'] = getid3_lib::BigEndian2Int(substr($chunk['data'], $pCALoffset, 4), false, true);
  309. $pCALoffset += 4;
  310. $thisfile_png_chunk_type_text['original_max'] = getid3_lib::BigEndian2Int(substr($chunk['data'], $pCALoffset, 4), false, true);
  311. $pCALoffset += 4;
  312. $thisfile_png_chunk_type_text['equation_type'] = getid3_lib::BigEndian2Int(substr($chunk['data'], $pCALoffset, 1));
  313. $pCALoffset += 1;
  314. $thisfile_png_chunk_type_text['equation_type_text'] = $this->PNGpCALequationTypeLookup($thisfile_png_chunk_type_text['equation_type']);
  315. $thisfile_png_chunk_type_text['parameter_count'] = getid3_lib::BigEndian2Int(substr($chunk['data'], $pCALoffset, 1));
  316. $pCALoffset += 1;
  317. $thisfile_png_chunk_type_text['parameters'] = explode("\x00", substr($chunk['data'], $pCALoffset));
  318. break;
  319. case 'sCAL': // Physical Scale Of Image Subject
  320. $thisfile_png_chunk_type_text['header'] = $chunk;
  321. $thisfile_png_chunk_type_text['unit_specifier'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
  322. $thisfile_png_chunk_type_text['unit'] = $this->PNGsCALUnitLookup($thisfile_png_chunk_type_text['unit_specifier']);
  323. list($pixelwidth, $pixelheight) = explode("\x00", substr($chunk['data'], 1));
  324. $thisfile_png_chunk_type_text['pixel_width'] = $pixelwidth;
  325. $thisfile_png_chunk_type_text['pixel_height'] = $pixelheight;
  326. break;
  327. case 'gIFg': // GIF Graphic Control Extension
  328. $gIFgCounter = 0;
  329. if (isset($thisfile_png_chunk_type_text) && is_array($thisfile_png_chunk_type_text)) {
  330. $gIFgCounter = count($thisfile_png_chunk_type_text);
  331. }
  332. $thisfile_png_chunk_type_text[$gIFgCounter]['header'] = $chunk;
  333. $thisfile_png_chunk_type_text[$gIFgCounter]['disposal_method'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 0, 1));
  334. $thisfile_png_chunk_type_text[$gIFgCounter]['user_input_flag'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 1, 1));
  335. $thisfile_png_chunk_type_text[$gIFgCounter]['delay_time'] = getid3_lib::BigEndian2Int(substr($chunk['data'], 2, 2));
  336. break;
  337. case 'gIFx': // GIF Application Extension
  338. $gIFxCounter = 0;
  339. if (isset($thisfile_png_chunk_type_text) && is_array($thisfile_png_chunk_type_text)) {
  340. $gIFxCounter = count($thisfile_png_chunk_type_text);
  341. }
  342. $thisfile_png_chunk_type_text[$gIFxCounter]['header'] = $chunk;
  343. $thisfile_png_chunk_type_text[$gIFxCounter]['application_identifier'] = substr($chunk['data'], 0, 8);
  344. $thisfile_png_chunk_type_text[$gIFxCounter]['authentication_code'] = substr($chunk['data'], 8, 3);
  345. $thisfile_png_chunk_type_text[$gIFxCounter]['application_data'] = substr($chunk['data'], 11);
  346. break;
  347. case 'IDAT': // Image Data
  348. $idatinformationfieldindex = 0;
  349. if (isset($thisfile_png['IDAT']) && is_array($thisfile_png['IDAT'])) {
  350. $idatinformationfieldindex = count($thisfile_png['IDAT']);
  351. }
  352. unset($chunk['data']);
  353. $thisfile_png_chunk_type_text[$idatinformationfieldindex]['header'] = $chunk;
  354. break;
  355. case 'IEND': // Image Trailer
  356. $thisfile_png_chunk_type_text['header'] = $chunk;
  357. break;
  358. default:
  359. //unset($chunk['data']);
  360. $thisfile_png_chunk_type_text['header'] = $chunk;
  361. $info['warning'][] = 'Unhandled chunk type: '.$chunk['type_text'];
  362. break;
  363. }
  364. }
  365. return true;
  366. }
  367. public function PNGsRGBintentLookup($sRGB) {
  368. static $PNGsRGBintentLookup = array(
  369. 0 => 'Perceptual',
  370. 1 => 'Relative colorimetric',
  371. 2 => 'Saturation',
  372. 3 => 'Absolute colorimetric'
  373. );
  374. return (isset($PNGsRGBintentLookup[$sRGB]) ? $PNGsRGBintentLookup[$sRGB] : 'invalid');
  375. }
  376. public function PNGcompressionMethodLookup($compressionmethod) {
  377. static $PNGcompressionMethodLookup = array(
  378. 0 => 'deflate/inflate'
  379. );
  380. return (isset($PNGcompressionMethodLookup[$compressionmethod]) ? $PNGcompressionMethodLookup[$compressionmethod] : 'invalid');
  381. }
  382. public function PNGpHYsUnitLookup($unitid) {
  383. static $PNGpHYsUnitLookup = array(
  384. 0 => 'unknown',
  385. 1 => 'meter'
  386. );
  387. return (isset($PNGpHYsUnitLookup[$unitid]) ? $PNGpHYsUnitLookup[$unitid] : 'invalid');
  388. }
  389. public function PNGoFFsUnitLookup($unitid) {
  390. static $PNGoFFsUnitLookup = array(
  391. 0 => 'pixel',
  392. 1 => 'micrometer'
  393. );
  394. return (isset($PNGoFFsUnitLookup[$unitid]) ? $PNGoFFsUnitLookup[$unitid] : 'invalid');
  395. }
  396. public function PNGpCALequationTypeLookup($equationtype) {
  397. static $PNGpCALequationTypeLookup = array(
  398. 0 => 'Linear mapping',
  399. 1 => 'Base-e exponential mapping',
  400. 2 => 'Arbitrary-base exponential mapping',
  401. 3 => 'Hyperbolic mapping'
  402. );
  403. return (isset($PNGpCALequationTypeLookup[$equationtype]) ? $PNGpCALequationTypeLookup[$equationtype] : 'invalid');
  404. }
  405. public function PNGsCALUnitLookup($unitid) {
  406. static $PNGsCALUnitLookup = array(
  407. 0 => 'meter',
  408. 1 => 'radian'
  409. );
  410. return (isset($PNGsCALUnitLookup[$unitid]) ? $PNGsCALUnitLookup[$unitid] : 'invalid');
  411. }
  412. public function IHDRcalculateBitsPerSample($color_type, $bit_depth) {
  413. switch ($color_type) {
  414. case 0: // Each pixel is a grayscale sample.
  415. return $bit_depth;
  416. break;
  417. case 2: // Each pixel is an R,G,B triple
  418. return 3 * $bit_depth;
  419. break;
  420. case 3: // Each pixel is a palette index; a PLTE chunk must appear.
  421. return $bit_depth;
  422. break;
  423. case 4: // Each pixel is a grayscale sample, followed by an alpha sample.
  424. return 2 * $bit_depth;
  425. break;
  426. case 6: // Each pixel is an R,G,B triple, followed by an alpha sample.
  427. return 4 * $bit_depth;
  428. break;
  429. }
  430. return false;
  431. }
  432. }