ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
module.graphic.gif.php
Go to the documentation of this file.
1 <?php
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
7 // See readme.txt for more details //
9 // //
10 // module.graphic.gif.php //
11 // module for analyzing GIF Image files //
12 // dependencies: NONE //
13 // ///
15 
16 
18 {
19 
20  function getid3_gif(&$fd, &$ThisFileInfo) {
21  $ThisFileInfo['fileformat'] = 'gif';
22  $ThisFileInfo['video']['dataformat'] = 'gif';
23  $ThisFileInfo['video']['lossless'] = true;
24  $ThisFileInfo['video']['pixel_aspect_ratio'] = (float) 1;
25 
26  fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
27  $GIFheader = fread($fd, 13);
28  $offset = 0;
29 
30  $ThisFileInfo['gif']['header']['raw']['identifier'] = substr($GIFheader, $offset, 3);
31  $offset += 3;
32 
33  if ($ThisFileInfo['gif']['header']['raw']['identifier'] != 'GIF') {
34  $ThisFileInfo['error'][] = 'Expecting "GIF" at offset '.$ThisFileInfo['avdataoffset'].', found "'.$ThisFileInfo['gif']['header']['raw']['identifier'].'"';
35  unset($ThisFileInfo['fileformat']);
36  unset($ThisFileInfo['gif']);
37  return false;
38  }
39 
40  $ThisFileInfo['gif']['header']['raw']['version'] = substr($GIFheader, $offset, 3);
41  $offset += 3;
42  $ThisFileInfo['gif']['header']['raw']['width'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 2));
43  $offset += 2;
44  $ThisFileInfo['gif']['header']['raw']['height'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 2));
45  $offset += 2;
46  $ThisFileInfo['gif']['header']['raw']['flags'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 1));
47  $offset += 1;
48  $ThisFileInfo['gif']['header']['raw']['bg_color_index'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 1));
49  $offset += 1;
50  $ThisFileInfo['gif']['header']['raw']['aspect_ratio'] = getid3_lib::LittleEndian2Int(substr($GIFheader, $offset, 1));
51  $offset += 1;
52 
53  $ThisFileInfo['video']['resolution_x'] = $ThisFileInfo['gif']['header']['raw']['width'];
54  $ThisFileInfo['video']['resolution_y'] = $ThisFileInfo['gif']['header']['raw']['height'];
55  $ThisFileInfo['gif']['version'] = $ThisFileInfo['gif']['header']['raw']['version'];
56  $ThisFileInfo['gif']['header']['flags']['global_color_table'] = (bool) ($ThisFileInfo['gif']['header']['raw']['flags'] & 0x80);
57  if ($ThisFileInfo['gif']['header']['raw']['flags'] & 0x80) {
58  // Number of bits per primary color available to the original image, minus 1
59  $ThisFileInfo['gif']['header']['bits_per_pixel'] = 3 * ((($ThisFileInfo['gif']['header']['raw']['flags'] & 0x70) >> 4) + 1);
60  } else {
61  $ThisFileInfo['gif']['header']['bits_per_pixel'] = 0;
62  }
63  $ThisFileInfo['gif']['header']['flags']['global_color_sorted'] = (bool) ($ThisFileInfo['gif']['header']['raw']['flags'] & 0x40);
64  if ($ThisFileInfo['gif']['header']['flags']['global_color_table']) {
65  // the number of bytes contained in the Global Color Table. To determine that
66  // actual size of the color table, raise 2 to [the value of the field + 1]
67  $ThisFileInfo['gif']['header']['global_color_size'] = pow(2, ($ThisFileInfo['gif']['header']['raw']['flags'] & 0x07) + 1);
68  $ThisFileInfo['video']['bits_per_sample'] = ($ThisFileInfo['gif']['header']['raw']['flags'] & 0x07) + 1;
69  } else {
70  $ThisFileInfo['gif']['header']['global_color_size'] = 0;
71  }
72  if ($ThisFileInfo['gif']['header']['raw']['aspect_ratio'] != 0) {
73  // Aspect Ratio = (Pixel Aspect Ratio + 15) / 64
74  $ThisFileInfo['gif']['header']['aspect_ratio'] = ($ThisFileInfo['gif']['header']['raw']['aspect_ratio'] + 15) / 64;
75  }
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 
163  return true;
164  }
165 
166 
167  function GetLSBits($fd, $bits) {
168  static $bitbuffer = '';
169  while (strlen($bitbuffer) < $bits) {
170 //echo 'Read another byte: '.ftell($fd).'<BR>';
171  $bitbuffer = str_pad(decbin(ord(fread($fd, 1))), 8, '0', STR_PAD_LEFT).$bitbuffer;
172  }
173 
174  $value = bindec(substr($bitbuffer, 0 - $bits));
175  $bitbuffer = substr($bitbuffer, 0, 0 - $bits);
176 
177  return $value;
178  }
179 
180 }
181 
182 
183 ?>