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 ) {
133 $buf = fread( $fh, 2 );
134 if ( strlen( $buf ) < 2 ) {
135 throw new Exception(
"Ran out of input" );
137 $delay = unpack(
'v', $buf )[1];
138 $duration += $delay * 0.01;
142 $term = fread( $fh, 1 );
143 if ( strlen( $term ) < 1 ) {
144 throw new Exception(
"Ran out of input" );
146 $term = unpack(
'C', $term )[1];
148 throw new Exception(
"Malformed Graphics Control Extension block" );
150 } elseif ( $extension_code == 0xFE ) {
153 if ( $data ===
"" ) {
154 throw new Exception(
'Read error, zero-length comment block' );
162 UtfNormal\Validator::quickIsNFCVerify( $dataCopy );
164 if ( $dataCopy !== $data ) {
165 Wikimedia\suppressWarnings();
166 $data = iconv(
'windows-1252',
'UTF-8', $data );
167 Wikimedia\restoreWarnings();
170 $commentCount = count( $comment );
171 if ( $commentCount === 0
173 || $comment[$commentCount - 1] !== $data
180 } elseif ( $extension_code == 0xFF ) {
183 $blockLength = fread( $fh, 1 );
184 if ( strlen( $blockLength ) < 1 ) {
185 throw new Exception(
"Ran out of input" );
187 $blockLength = unpack(
'C', $blockLength )[1];
188 $data = fread( $fh, $blockLength );
190 if ( $blockLength != 11 ) {
191 wfDebug( __METHOD__ .
" GIF application block with wrong length" );
192 fseek( $fh, -( $blockLength + 1 ), SEEK_CUR );
198 if ( $data ==
'NETSCAPE2.0' ) {
199 $data = fread( $fh, 2 );
201 if ( $data !=
"\x03\x01" ) {
202 throw new Exception(
"Expected \x03\x01, got $data" );
206 $loopData = fread( $fh, 2 );
207 if ( strlen( $loopData ) < 2 ) {
208 throw new Exception(
"Ran out of input" );
210 $loopCount = unpack(
'v', $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 )[1];
249 throw new Exception(
"At position: " . ftell( $fh ) .
", Unknown byte " . $byte );
254 'frameCount' => $frameCount,
255 'looped' => $isLooped,
256 'duration' => $duration,
258 'comment' => $comment,
326 private static function readBlock( $fh, $includeLengths =
false ) {
328 $subLength = fread( $fh, 1 );
331 while ( $subLength !==
"\0" ) {
333 if ( $blocks > self::MAX_SUBBLOCKS ) {
334 throw new Exception(
"MAX_SUBBLOCKS exceeded (over $blocks sub-blocks)" );
337 throw new Exception(
"Read error: Unexpected EOF." );
339 if ( $includeLengths ) {
343 $data .= fread( $fh, ord( $subLength ) );
344 $subLength = fread( $fh, 1 );