57 self::$gifFrameSep = pack(
"C", ord(
"," ) );
58 self::$gifExtensionSep = pack(
"C", ord(
"!" ) );
59 self::$gifTerm = pack(
"C", ord(
";" ) );
68 throw new Exception(
"No file name specified" );
69 } elseif ( !file_exists( $filename ) || is_dir( $filename ) ) {
70 throw new Exception(
"File $filename does not exist" );
73 $fh = fopen( $filename,
'rb' );
76 throw new Exception(
"Unable to open file $filename" );
80 $buf = fread( $fh, 6 );
81 if ( !( $buf ==
'GIF87a' || $buf ==
'GIF89a' ) ) {
82 throw new Exception(
"Not a valid GIF file; header: $buf" );
89 $buf = fread( $fh, 1 );
98 while ( !feof( $fh ) ) {
99 $buf = fread( $fh, 1 );
101 if ( $buf == self::$gifFrameSep ) {
109 $buf = fread( $fh, 1 );
116 } elseif ( $buf == self::$gifExtensionSep ) {
117 $buf = fread( $fh, 1 );
118 if ( strlen( $buf ) < 1 ) {
119 throw new Exception(
"Ran out of input" );
121 $extension_code = unpack(
'C', $buf );
122 $extension_code = $extension_code[1];
124 if ( $extension_code == 0xF9 ) {
130 $buf = fread( $fh, 2 );
131 if ( strlen( $buf ) < 2 ) {
132 throw new Exception(
"Ran out of input" );
134 $delay = unpack(
'v', $buf );
136 $duration += $delay * 0.01;
140 $term = fread( $fh, 1 );
141 if ( strlen(
$term ) < 1 ) {
142 throw new Exception(
"Ran out of input" );
147 throw new Exception(
"Malformed Graphics Control Extension block" );
149 } elseif ( $extension_code == 0xFE ) {
152 if ( $data ===
"" ) {
153 throw new Exception(
'Read error, zero-length comment block' );
163 if ( $dataCopy !== $data ) {
165 $data = iconv(
'windows-1252',
'UTF-8', $data );
170 if ( $commentCount === 0
171 ||
$comment[$commentCount - 1] !== $data
178 } elseif ( $extension_code == 0xFF ) {
181 $blockLength = fread( $fh, 1 );
182 if ( strlen( $blockLength ) < 1 ) {
183 throw new Exception(
"Ran out of input" );
185 $blockLength = unpack(
'C', $blockLength );
186 $blockLength = $blockLength[1];
187 $data = fread( $fh, $blockLength );
189 if ( $blockLength != 11 ) {
190 wfDebug( __METHOD__ .
" GIF application block with wrong length\n" );
191 fseek( $fh, -( $blockLength + 1 ), SEEK_CUR );
197 if ( $data ==
'NETSCAPE2.0' ) {
198 $data = fread( $fh, 2 );
200 if ( $data !=
"\x03\x01" ) {
201 throw new Exception(
"Expected \x03\x01, got $data" );
205 $loopData = fread( $fh, 2 );
206 if ( strlen( $loopData ) < 2 ) {
207 throw new Exception(
"Ran out of input" );
209 $loopData = unpack(
'v', $loopData );
210 $loopCount = $loopData[1];
212 if ( $loopCount != 1 ) {
218 } elseif ( $data ==
'XMP DataXMP' ) {
224 if ( substr( $xmp, -257, 3 ) !==
"\x01\xFF\xFE"
225 || substr( $xmp, -4 ) !==
"\x03\x02\x01\x00"
228 throw new Exception(
"XMP does not have magic trailer!" );
232 $xmp = substr( $xmp, 0, -257 );
235 fseek( $fh, -( $blockLength + 1 ), SEEK_CUR );
242 } elseif ( $buf == self::$gifTerm ) {
245 if ( strlen( $buf ) < 1 ) {
246 throw new Exception(
"Ran out of input" );
248 $byte = unpack(
'C', $buf );
250 throw new Exception(
"At position: " . ftell( $fh ) .
", Unknown byte " . $byte );
255 'frameCount' => $frameCount,
256 'looped' => $isLooped,
257 'duration' => $duration,
270 $max = pow( 2, $bpp );
271 for ( $i = 1; $i <= $max; ++$i ) {
283 if ( strlen( $data ) < 1 ) {
284 throw new Exception(
"Ran out of input" );
286 $buf = unpack(
'C', $data );
288 $bpp = ( $buf & 7 ) + 1;
291 $have_map = $buf & 1;
293 return $have_map ? $bpp : 0;
301 while ( !feof( $fh ) ) {
302 $buf = fread( $fh, 1 );
303 if ( strlen( $buf ) < 1 ) {
304 throw new Exception(
"Ran out of input" );
306 $block_len = unpack(
'C', $buf );
307 $block_len = $block_len[1];
308 if ( $block_len == 0 ) {
311 fread( $fh, $block_len );
329 static function readBlock( $fh, $includeLengths =
false ) {
331 $subLength = fread( $fh, 1 );
334 while ( $subLength !==
"\0" ) {
336 if ( $blocks > self::MAX_SUBBLOCKS ) {
337 throw new Exception(
"MAX_SUBBLOCKS exceeded (over $blocks sub-blocks)" );
340 throw new Exception(
"Read error: Unexpected EOF." );
342 if ( $includeLengths ) {
346 $data .= fread( $fh, ord( $subLength ) );
347 $subLength = fread( $fh, 1 );