module.graphic.png.php 29 KB

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