module.graphic.gif.php 8.1 KB

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