module.graphic.gif.php 9.8 KB

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