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" );
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;
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 ) {
165 $data = iconv(
'windows-1252',
'UTF-8',
$data );
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];
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' ) {
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 ) {
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 );
256 'comment' => $comment,