49 self::$pngSig = pack(
"C8", 137, 80, 78, 71, 13, 10, 26, 10 );
55 'xml:com.adobe.xmp' =>
'xmp',
56 # Artist is unofficial. Author is the recommended
57 # keyword in the PNG spec. However some people output
58 # Artist so support both.
63 'comment' =>
'PNGFileComment',
64 'description' =>
'ImageDescription',
65 'title' =>
'ObjectName',
66 'copyright' =>
'Copyright',
67 # Source as in original device used to make image
68 # not as in who gave you the image
70 'software' =>
'Software',
71 'disclaimer' =>
'Disclaimer',
72 'warning' =>
'ContentWarning',
73 'url' =>
'Identifier', # Not sure
if this is best mapping. Maybe WebStatement.
75 'creation time' =>
'DateTimeDigitized',
86 $colorType =
'unknown';
89 throw new Exception( __METHOD__ .
": No file name specified" );
92 if ( !file_exists( $filename ) || is_dir( $filename ) ) {
93 throw new Exception( __METHOD__ .
": File $filename does not exist" );
96 $fh = fopen( $filename,
'rb' );
99 throw new Exception( __METHOD__ .
": Unable to open file $filename" );
103 $buf = self::read( $fh, 8 );
104 if ( $buf !== self::$pngSig ) {
105 throw new Exception( __METHOD__ .
": Not a valid PNG file; header: $buf" );
109 while ( !feof( $fh ) ) {
110 $buf = self::read( $fh, 4 );
111 $chunk_size = unpack(
"N", $buf )[1];
113 if ( $chunk_size < 0 || $chunk_size > self::MAX_CHUNK_SIZE ) {
114 wfDebug( __METHOD__ .
': Chunk size of ' . $chunk_size .
115 ' too big, skipping. Max size is: ' . self::MAX_CHUNK_SIZE );
116 if ( fseek( $fh, 4 + $chunk_size + self::$crcSize, SEEK_CUR ) !== 0 ) {
117 throw new Exception( __METHOD__ .
': seek error' );
122 $chunk_type = self::read( $fh, 4 );
123 $buf = self::read( $fh, $chunk_size );
124 $crc = self::read( $fh, self::$crcSize );
125 $computed = crc32( $chunk_type . $buf );
126 if ( pack(
'N', $computed ) !== $crc ) {
127 wfDebug( __METHOD__ .
': chunk has invalid CRC, skipping' );
131 if ( $chunk_type ===
"IHDR" ) {
132 $width = unpack(
'N', substr( $buf, 0, 4 ) )[1];
133 $height = unpack(
'N', substr( $buf, 4, 4 ) )[1];
134 $bitDepth = ord( substr( $buf, 8, 1 ) );
137 switch ( ord( substr( $buf, 9, 1 ) ) ) {
139 $colorType =
'greyscale';
142 $colorType =
'truecolour';
145 $colorType =
'index-coloured';
148 $colorType =
'greyscale-alpha';
151 $colorType =
'truecolour-alpha';
154 $colorType =
'unknown';
157 } elseif ( $chunk_type ===
"acTL" ) {
158 if ( $chunk_size < 4 ) {
159 wfDebug( __METHOD__ .
": acTL chunk too small" );
163 $actl = unpack(
"Nframes/Nplays", $buf );
164 $frameCount = $actl[
'frames'];
165 $loopCount = $actl[
'plays'];
166 } elseif ( $chunk_type ===
"fcTL" ) {
167 $buf = substr( $buf, 20 );
168 if ( strlen( $buf ) < 4 ) {
169 wfDebug( __METHOD__ .
": fcTL chunk too small" );
173 $fctldur = unpack(
"ndelay_num/ndelay_den", $buf );
174 if ( $fctldur[
'delay_den'] == 0 ) {
175 $fctldur[
'delay_den'] = 100;
177 if ( $fctldur[
'delay_num'] ) {
178 $duration += $fctldur[
'delay_num'] / $fctldur[
'delay_den'];
180 } elseif ( $chunk_type ===
"iTXt" ) {
184 '/^([^\x00]{1,79})\x00(\x00|\x01)\x00([^\x00]*)(.)[^\x00]*\x00(.*)$/Ds',
193 $items[1] = strtolower( $items[1] );
194 if ( !isset( self::$textChunks[$items[1]] ) ) {
199 $items[3] = strtolower( $items[3] );
200 if ( $items[3] ==
'' ) {
202 $items[3] =
'x-default';
206 if ( $items[2] ===
"\x01" ) {
207 if ( function_exists(
'gzuncompress' ) && $items[4] ===
"\x00" ) {
208 AtEase::suppressWarnings();
209 $items[5] = gzuncompress( $items[5] );
210 AtEase::restoreWarnings();
212 if ( $items[5] ===
false ) {
214 wfDebug( __METHOD__ .
' Error decompressing iTxt chunk - ' . $items[1] );
218 wfDebug( __METHOD__ .
' Skipping compressed png iTXt chunk due to lack of zlib,'
219 .
" or potentially invalid compression method" );
223 $finalKeyword = self::$textChunks[$items[1]];
224 $text[$finalKeyword][$items[3]] = $items[5];
225 $text[$finalKeyword][
'_type'] =
'lang';
228 wfDebug( __METHOD__ .
": Invalid iTXt chunk" );
230 } elseif ( $chunk_type ===
'tEXt' ) {
232 if ( strpos( $buf,
"\x00" ) ===
false ) {
233 wfDebug( __METHOD__ .
": Invalid tEXt chunk: no null byte" );
237 [ $keyword,
$content ] = explode(
"\x00", $buf, 2 );
238 if ( $keyword ===
'' ) {
239 wfDebug( __METHOD__ .
": Empty tEXt keyword" );
244 $keyword = strtolower( $keyword );
245 if ( !isset( self::$textChunks[$keyword] ) ) {
249 AtEase::suppressWarnings();
251 AtEase::restoreWarnings();
254 wfDebug( __METHOD__ .
": Read error (error with iconv)" );
258 $finalKeyword = self::$textChunks[$keyword];
259 $text[$finalKeyword][
'x-default'] =
$content;
260 $text[$finalKeyword][
'_type'] =
'lang';
261 } elseif ( $chunk_type ===
'zTXt' ) {
262 if ( function_exists(
'gzuncompress' ) ) {
264 if ( strpos( $buf,
"\x00" ) ===
false ) {
265 wfDebug( __METHOD__ .
": No null byte in zTXt chunk" );
269 [ $keyword, $postKeyword ] = explode(
"\x00", $buf, 2 );
270 if ( $keyword ===
'' || $postKeyword ===
'' ) {
271 wfDebug( __METHOD__ .
": Empty zTXt chunk" );
275 $keyword = strtolower( $keyword );
277 if ( !isset( self::$textChunks[$keyword] ) ) {
281 $compression = substr( $postKeyword, 0, 1 );
282 $content = substr( $postKeyword, 1 );
283 if ( $compression !==
"\x00" ) {
284 wfDebug( __METHOD__ .
" Unrecognized compression method in zTXt ($keyword). Skipping." );
288 AtEase::suppressWarnings();
290 AtEase::restoreWarnings();
294 wfDebug( __METHOD__ .
' Error decompressing zTXt chunk - ' . $keyword );
298 AtEase::suppressWarnings();
300 AtEase::restoreWarnings();
303 wfDebug( __METHOD__ .
": iconv error in zTXt chunk" );
307 $finalKeyword = self::$textChunks[$keyword];
308 $text[$finalKeyword][
'x-default'] =
$content;
309 $text[$finalKeyword][
'_type'] =
'lang';
311 wfDebug( __METHOD__ .
" Cannot decompress zTXt chunk due to lack of zlib. Skipping." );
313 } elseif ( $chunk_type ===
'tIME' ) {
315 if ( $chunk_size !== 7 ) {
316 wfDebug( __METHOD__ .
": tIME wrong size" );
321 $t = unpack(
"ny/Cm/Cd/Ch/Cmin/Cs", $buf );
322 $strTime = sprintf(
"%04d%02d%02d%02d%02d%02d",
324 $t[
'min'],
$t[
's'] );
329 $text[
'DateTime'] = $exifTime;
331 } elseif ( $chunk_type ===
'pHYs' ) {
333 if ( $chunk_size !== 9 ) {
334 wfDebug( __METHOD__ .
": pHYs wrong size" );
338 $dim = unpack(
"Nwidth/Nheight/Cunit", $buf );
339 if ( $dim[
'unit'] === 1 ) {
342 if ( $dim[
'width'] > 0 && $dim[
'height'] > 0 ) {
345 $text[
'XResolution'] = $dim[
'width']
347 $text[
'YResolution'] = $dim[
'height']
349 $text[
'ResolutionUnit'] = 3;
353 } elseif ( $chunk_type ===
"IEND" ) {
359 if ( $loopCount > 1 ) {
360 $duration *= $loopCount;
363 if ( isset( $text[
'DateTimeDigitized'] ) ) {
365 foreach ( $text[
'DateTimeDigitized'] as $name => &$value ) {
366 if ( $name ===
'_type' ) {
393 'frameCount' => $frameCount,
394 'loopCount' => $loopCount,
395 'duration' => $duration,
397 'bitDepth' => $bitDepth,
398 'colorType' => $colorType,