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 );
101 while ( !feof( $fh ) ) {
102 $buf = fread( $fh, 1 );
104 if ( $buf == self::$gifFrameSep ) {
108 # # Skip bounding box
112 $buf = fread( $fh, 1 );
119 } elseif ( $buf == self::$gifExtensionSep ) {
120 $buf = fread( $fh, 1 );
121 if ( strlen( $buf ) < 1 ) {
122 throw new Exception(
"Ran out of input" );
124 $extension_code = unpack(
'C', $buf )[1];
126 if ( $extension_code == 0xF9 ) {
132 $buf = fread( $fh, 2 );
133 if ( strlen( $buf ) < 2 ) {
134 throw new Exception(
"Ran out of input" );
136 $delay = unpack(
'v', $buf )[1];
137 $duration += $delay * 0.01;
141 $term = fread( $fh, 1 );
142 if ( strlen(
$term ) < 1 ) {
143 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' );
161 UtfNormal\Validator::quickIsNFCVerify( $dataCopy );
163 if ( $dataCopy !== $data ) {
164 MediaWiki\suppressWarnings();
165 $data = iconv(
'windows-1252',
'UTF-8', $data );
166 MediaWiki\restoreWarnings();
169 $commentCount = count( $comment );
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 )[1];
186 $data = fread( $fh, $blockLength );
188 if ( $blockLength != 11 ) {
189 wfDebug( __METHOD__ .
" GIF application block with wrong length\n" );
190 fseek( $fh, -( $blockLength + 1 ), SEEK_CUR );
196 if ( $data ==
'NETSCAPE2.0' ) {
197 $data = fread( $fh, 2 );
199 if ( $data !=
"\x03\x01" ) {
200 throw new Exception(
"Expected \x03\x01, got $data" );
204 $loopData = fread( $fh, 2 );
205 if ( strlen( $loopData ) < 2 ) {
206 throw new Exception(
"Ran out of input" );
208 $loopCount = unpack(
'v', $loopData )[1];
210 if ( $loopCount != 1 ) {
216 } elseif ( $data ==
'XMP DataXMP' ) {
222 if ( substr( $xmp, -257, 3 ) !==
"\x01\xFF\xFE"
223 || substr( $xmp, -4 ) !==
"\x03\x02\x01\x00"
226 throw new Exception(
"XMP does not have magic trailer!" );
230 $xmp = substr( $xmp, 0, -257 );
233 fseek( $fh, -( $blockLength + 1 ), SEEK_CUR );
240 } elseif ( $buf == self::$gifTerm ) {
243 if ( strlen( $buf ) < 1 ) {
244 throw new Exception(
"Ran out of input" );
246 $byte = unpack(
'C', $buf )[1];
247 throw new Exception(
"At position: " . ftell( $fh ) .
", Unknown byte " . $byte );
252 'frameCount' => $frameCount,
253 'looped' => $isLooped,
254 'duration' => $duration,
256 'comment' => $comment,
324 static function readBlock( $fh, $includeLengths =
false ) {
326 $subLength = fread( $fh, 1 );
329 while ( $subLength !==
"\0" ) {
331 if ( $blocks > self::MAX_SUBBLOCKS ) {
332 throw new Exception(
"MAX_SUBBLOCKS exceeded (over $blocks sub-blocks)" );
335 throw new Exception(
"Read error: Unexpected EOF." );
337 if ( $includeLengths ) {
341 $data .= fread( $fh, ord( $subLength ) );
342 $subLength = fread( $fh, 1 );