44 'xml:com.adobe.xmp' =>
'xmp',
45 # Artist is unofficial. Author is the recommended
46 # keyword in the PNG spec. However some people output
47 # Artist so support both.
52 'comment' =>
'PNGFileComment',
53 'description' =>
'ImageDescription',
54 'title' =>
'ObjectName',
55 'copyright' =>
'Copyright',
56 # Source as in original device used to make image
57 # not as in who gave you the image
59 'software' =>
'Software',
60 'disclaimer' =>
'Disclaimer',
61 'warning' =>
'ContentWarning',
62 'url' =>
'Identifier', # Not sure
if this is best mapping. Maybe WebStatement.
64 'creation time' =>
'DateTimeDigitized',
75 $colorType =
'unknown';
79 throw new InvalidArgumentException( __METHOD__ .
": No file name specified" );
82 if ( !file_exists( $filename ) || is_dir( $filename ) ) {
83 throw new InvalidArgumentException( __METHOD__ .
": File $filename does not exist" );
86 $fh = fopen( $filename,
'rb' );
89 throw new InvalidArgumentException( __METHOD__ .
": Unable to open file $filename" );
93 $buf = self::read( $fh, 8 );
94 if ( $buf !==
"\x89PNG\x0d\x0a\x1a\x0a" ) {
95 throw new InvalidArgumentException( __METHOD__ .
": Not a valid PNG file; header: $buf" );
99 while ( !feof( $fh ) ) {
100 $buf = self::read( $fh, 4 );
101 $chunk_size = unpack(
"N", $buf )[1];
103 if ( $chunk_size < 0 || $chunk_size > self::MAX_CHUNK_SIZE ) {
104 wfDebug( __METHOD__ .
': Chunk size of ' . $chunk_size .
105 ' too big, skipping. Max size is: ' . self::MAX_CHUNK_SIZE );
106 if ( fseek( $fh, 4 + $chunk_size + self::$crcSize, SEEK_CUR ) !== 0 ) {
107 throw new InvalidArgumentException( __METHOD__ .
': seek error' );
112 $chunk_type = self::read( $fh, 4 );
113 $buf = self::read( $fh, $chunk_size );
114 $crc = self::read( $fh, self::$crcSize );
115 $computed = crc32( $chunk_type . $buf );
116 if ( pack(
'N', $computed ) !== $crc ) {
117 wfDebug( __METHOD__ .
': chunk has invalid CRC, skipping' );
121 if ( $chunk_type ===
"IHDR" ) {
122 $width = unpack(
'N', substr( $buf, 0, 4 ) )[1];
123 $height = unpack(
'N', substr( $buf, 4, 4 ) )[1];
124 $bitDepth = ord( substr( $buf, 8, 1 ) );
127 $colorType = match ( ord( substr( $buf, 9, 1 ) ) ) {
130 3 =>
'index-coloured',
131 4 =>
'greyscale-alpha',
132 6 =>
'truecolour-alpha',
135 } elseif ( $chunk_type ===
"acTL" ) {
136 if ( $chunk_size < 4 ) {
137 wfDebug( __METHOD__ .
": acTL chunk too small" );
141 $actl = unpack(
"Nframes/Nplays", $buf );
142 $frameCount = $actl[
'frames'];
143 $loopCount = $actl[
'plays'];
144 } elseif ( $chunk_type ===
"fcTL" ) {
145 $buf = substr( $buf, 20 );
146 if ( strlen( $buf ) < 4 ) {
147 wfDebug( __METHOD__ .
": fcTL chunk too small" );
151 $fctldur = unpack(
"ndelay_num/ndelay_den", $buf );
152 if ( $fctldur[
'delay_den'] == 0 ) {
153 $fctldur[
'delay_den'] = 100;
155 if ( $fctldur[
'delay_num'] ) {
156 $duration += $fctldur[
'delay_num'] / $fctldur[
'delay_den'];
158 } elseif ( $chunk_type ===
"iTXt" ) {
162 '/^([^\x00]{1,79})\x00(\x00|\x01)\x00([^\x00]*)(.)[^\x00]*\x00(.*)$/Ds',
171 $items[1] = strtolower( $items[1] );
172 if ( !isset( self::$textChunks[$items[1]] ) ) {
177 $items[3] = strtolower( $items[3] );
178 if ( $items[3] ==
'' ) {
180 $items[3] =
'x-default';
184 if ( $items[2] ===
"\x01" ) {
185 if ( function_exists(
'gzuncompress' ) && $items[4] ===
"\x00" ) {
187 $items[5] = @gzuncompress( $items[5] );
189 if ( $items[5] ===
false ) {
191 wfDebug( __METHOD__ .
' Error decompressing iTxt chunk - ' . $items[1] );
195 wfDebug( __METHOD__ .
' Skipping compressed png iTXt chunk due to lack of zlib,'
196 .
" or potentially invalid compression method" );
200 $finalKeyword = self::$textChunks[$items[1]];
201 $text[$finalKeyword][$items[3]] = $items[5];
202 $text[$finalKeyword][
'_type'] =
'lang';
205 wfDebug( __METHOD__ .
": Invalid iTXt chunk" );
207 } elseif ( $chunk_type ===
'tEXt' ) {
209 if ( !str_contains( $buf,
"\x00" ) ) {
210 wfDebug( __METHOD__ .
": Invalid tEXt chunk: no null byte" );
214 [ $keyword, $content ] = explode(
"\x00", $buf, 2 );
215 if ( $keyword ===
'' ) {
216 wfDebug( __METHOD__ .
": Empty tEXt keyword" );
221 $keyword = strtolower( $keyword );
222 if ( !isset( self::$textChunks[$keyword] ) ) {
227 $content = @iconv(
'ISO-8859-1',
'UTF-8', $content );
229 if ( $content ===
false ) {
230 wfDebug( __METHOD__ .
": Read error (error with iconv)" );
234 $finalKeyword = self::$textChunks[$keyword];
235 $text[$finalKeyword][
'x-default'] = $content;
236 $text[$finalKeyword][
'_type'] =
'lang';
237 } elseif ( $chunk_type ===
'zTXt' ) {
238 if ( function_exists(
'gzuncompress' ) ) {
240 if ( !str_contains( $buf,
"\x00" ) ) {
241 wfDebug( __METHOD__ .
": No null byte in zTXt chunk" );
245 [ $keyword, $postKeyword ] = explode(
"\x00", $buf, 2 );
246 if ( $keyword ===
'' || $postKeyword ===
'' ) {
247 wfDebug( __METHOD__ .
": Empty zTXt chunk" );
251 $keyword = strtolower( $keyword );
253 if ( !isset( self::$textChunks[$keyword] ) ) {
257 $compression = substr( $postKeyword, 0, 1 );
258 $content = substr( $postKeyword, 1 );
259 if ( $compression !==
"\x00" ) {
260 wfDebug( __METHOD__ .
" Unrecognized compression method in zTXt ($keyword). Skipping." );
265 $content = @gzuncompress( $content );
267 if ( $content ===
false ) {
269 wfDebug( __METHOD__ .
' Error decompressing zTXt chunk - ' . $keyword );
274 $content = @iconv(
'ISO-8859-1',
'UTF-8', $content );
276 if ( $content ===
false ) {
277 wfDebug( __METHOD__ .
": iconv error in zTXt chunk" );
281 $finalKeyword = self::$textChunks[$keyword];
282 $text[$finalKeyword][
'x-default'] = $content;
283 $text[$finalKeyword][
'_type'] =
'lang';
285 wfDebug( __METHOD__ .
" Cannot decompress zTXt chunk due to lack of zlib. Skipping." );
287 } elseif ( $chunk_type ===
'tIME' ) {
289 if ( $chunk_size !== 7 ) {
290 wfDebug( __METHOD__ .
": tIME wrong size" );
295 $t = unpack(
"ny/Cm/Cd/Ch/Cmin/Cs", $buf );
296 $strTime = sprintf(
"%04d%02d%02d%02d%02d%02d",
297 $t[
'y'], $t[
'm'], $t[
'd'], $t[
'h'],
298 $t[
'min'], $t[
's'] );
303 $text[
'DateTime'] = $exifTime;
305 } elseif ( $chunk_type ===
'pHYs' ) {
307 if ( $chunk_size !== 9 ) {
308 wfDebug( __METHOD__ .
": pHYs wrong size" );
312 $dim = unpack(
"Nwidth/Nheight/Cunit", $buf );
313 if ( $dim[
'unit'] === 1 ) {
316 if ( $dim[
'width'] > 0 && $dim[
'height'] > 0 ) {
319 $text[
'XResolution'] = $dim[
'width']
321 $text[
'YResolution'] = $dim[
'height']
323 $text[
'ResolutionUnit'] = 3;
327 } elseif ( $chunk_type ===
"eXIf" ) {
332 substr( $buf, 0, 4 ) !==
"II\x2A\x00" &&
333 substr( $buf, 0, 4 ) !==
"MM\x00\x2A"
336 wfDebug( __METHOD__ .
": Invalid eXIf tag" );
339 } elseif ( $chunk_type ===
"IEND" ) {
345 if ( $loopCount > 1 ) {
346 $duration *= $loopCount;
349 if ( isset( $text[
'DateTimeDigitized'] ) ) {
351 foreach ( $text[
'DateTimeDigitized'] as $name => &$value ) {
352 if ( $name ===
'_type' ) {
379 'frameCount' => $frameCount,
380 'loopCount' => $loopCount,
381 'duration' => $duration,
383 'bitDepth' => $bitDepth,
384 'colorType' => $colorType,