PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/common/libraries/plugin/getid3/module.graphic.gif.php

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