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" );
86 $buf = fread( $fh, 2 );
87 $width = unpack(
'v', $buf )[1];
88 $buf = fread( $fh, 2 );
89 $height = unpack(
'v', $buf )[1];
92 $buf = fread( $fh, 1 );
104 while ( !feof( $fh ) ) {
105 $buf = fread( $fh, 1 );
107 if ( $buf == self::$gifFrameSep ) {
111 # # Skip bounding box
116 $buf = fread( $fh, 1 );
126 } elseif ( $buf == self::$gifExtensionSep ) {
127 $buf = fread( $fh, 1 );
128 if ( strlen( $buf ) < 1 ) {
129 throw new Exception(
"Ran out of input" );
131 $extension_code = unpack(
'C', $buf )[1];
133 if ( $extension_code == 0xF9 ) {
142 $buf = fread( $fh, 2 );
143 if ( strlen( $buf ) < 2 ) {
144 throw new Exception(
"Ran out of input" );
146 $delay = unpack(
'v', $buf )[1];
147 $duration += $delay * 0.01;
152 $term = fread( $fh, 1 );
153 if ( strlen( $term ) < 1 ) {
154 throw new Exception(
"Ran out of input" );
156 $term = unpack(
'C', $term )[1];
158 throw new Exception(
"Malformed Graphics Control Extension block" );
160 } elseif ( $extension_code == 0xFE ) {
163 if ( $data ===
"" ) {
164 throw new Exception(
'Read error, zero-length comment block' );
172 UtfNormal\Validator::quickIsNFCVerify( $dataCopy );
174 if ( $dataCopy !== $data ) {
175 Wikimedia\suppressWarnings();
176 $data = iconv(
'windows-1252',
'UTF-8', $data );
177 Wikimedia\restoreWarnings();
180 $commentCount = count( $comment );
181 if ( $commentCount === 0
183 || $comment[$commentCount - 1] !== $data
190 } elseif ( $extension_code == 0xFF ) {
193 $blockLength = fread( $fh, 1 );
194 if ( strlen( $blockLength ) < 1 ) {
195 throw new Exception(
"Ran out of input" );
197 $blockLength = unpack(
'C', $blockLength )[1];
198 $data = fread( $fh, $blockLength );
200 if ( $blockLength != 11 ) {
201 wfDebug( __METHOD__ .
" GIF application block with wrong length" );
202 fseek( $fh, -( $blockLength + 1 ), SEEK_CUR );
208 if ( $data ==
'NETSCAPE2.0' ) {
209 $data = fread( $fh, 2 );
211 if ( $data !=
"\x03\x01" ) {
212 throw new Exception(
"Expected \x03\x01, got $data" );
216 $loopData = fread( $fh, 2 );
217 if ( strlen( $loopData ) < 2 ) {
218 throw new Exception(
"Ran out of input" );
220 $loopCount = unpack(
'v', $loopData )[1];
222 if ( $loopCount != 1 ) {
229 } elseif ( $data ==
'XMP DataXMP' ) {
235 if ( substr( $xmp, -257, 3 ) !==
"\x01\xFF\xFE"
236 || substr( $xmp, -4 ) !==
"\x03\x02\x01\x00"
239 throw new Exception(
"XMP does not have magic trailer!" );
243 $xmp = substr( $xmp, 0, -257 );
246 fseek( $fh, -( $blockLength + 1 ), SEEK_CUR );
252 } elseif ( $buf == self::$gifTerm ) {
255 if ( strlen( $buf ) < 1 ) {
256 throw new Exception(
"Ran out of input" );
258 $byte = unpack(
'C', $buf )[1];
259 throw new Exception(
"At position: " . ftell( $fh ) .
", Unknown byte " . $byte );
264 'frameCount' => $frameCount,
265 'looped' => $isLooped,
266 'duration' => $duration,
268 'comment' => $comment,
339 private static function readBlock( $fh, $includeLengths =
false ) {
341 $subLength = fread( $fh, 1 );
344 while ( $subLength !==
"\0" ) {
346 if ( $blocks > self::MAX_SUBBLOCKS ) {
347 throw new Exception(
"MAX_SUBBLOCKS exceeded (over $blocks sub-blocks)" );
350 throw new Exception(
"Read error: Unexpected EOF." );
352 if ( $includeLengths ) {
356 $data .= fread( $fh, ord( $subLength ) );
357 $subLength = fread( $fh, 1 );