From: Bryan Tong Minh Date: Fri, 18 Mar 2011 23:03:58 +0000 (+0000) Subject: Extract bit depth and color type for PNG images X-Git-Tag: 1.31.0-rc.0~31330 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/operations/recherche.php?a=commitdiff_plain;h=f71e2b1189f28a43671349bac7945075c9ed4929;p=lhc%2Fweb%2Fwiklou.git Extract bit depth and color type for PNG images --- diff --git a/includes/media/PNGMetadataExtractor.php b/includes/media/PNGMetadataExtractor.php index 6a931e6c57..56e6ee9c56 100644 --- a/includes/media/PNGMetadataExtractor.php +++ b/includes/media/PNGMetadataExtractor.php @@ -25,6 +25,8 @@ class PNGMetadataExtractor { $frameCount = 0; $loopCount = 1; $duration = 0.0; + $bitDepth = 0; + $colorType = 'unknown'; if (!$filename) throw new Exception( __METHOD__ . ": No file name specified" ); @@ -57,7 +59,35 @@ class PNGMetadataExtractor { throw new Exception( __METHOD__ . ": Read error" ); } - if ( $chunk_type == "acTL" ) { + if ( $chunk_type == "IHDR" ) { + $buf = fread( $fh, $chunk_size ); + if( !$buf ) { + throw new Exception( __METHOD__ . ": Read error" ); + } + $bitDepth = ord( substr( $buf, 8, 1 ) ); + // Detect the color type in British English as per the spec + // http://www.w3.org/TR/PNG/#11IHDR + switch ( ord( substr( $buf, 9, 1 ) ) ) { + case 0: + $colorType = 'greyscale'; + break; + case 2: + $colorType = 'truecolour'; + break; + case 3: + $colorType = 'index-coloured'; + break; + case 4: + $colorType = 'greyscale-alpha'; + break; + case 6: + $colorType = 'truecolour-alpha'; + break; + default: + $colorType = 'unknown'; + break; + } + } elseif ( $chunk_type == "acTL" ) { $buf = fread( $fh, $chunk_size ); if( !$buf ) { throw new Exception( __METHOD__ . ": Read error" ); @@ -97,7 +127,9 @@ class PNGMetadataExtractor { return array( 'frameCount' => $frameCount, 'loopCount' => $loopCount, - 'duration' => $duration + 'duration' => $duration, + 'bitDepth' => $bitDepth, + 'colorType' => $colorType, ); }