47 self::$pngSig = pack(
"C8", 137, 80, 78, 71, 13, 10, 26, 10 );
53 'xml:com.adobe.xmp' =>
'xmp',
54 # Artist is unofficial. Author is the recommended
55 # keyword in the PNG spec. However some people output
56 # Artist so support both.
61 'comment' =>
'PNGFileComment',
62 'description' =>
'ImageDescription',
63 'title' =>
'ObjectName',
64 'copyright' =>
'Copyright',
65 # Source as in original device used to make image
66 # not as in who gave you the image
68 'software' =>
'Software',
69 'disclaimer' =>
'Disclaimer',
70 'warning' =>
'ContentWarning',
71 'url' =>
'Identifier', # Not sure
if this is best mapping. Maybe WebStatement.
73 'creation time' =>
'DateTimeDigitized',
82 $colorType =
'unknown';
85 throw new Exception( __METHOD__ .
": No file name specified" );
86 } elseif ( !file_exists( $filename ) || is_dir( $filename ) ) {
87 throw new Exception( __METHOD__ .
": File $filename does not exist" );
90 $fh = fopen( $filename,
'rb' );
93 throw new Exception( __METHOD__ .
": Unable to open file $filename" );
97 $buf = fread( $fh, 8 );
98 if ( $buf != self::$pngSig ) {
99 throw new Exception( __METHOD__ .
": Not a valid PNG file; header: $buf" );
103 while ( !feof( $fh ) ) {
104 $buf = fread( $fh, 4 );
105 if ( !$buf || strlen( $buf ) < 4 ) {
106 throw new Exception( __METHOD__ .
": Read error" );
108 $chunk_size = unpack(
"N", $buf )[1];
110 if ( $chunk_size < 0 ) {
111 throw new Exception( __METHOD__ .
": Chunk size too big for unpack" );
114 $chunk_type = fread( $fh, 4 );
115 if ( !$chunk_type || strlen( $chunk_type ) < 4 ) {
116 throw new Exception( __METHOD__ .
": Read error" );
119 if ( $chunk_type ==
"IHDR" ) {
121 if ( !$buf || strlen( $buf ) < $chunk_size ) {
122 throw new Exception( __METHOD__ .
": Read error" );
124 $bitDepth = ord( substr( $buf, 8, 1 ) );
127 switch ( ord( substr( $buf, 9, 1 ) ) ) {
129 $colorType =
'greyscale';
132 $colorType =
'truecolour';
135 $colorType =
'index-coloured';
138 $colorType =
'greyscale-alpha';
141 $colorType =
'truecolour-alpha';
144 $colorType =
'unknown';
147 } elseif ( $chunk_type ==
"acTL" ) {
148 $buf = fread( $fh, $chunk_size );
149 if ( !$buf || strlen( $buf ) < $chunk_size || $chunk_size < 4 ) {
150 throw new Exception( __METHOD__ .
": Read error" );
153 $actl = unpack(
"Nframes/Nplays", $buf );
154 $frameCount = $actl[
'frames'];
155 $loopCount = $actl[
'plays'];
156 } elseif ( $chunk_type ==
"fcTL" ) {
158 if ( !$buf || strlen( $buf ) < $chunk_size ) {
159 throw new Exception( __METHOD__ .
": Read error" );
161 $buf = substr( $buf, 20 );
162 if ( strlen( $buf ) < 4 ) {
163 throw new Exception( __METHOD__ .
": Read error" );
166 $fctldur = unpack(
"ndelay_num/ndelay_den", $buf );
167 if ( $fctldur[
'delay_den'] == 0 ) {
168 $fctldur[
'delay_den'] = 100;
170 if ( $fctldur[
'delay_num'] ) {
171 $duration += $fctldur[
'delay_num'] / $fctldur[
'delay_den'];
173 } elseif ( $chunk_type ==
"iTXt" ) {
178 '/^([^\x00]{1,79})\x00(\x00|\x01)\x00([^\x00]*)(.)[^\x00]*\x00(.*)$/Ds',
187 $items[1] = strtolower( $items[1] );
188 if ( !isset( self::$textChunks[$items[1]] ) ) {
190 fseek( $fh, self::$crcSize, SEEK_CUR );
194 $items[3] = strtolower( $items[3] );
195 if ( $items[3] ==
'' ) {
197 $items[3] =
'x-default';
201 if ( $items[2] ==
"\x01" ) {
202 if ( function_exists(
'gzuncompress' ) && $items[4] ===
"\x00" ) {
203 MediaWiki\suppressWarnings();
204 $items[5] = gzuncompress( $items[5] );
205 MediaWiki\restoreWarnings();
207 if ( $items[5] ===
false ) {
209 wfDebug( __METHOD__ .
' Error decompressing iTxt chunk - ' . $items[1] .
"\n" );
210 fseek( $fh, self::$crcSize, SEEK_CUR );
214 wfDebug( __METHOD__ .
' Skipping compressed png iTXt chunk due to lack of zlib,'
215 .
" or potentially invalid compression method\n" );
216 fseek( $fh, self::$crcSize, SEEK_CUR );
220 $finalKeyword = self::$textChunks[$items[1]];
221 $text[$finalKeyword][$items[3]] = $items[5];
222 $text[$finalKeyword][
'_type'] =
'lang';
225 throw new Exception( __METHOD__ .
": Read error on iTXt chunk" );
227 } elseif ( $chunk_type ==
'tEXt' ) {
231 if ( strpos( $buf,
"\x00" ) ===
false ) {
232 throw new Exception( __METHOD__ .
": Read error on tEXt chunk" );
235 list( $keyword,
$content ) = explode(
"\x00", $buf, 2 );
236 if ( $keyword ===
'' ||
$content ===
'' ) {
237 throw new Exception( __METHOD__ .
": Read error on tEXt chunk" );
241 $keyword = strtolower( $keyword );
242 if ( !isset( self::$textChunks[$keyword] ) ) {
244 fseek( $fh, self::$crcSize, SEEK_CUR );
247 MediaWiki\suppressWarnings();
249 MediaWiki\restoreWarnings();
252 throw new Exception( __METHOD__ .
": Read error (error with iconv)" );
255 $finalKeyword = self::$textChunks[$keyword];
256 $text[$finalKeyword][
'x-default'] =
$content;
257 $text[$finalKeyword][
'_type'] =
'lang';
258 } elseif ( $chunk_type ==
'zTXt' ) {
259 if ( function_exists(
'gzuncompress' ) ) {
263 if ( strpos( $buf,
"\x00" ) ===
false ) {
264 throw new Exception( __METHOD__ .
": Read error on zTXt chunk" );
267 list( $keyword, $postKeyword ) = explode(
"\x00", $buf, 2 );
268 if ( $keyword ===
'' || $postKeyword ===
'' ) {
269 throw new Exception( __METHOD__ .
": Read error on zTXt chunk" );
272 $keyword = strtolower( $keyword );
274 if ( !isset( self::$textChunks[$keyword] ) ) {
276 fseek( $fh, self::$crcSize, SEEK_CUR );
279 $compression = substr( $postKeyword, 0, 1 );
280 $content = substr( $postKeyword, 1 );
281 if ( $compression !==
"\x00" ) {
282 wfDebug( __METHOD__ .
" Unrecognized compression method in zTXt ($keyword). Skipping.\n" );
283 fseek( $fh, self::$crcSize, SEEK_CUR );
287 MediaWiki\suppressWarnings();
289 MediaWiki\restoreWarnings();
293 wfDebug( __METHOD__ .
' Error decompressing zTXt chunk - ' . $keyword .
"\n" );
294 fseek( $fh, self::$crcSize, SEEK_CUR );
298 MediaWiki\suppressWarnings();
300 MediaWiki\restoreWarnings();
303 throw new Exception( __METHOD__ .
": Read error (error with iconv)" );
306 $finalKeyword = self::$textChunks[$keyword];
307 $text[$finalKeyword][
'x-default'] =
$content;
308 $text[$finalKeyword][
'_type'] =
'lang';
310 wfDebug( __METHOD__ .
" Cannot decompress zTXt chunk due to lack of zlib. Skipping.\n" );
311 fseek( $fh, $chunk_size, SEEK_CUR );
313 } elseif ( $chunk_type ==
'tIME' ) {
315 if ( $chunk_size !== 7 ) {
316 throw new Exception( __METHOD__ .
": tIME wrong size" );
319 if ( !$buf || strlen( $buf ) < $chunk_size ) {
320 throw new Exception( __METHOD__ .
": Read error" );
324 $t = unpack(
"ny/Cm/Cd/Ch/Cmin/Cs", $buf );
325 $strTime = sprintf(
"%04d%02d%02d%02d%02d%02d",
327 $t[
'min'],
$t[
's'] );
332 $text[
'DateTime'] = $exifTime;
334 } elseif ( $chunk_type ==
'pHYs' ) {
336 if ( $chunk_size !== 9 ) {
337 throw new Exception( __METHOD__ .
": pHYs wrong size" );
341 if ( !$buf || strlen( $buf ) < $chunk_size ) {
342 throw new Exception( __METHOD__ .
": Read error" );
345 $dim = unpack(
"Nwidth/Nheight/Cunit", $buf );
346 if ( $dim[
'unit'] == 1 ) {
349 if ( $dim[
'width'] > 0 && $dim[
'height'] > 0 ) {
352 $text[
'XResolution'] = $dim[
'width']
354 $text[
'YResolution'] = $dim[
'height']
356 $text[
'ResolutionUnit'] = 3;
360 } elseif ( $chunk_type ==
"IEND" ) {
363 fseek( $fh, $chunk_size, SEEK_CUR );
365 fseek( $fh, self::$crcSize, SEEK_CUR );
369 if ( $loopCount > 1 ) {
370 $duration *= $loopCount;
373 if ( isset( $text[
'DateTimeDigitized'] ) ) {
376 if (
$name ===
'_type' ) {
401 'frameCount' => $frameCount,
402 'loopCount' => $loopCount,
403 'duration' => $duration,
405 'bitDepth' => $bitDepth,
406 'colorType' => $colorType,
418 private static function read( $fh, $size ) {
419 if ( $size > self::MAX_CHUNK_SIZE ) {
420 throw new Exception( __METHOD__ .
': Chunk size of ' . $size .
421 ' too big. Max size is: ' . self::MAX_CHUNK_SIZE );
424 return fread( $fh, $size );