59 self::$gifFrameSep = pack(
"C", ord(
"," ) );
60 self::$gifExtensionSep = pack(
"C", ord(
"!" ) );
61 self::$gifTerm = pack(
"C", ord(
";" ) );
70 throw new Exception(
"No file name specified" );
71 } elseif ( !file_exists( $filename ) || is_dir( $filename ) ) {
72 throw new Exception(
"File $filename does not exist" );
75 $fh = fopen( $filename,
'rb' );
78 throw new Exception(
"Unable to open file $filename" );
82 $buf = fread( $fh, 6 );
83 if ( !( $buf ==
'GIF87a' || $buf ==
'GIF89a' ) ) {
84 throw new Exception(
"Not a valid GIF file; header: $buf" );
88 $buf = fread( $fh, 2 );
89 $width = unpack(
'v', $buf )[1];
90 $buf = fread( $fh, 2 );
91 $height = unpack(
'v', $buf )[1];
94 $buf = fread( $fh, 1 );
95 list( $bpp, $have_map ) = self::decodeBPP( $buf );
103 self::readGCT( $fh, $bpp );
106 while ( !feof( $fh ) ) {
107 $buf = fread( $fh, 1 );
109 if ( $buf == self::$gifFrameSep ) {
113 # # Skip bounding box
118 $buf = fread( $fh, 1 );
119 list( $bpp, $have_map ) = self::decodeBPP( $buf );
123 self::readGCT( $fh, $bpp );
127 self::skipBlock( $fh );
128 } elseif ( $buf == self::$gifExtensionSep ) {
129 $buf = fread( $fh, 1 );
130 if ( strlen( $buf ) < 1 ) {
131 throw new Exception(
"Ran out of input" );
133 $extension_code = unpack(
'C', $buf )[1];
135 if ( $extension_code == 0xF9 ) {
144 $buf = fread( $fh, 2 );
145 if ( strlen( $buf ) < 2 ) {
146 throw new Exception(
"Ran out of input" );
148 $delay = unpack(
'v', $buf )[1];
149 $duration += $delay * 0.01;
154 $term = fread( $fh, 1 );
155 if ( strlen( $term ) < 1 ) {
156 throw new Exception(
"Ran out of input" );
158 $term = unpack(
'C', $term )[1];
160 throw new Exception(
"Malformed Graphics Control Extension block" );
162 } elseif ( $extension_code == 0xFE ) {
164 $data = self::readBlock( $fh );
165 if ( $data ===
"" ) {
166 throw new Exception(
'Read error, zero-length comment block' );
174 UtfNormal\Validator::quickIsNFCVerify( $dataCopy );
176 if ( $dataCopy !== $data ) {
177 AtEase::suppressWarnings();
178 $data = iconv(
'windows-1252',
'UTF-8', $data );
179 AtEase::restoreWarnings();
182 $commentCount = count( $comment );
183 if ( $commentCount === 0
185 || $comment[$commentCount - 1] !== $data
192 } elseif ( $extension_code == 0xFF ) {
195 $blockLength = fread( $fh, 1 );
196 if ( strlen( $blockLength ) < 1 ) {
197 throw new Exception(
"Ran out of input" );
199 $blockLength = unpack(
'C', $blockLength )[1];
200 $data = fread( $fh, $blockLength );
202 if ( $blockLength != 11 ) {
203 wfDebug( __METHOD__ .
" GIF application block with wrong length" );
204 fseek( $fh, -( $blockLength + 1 ), SEEK_CUR );
205 self::skipBlock( $fh );
210 if ( $data ==
'NETSCAPE2.0' ) {
211 $data = fread( $fh, 2 );
213 if ( $data !=
"\x03\x01" ) {
214 throw new Exception(
"Expected \x03\x01, got $data" );
218 $loopData = fread( $fh, 2 );
219 if ( strlen( $loopData ) < 2 ) {
220 throw new Exception(
"Ran out of input" );
222 $loopCount = unpack(
'v', $loopData )[1];
224 if ( $loopCount != 1 ) {
231 } elseif ( $data ==
'XMP DataXMP' ) {
235 $xmp = self::readBlock( $fh,
true );
237 if ( substr( $xmp, -257, 3 ) !==
"\x01\xFF\xFE"
238 || substr( $xmp, -4 ) !==
"\x03\x02\x01\x00"
240 throw new Exception(
"XMP does not have magic trailer!" );
244 $xmp = substr( $xmp, 0, -257 );
247 fseek( $fh, -( $blockLength + 1 ), SEEK_CUR );
248 self::skipBlock( $fh );
251 self::skipBlock( $fh );
253 } elseif ( $buf == self::$gifTerm ) {
256 if ( strlen( $buf ) < 1 ) {
257 throw new Exception(
"Ran out of input" );
259 $byte = unpack(
'C', $buf )[1];
260 throw new Exception(
"At position: " . ftell( $fh ) .
", Unknown byte " . $byte );
265 'frameCount' => $frameCount,
266 'looped' => $isLooped,
267 'duration' => $duration,
269 'comment' => $comment,