4 * Slightly derived from GIFMetadataExtractor.php
5 * Deliberately not using MWExceptions to avoid external dependencies, encouraging
17 class PNGMetadataExtractor
{
21 static function getMetadata( $filename ) {
22 self
::$png_sig = pack( "C8", 137, 80, 78, 71, 13, 10, 26, 10 );
30 throw new Exception( __METHOD__
. ": No file name specified" );
31 elseif ( !file_exists($filename) ||
is_dir($filename) )
32 throw new Exception( __METHOD__
. ": File $filename does not exist" );
34 $fh = fopen( $filename, 'r' );
37 throw new Exception( __METHOD__
. ": Unable to open file $filename" );
40 // Check for the PNG header
41 $buf = fread( $fh, 8 );
42 if ( $buf != self
::$png_sig ) {
43 throw new Exception( __METHOD__
. ": Not a valid PNG file; header: $buf" );
47 while( !feof( $fh ) ) {
48 $buf = fread( $fh, 4 );
50 throw new Exception( __METHOD__
. ": Read error" );
52 $chunk_size = unpack( "N", $buf);
53 $chunk_size = $chunk_size[1];
55 $chunk_type = fread( $fh, 4 );
57 throw new Exception( __METHOD__
. ": Read error" );
60 if ( $chunk_type == "acTL" ) {
61 $buf = fread( $fh, $chunk_size );
63 throw new Exception( __METHOD__
. ": Read error" );
66 $actl = unpack( "Nframes/Nplays", $buf );
67 $frameCount = $actl['frames'];
68 $loopCount = $actl['plays'];
69 } elseif ( $chunk_type == "fcTL" ) {
70 $buf = fread( $fh, $chunk_size );
72 throw new Exception( __METHOD__
. ": Read error" );
74 $buf = substr( $buf, 20 );
76 $fctldur = unpack( "ndelay_num/ndelay_den", $buf );
77 if( $fctldur['delay_den'] == 0 ) $fctldur['delay_den'] = 100;
78 if( $fctldur['delay_num'] ) {
79 $duration +
= $fctldur['delay_num'] / $fctldur['delay_den'];
81 } elseif ( ( $chunk_type == "IDAT" ||
$chunk_type == "IEND" ) && $frameCount == 0 ) {
82 // Not a valid animated image. No point in continuing.
84 } elseif ( $chunk_type == "IEND" ) {
87 fseek( $fh, $chunk_size, SEEK_CUR
);
89 fseek( $fh, self
::$CRC_size, SEEK_CUR
);
93 if( $loopCount > 1 ) {
94 $duration *= $loopCount;
98 'frameCount' => $frameCount,
99 'loopCount' => $loopCount,
100 'duration' => $duration