module.graphic.png.php 29 KB

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