module.graphic.gif.php 8.3 KB

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