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',
84 $colorType =
'unknown';
87 throw new Exception( __METHOD__ .
": No file name specified" );
88 } elseif ( !file_exists( $filename ) || is_dir( $filename ) ) {
89 throw new Exception( __METHOD__ .
": File $filename does not exist" );
92 $fh = fopen( $filename,
'rb' );
95 throw new Exception( __METHOD__ .
": Unable to open file $filename" );
100 if ( $buf != self::$pngSig ) {
101 throw new Exception( __METHOD__ .
": Not a valid PNG file; header: $buf" );
105 while ( !feof( $fh ) ) {
107 $chunk_size = unpack(
"N", $buf )[1];
109 if ( $chunk_size < 0 || $chunk_size > self::MAX_CHUNK_SIZE ) {
110 wfDebug( __METHOD__ .
': Chunk size of ' . $chunk_size .
111 ' too big, skipping. Max size is: ' . self::MAX_CHUNK_SIZE );
112 if ( fseek( $fh, 4 + $chunk_size + self::$crcSize, SEEK_CUR ) !== 0 ) {
113 throw new Exception( __METHOD__ .
': seek error' );
121 $computed = crc32( $chunk_type . $buf );
122 if ( pack(
'N', $computed ) !== $crc ) {
123 wfDebug( __METHOD__ .
': chunk has invalid CRC, skipping' );
127 if ( $chunk_type ==
"IHDR" ) {
128 $width = unpack(
'N', substr( $buf, 0, 4 ) )[1];
129 $height = unpack(
'N', substr( $buf, 4, 4 ) )[1];
130 $bitDepth = ord( substr( $buf, 8, 1 ) );
133 switch ( ord( substr( $buf, 9, 1 ) ) ) {
135 $colorType =
'greyscale';
138 $colorType =
'truecolour';
141 $colorType =
'index-coloured';
144 $colorType =
'greyscale-alpha';
147 $colorType =
'truecolour-alpha';
150 $colorType =
'unknown';
153 } elseif ( $chunk_type ==
"acTL" ) {
154 if ( $chunk_size < 4 ) {
155 wfDebug( __METHOD__ .
": acTL chunk too small" );
159 $actl = unpack(
"Nframes/Nplays", $buf );
160 $frameCount = $actl[
'frames'];
161 $loopCount = $actl[
'plays'];
162 } elseif ( $chunk_type ==
"fcTL" ) {
163 $buf = substr( $buf, 20 );
164 if ( strlen( $buf ) < 4 ) {
165 wfDebug( __METHOD__ .
": fcTL chunk too small" );
169 $fctldur = unpack(
"ndelay_num/ndelay_den", $buf );
170 if ( $fctldur[
'delay_den'] == 0 ) {
171 $fctldur[
'delay_den'] = 100;
173 if ( $fctldur[
'delay_num'] ) {
174 $duration += $fctldur[
'delay_num'] / $fctldur[
'delay_den'];
176 } elseif ( $chunk_type ==
"iTXt" ) {
180 '/^([^\x00]{1,79})\x00(\x00|\x01)\x00([^\x00]*)(.)[^\x00]*\x00(.*)$/Ds',
189 $items[1] = strtolower( $items[1] );
190 if ( !isset( self::$textChunks[$items[1]] ) ) {
195 $items[3] = strtolower( $items[3] );
196 if ( $items[3] ==
'' ) {
198 $items[3] =
'x-default';
202 if ( $items[2] ==
"\x01" ) {
203 if ( function_exists(
'gzuncompress' ) && $items[4] ===
"\x00" ) {
204 Wikimedia\suppressWarnings();
205 $items[5] = gzuncompress( $items[5] );
206 Wikimedia\restoreWarnings();
208 if ( $items[5] ===
false ) {
210 wfDebug( __METHOD__ .
' Error decompressing iTxt chunk - ' . $items[1] );
214 wfDebug( __METHOD__ .
' Skipping compressed png iTXt chunk due to lack of zlib,'
215 .
" or potentially invalid compression method" );
219 $finalKeyword = self::$textChunks[$items[1]];
220 $text[$finalKeyword][$items[3]] = $items[5];
221 $text[$finalKeyword][
'_type'] =
'lang';
224 wfDebug( __METHOD__ .
": Invalid iTXt chunk" );
226 } elseif ( $chunk_type ==
'tEXt' ) {
228 if ( strpos( $buf,
"\x00" ) ===
false ) {
229 wfDebug( __METHOD__ .
": Invalid tEXt chunk: no null byte" );
233 list( $keyword,
$content ) = explode(
"\x00", $buf, 2 );
234 if ( $keyword ===
'' ) {
235 wfDebug( __METHOD__ .
": Empty tEXt keyword" );
240 $keyword = strtolower( $keyword );
241 if ( !isset( self::$textChunks[$keyword] ) ) {
245 Wikimedia\suppressWarnings();
247 Wikimedia\restoreWarnings();
250 wfDebug( __METHOD__ .
": Read error (error with iconv)" );
254 $finalKeyword = self::$textChunks[$keyword];
255 $text[$finalKeyword][
'x-default'] =
$content;
256 $text[$finalKeyword][
'_type'] =
'lang';
257 } elseif ( $chunk_type ==
'zTXt' ) {
258 if ( function_exists(
'gzuncompress' ) ) {
260 if ( strpos( $buf,
"\x00" ) ===
false ) {
261 wfDebug( __METHOD__ .
": No null byte in zTXt chunk" );
265 list( $keyword, $postKeyword ) = explode(
"\x00", $buf, 2 );
266 if ( $keyword ===
'' || $postKeyword ===
'' ) {
267 wfDebug( __METHOD__ .
": Empty zTXt chunk" );
271 $keyword = strtolower( $keyword );
273 if ( !isset( self::$textChunks[$keyword] ) ) {
277 $compression = substr( $postKeyword, 0, 1 );
278 $content = substr( $postKeyword, 1 );
279 if ( $compression !==
"\x00" ) {
280 wfDebug( __METHOD__ .
" Unrecognized compression method in zTXt ($keyword). Skipping." );
284 Wikimedia\suppressWarnings();
286 Wikimedia\restoreWarnings();
290 wfDebug( __METHOD__ .
' Error decompressing zTXt chunk - ' . $keyword );
294 Wikimedia\suppressWarnings();
296 Wikimedia\restoreWarnings();
299 wfDebug( __METHOD__ .
": iconv error in zTXt chunk" );
303 $finalKeyword = self::$textChunks[$keyword];
304 $text[$finalKeyword][
'x-default'] =
$content;
305 $text[$finalKeyword][
'_type'] =
'lang';
307 wfDebug( __METHOD__ .
" Cannot decompress zTXt chunk due to lack of zlib. Skipping." );
309 } elseif ( $chunk_type ==
'tIME' ) {
311 if ( $chunk_size !== 7 ) {
312 wfDebug( __METHOD__ .
": tIME wrong size" );
317 $t = unpack(
"ny/Cm/Cd/Ch/Cmin/Cs", $buf );
318 $strTime = sprintf(
"%04d%02d%02d%02d%02d%02d",
320 $t[
'min'],
$t[
's'] );
325 $text[
'DateTime'] = $exifTime;
327 } elseif ( $chunk_type ==
'pHYs' ) {
329 if ( $chunk_size !== 9 ) {
330 wfDebug( __METHOD__ .
": pHYs wrong size" );
334 $dim = unpack(
"Nwidth/Nheight/Cunit", $buf );
335 if ( $dim[
'unit'] == 1 ) {
338 if ( $dim[
'width'] > 0 && $dim[
'height'] > 0 ) {
341 $text[
'XResolution'] = $dim[
'width']
343 $text[
'YResolution'] = $dim[
'height']
345 $text[
'ResolutionUnit'] = 3;
349 } elseif ( $chunk_type ==
"IEND" ) {
355 if ( $loopCount > 1 ) {
356 $duration *= $loopCount;
359 if ( isset( $text[
'DateTimeDigitized'] ) ) {
361 foreach ( $text[
'DateTimeDigitized'] as $name => &$value ) {
362 if ( $name ===
'_type' ) {
389 'frameCount' => $frameCount,
390 'loopCount' => $loopCount,
391 'duration' => $duration,
393 'bitDepth' => $bitDepth,
394 'colorType' => $colorType,