module.graphic.gif.php 8.1 KB

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