module.graphic.gif.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.gif.php //
  11. // module for analyzing GIF Image files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. /**
  16. * @link https://www.w3.org/Graphics/GIF/spec-gif89a.txt
  17. * @link http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
  18. */
  19. if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
  20. exit;
  21. }
  22. class getid3_gif extends getid3_handler
  23. {
  24. /**
  25. * @return bool
  26. */
  27. public function Analyze() {
  28. $info = &$this->getid3->info;
  29. $info['fileformat'] = 'gif';
  30. $info['video']['dataformat'] = 'gif';
  31. $info['video']['lossless'] = true;
  32. $info['video']['pixel_aspect_ratio'] = (float) 1;
  33. $this->fseek($info['avdataoffset']);
  34. $GIFheader = $this->fread(13);
  35. $offset = 0;
  36. $info['gif']['header']['raw']['identifier'] = substr($GIFheader, $offset, 3);
  37. $offset += 3;
  38. $magic = 'GIF';
  39. if ($info['gif']['header']['raw']['identifier'] != $magic) {
  40. $this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($info['gif']['header']['raw']['identifier']).'"');
  41. unset($info['fileformat']);
  42. unset($info['gif']);
  43. return false;
  44. }
  45. //if (!$this->getid3->option_extra_info) {
  46. // $this->warning('GIF Extensions and Global Color Table not returned due to !getid3->option_extra_info');
  47. //}
  48. $info['gif']['header']['raw']['version'] = substr($GIFheader, $offset, 3);
  49. $offset += 3;
  50. $info['gif']['header']['raw']['width'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 2));
  51. $offset += 2;
  52. $info['gif']['header']['raw']['height'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 2));
  53. $offset += 2;
  54. $info['gif']['header']['raw']['flags'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 1));
  55. $offset += 1;
  56. $info['gif']['header']['raw']['bg_color_index'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 1));
  57. $offset += 1;
  58. $info['gif']['header']['raw']['aspect_ratio'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 1));
  59. $offset += 1;
  60. $info['video']['resolution_x'] = $info['gif']['header']['raw']['width'];
  61. $info['video']['resolution_y'] = $info['gif']['header']['raw']['height'];
  62. $info['gif']['version'] = $info['gif']['header']['raw']['version'];
  63. $info['gif']['header']['flags']['global_color_table'] = (bool) ($info['gif']['header']['raw']['flags'] & 0x80);
  64. if ($info['gif']['header']['raw']['flags'] & 0x80) {
  65. // Number of bits per primary color available to the original image, minus 1
  66. $info['gif']['header']['bits_per_pixel'] = 3 * ((($info['gif']['header']['raw']['flags'] & 0x70) >> 4) + 1);
  67. } else {
  68. $info['gif']['header']['bits_per_pixel'] = 0;
  69. }
  70. $info['gif']['header']['flags']['global_color_sorted'] = (bool) ($info['gif']['header']['raw']['flags'] & 0x40);
  71. if ($info['gif']['header']['flags']['global_color_table']) {
  72. // the number of bytes contained in the Global Color Table. To determine that
  73. // actual size of the color table, raise 2 to [the value of the field + 1]
  74. $info['gif']['header']['global_color_size'] = pow(2, ($info['gif']['header']['raw']['flags'] & 0x07) + 1);
  75. $info['video']['bits_per_sample'] = ($info['gif']['header']['raw']['flags'] & 0x07) + 1;
  76. } else {
  77. $info['gif']['header']['global_color_size'] = 0;
  78. }
  79. if ($info['gif']['header']['raw']['aspect_ratio'] != 0) {
  80. // Aspect Ratio = (Pixel Aspect Ratio + 15) / 64
  81. $info['gif']['header']['aspect_ratio'] = ($info['gif']['header']['raw']['aspect_ratio'] + 15) / 64;
  82. }
  83. if ($info['gif']['header']['flags']['global_color_table']) {
  84. $GIFcolorTable = $this->fread(3 * $info['gif']['header']['global_color_size']);
  85. if ($this->getid3->option_extra_info) {
  86. $offset = 0;
  87. for ($i = 0; $i < $info['gif']['header']['global_color_size']; $i++) {
  88. $red = getid3_lib::LittleEndian2Int(substr($GIFcolorTable, $offset++, 1));
  89. $green = getid3_lib::LittleEndian2Int(substr($GIFcolorTable, $offset++, 1));
  90. $blue = getid3_lib::LittleEndian2Int(substr($GIFcolorTable, $offset++, 1));
  91. $info['gif']['global_color_table'][$i] = (($red << 16) | ($green << 8) | ($blue));
  92. $info['gif']['global_color_table_rgb'][$i] = sprintf('%02X%02X%02X', $red, $green, $blue);
  93. }
  94. }
  95. }
  96. // Image Descriptor
  97. $info['gif']['animation']['animated'] = false;
  98. while (!feof($this->getid3->fp)) {
  99. $NextBlockTest = $this->fread(1);
  100. switch ($NextBlockTest) {
  101. /*
  102. case ',': // ',' - Image separator character
  103. $ImageDescriptorData = $NextBlockTest.$this->fread(9);
  104. $ImageDescriptor = array();
  105. $ImageDescriptor['image_left'] = getid3_lib::LittleEndian2Int(substr($ImageDescriptorData, 1, 2));
  106. $ImageDescriptor['image_top'] = getid3_lib::LittleEndian2Int(substr($ImageDescriptorData, 3, 2));
  107. $ImageDescriptor['image_width'] = getid3_lib::LittleEndian2Int(substr($ImageDescriptorData, 5, 2));
  108. $ImageDescriptor['image_height'] = getid3_lib::LittleEndian2Int(substr($ImageDescriptorData, 7, 2));
  109. $ImageDescriptor['flags_raw'] = getid3_lib::LittleEndian2Int(substr($ImageDescriptorData, 9, 1));
  110. $ImageDescriptor['flags']['use_local_color_map'] = (bool) ($ImageDescriptor['flags_raw'] & 0x80);
  111. $ImageDescriptor['flags']['image_interlaced'] = (bool) ($ImageDescriptor['flags_raw'] & 0x40);
  112. $info['gif']['image_descriptor'][] = $ImageDescriptor;
  113. if ($ImageDescriptor['flags']['use_local_color_map']) {
  114. $this->warning('This version of getID3() cannot parse local color maps for GIFs');
  115. return true;
  116. }
  117. $RasterData = array();
  118. $RasterData['code_size'] = getid3_lib::LittleEndian2Int($this->fread(1));
  119. $RasterData['block_byte_count'] = getid3_lib::LittleEndian2Int($this->fread(1));
  120. $info['gif']['raster_data'][count($info['gif']['image_descriptor']) - 1] = $RasterData;
  121. $CurrentCodeSize = $RasterData['code_size'] + 1;
  122. for ($i = 0; $i < pow(2, $RasterData['code_size']); $i++) {
  123. $DefaultDataLookupTable[$i] = chr($i);
  124. }
  125. $DefaultDataLookupTable[pow(2, $RasterData['code_size']) + 0] = ''; // Clear Code
  126. $DefaultDataLookupTable[pow(2, $RasterData['code_size']) + 1] = ''; // End Of Image Code
  127. $NextValue = $this->GetLSBits($CurrentCodeSize);
  128. echo 'Clear Code: '.$NextValue.'<BR>';
  129. $NextValue = $this->GetLSBits($CurrentCodeSize);
  130. echo 'First Color: '.$NextValue.'<BR>';
  131. $Prefix = $NextValue;
  132. $i = 0;
  133. while ($i++ < 20) {
  134. $NextValue = $this->GetLSBits($CurrentCodeSize);
  135. echo $NextValue.'<br>';
  136. }
  137. echo 'escaping<br>';
  138. return true;
  139. break;
  140. */
  141. case '!':
  142. // GIF Extension Block
  143. $ExtensionBlockData = $NextBlockTest.$this->fread(2);
  144. $ExtensionBlock = array();
  145. $ExtensionBlock['function_code'] = getid3_lib::LittleEndian2Int(substr($ExtensionBlockData, 1, 1));
  146. $ExtensionBlock['byte_length'] = getid3_lib::LittleEndian2Int(substr($ExtensionBlockData, 2, 1));
  147. $ExtensionBlock['data'] = (($ExtensionBlock['byte_length'] > 0) ? $this->fread($ExtensionBlock['byte_length']) : null);
  148. if (substr($ExtensionBlock['data'], 0, 11) == 'NETSCAPE2.0') { // Netscape Application Block (NAB)
  149. $ExtensionBlock['data'] .= $this->fread(4);
  150. if (substr($ExtensionBlock['data'], 11, 2) == "\x03\x01") {
  151. $info['gif']['animation']['animated'] = true;
  152. $info['gif']['animation']['loop_count'] = getid3_lib::LittleEndian2Int(substr($ExtensionBlock['data'], 13, 2));
  153. } else {
  154. $this->warning('Expecting 03 01 at offset '.($this->ftell() - 4).', found "'.getid3_lib::PrintHexBytes(substr($ExtensionBlock['data'], 11, 2)).'"');
  155. }
  156. }
  157. if ($this->getid3->option_extra_info) {
  158. $info['gif']['extension_blocks'][] = $ExtensionBlock;
  159. }
  160. break;
  161. case ';':
  162. $info['gif']['terminator_offset'] = $this->ftell() - 1;
  163. // GIF Terminator
  164. break;
  165. default:
  166. break;
  167. }
  168. }
  169. return true;
  170. }
  171. /**
  172. * @param int $bits
  173. *
  174. * @return float|int
  175. */
  176. public function GetLSBits($bits) {
  177. static $bitbuffer = '';
  178. while (strlen($bitbuffer) < $bits) {
  179. $bitbuffer = str_pad(decbin(ord($this->fread(1))), 8, '0', STR_PAD_LEFT).$bitbuffer;
  180. }
  181. $value = bindec(substr($bitbuffer, 0 - $bits));
  182. $bitbuffer = substr($bitbuffer, 0, 0 - $bits);
  183. return $value;
  184. }
  185. }