From: Derk-Jan Hartman Date: Sun, 20 Jun 2010 16:09:12 +0000 (+0000) Subject: Add a new PNG parser in order to recognize APNG (animated PNG) images. X-Git-Tag: 1.31.0-rc.0~36447 X-Git-Url: http://git.cyclocoop.org//%27%40script%40/%27?a=commitdiff_plain;h=0ccd98bdb63c97862d0d7096992d471f4db8a19d;p=lhc%2Fweb%2Fwiklou.git Add a new PNG parser in order to recognize APNG (animated PNG) images. --- diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 312f20a481..2c4598dbf3 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -444,6 +444,8 @@ $wgAutoloadLocalClasses = array( 'MediaHandler' => 'includes/media/Generic.php', 'MediaTransformError' => 'includes/media/MediaTransformOutput.php', 'MediaTransformOutput' => 'includes/media/MediaTransformOutput.php', + 'PNGHandler' => 'includes/media/PNG.php', + 'PNGMetadataExtractor' => 'includes/media/PNGMetadataExtractor.php', 'SvgHandler' => 'includes/media/SVG.php', 'ThumbnailImage' => 'includes/media/MediaTransformOutput.php', 'TiffHandler' => 'includes/media/Tiff.php', diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 2bc8f6a318..e27ac42273 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -562,7 +562,7 @@ $wgTrustedMediaFormats = array( */ $wgMediaHandlers = array( 'image/jpeg' => 'BitmapHandler', - 'image/png' => 'BitmapHandler', + 'image/png' => 'PNGHandler', 'image/gif' => 'GIFHandler', 'image/tiff' => 'TiffHandler', 'image/x-ms-bmp' => 'BmpHandler', diff --git a/includes/media/PNG.php b/includes/media/PNG.php new file mode 100644 index 0000000000..babaddea19 --- /dev/null +++ b/includes/media/PNG.php @@ -0,0 +1,82 @@ +parsedPNGMetadata) ) { + try { + $image->parsedPNGMetadata = PNGMetadataExtractor::getMetadata( $filename ); + } catch( Exception $e ) { + // Broken file? + wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" ); + return '0'; + } + } + + return serialize($image->parsedPNGMetadata); + + } + + function formatMetadata( $image ) { + return false; + } + + function isAnimatedImage( $image ) { + $ser = $image->getMetadata(); + if ($ser) { + $metadata = unserialize($ser); + if( $metadata['frameCount'] > 1 ) return true; + } + return false; + } + + function getMetadataType( $image ) { + return 'parsed-png'; + } + + function isMetadataValid( $image, $metadata ) { + wfSuppressWarnings(); + $data = unserialize( $metadata ); + wfRestoreWarnings(); + return (boolean) $data; + } + function getLongDesc( $image ) { + global $wgUser, $wgLang; + $sk = $wgUser->getSkin(); + $original = parent::getLongDesc( $image ); + + wfSuppressWarnings(); + $metadata = unserialize($image->getMetadata()); + wfRestoreWarnings(); + + if( !metadata || $metadata['frameCount'] == 0 ) + return $original; + + $info[] = substr( $original, 1, strlen( $original )-2 ); + + if ($metadata['loopCount'] == 0) + $info[] = wfMsgExt( 'file-info-png-looped', 'parseinline' ); + elseif ($metadata['loopCount'] > 1) + $info[] = wfMsgExt( 'file-info-png-repeat', 'parseinline', $metadata['loopCount'] );; + + if ($metadata['frameCount'] > 0) + $info[] = wfMsgExt( 'file-info-png-frames', 'parseinline', $metadata['frameCount'] ); + + if ($metadata['duration']) + $info[] = $wgLang->formatTimePeriod( $metadata['duration'] ); + + $infoString = $wgLang->commaList( $info ); + + return "($infoString)"; + } + +} diff --git a/includes/media/PNGMetadataExtractor.php b/includes/media/PNGMetadataExtractor.php new file mode 100644 index 0000000000..2e87ff3b7b --- /dev/null +++ b/includes/media/PNGMetadataExtractor.php @@ -0,0 +1,87 @@ + 1 ) { + $duration *= $loopCount; + } + + return array( + 'frameCount' => $frameCount, + 'loopCount' => $loopCount, + 'duration' => $duration + ); + + } +} diff --git a/includes/mime.types b/includes/mime.types index df27d1fa86..76e63a6277 100644 --- a/includes/mime.types +++ b/includes/mime.types @@ -78,7 +78,7 @@ image/cgm cgm image/gif gif image/ief ief image/jpeg jpeg jpg jpe -image/png png +image/png png apng image/svg+xml svg image/tiff tiff tif image/vnd.djvu djvu djv diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index e78edc919f..e3c41db238 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -3552,6 +3552,9 @@ By executing it, your system may be compromised.
", 'show-big-image-thumb' => 'Size of this preview: $1 × $2 pixels', 'file-info-gif-looped' => 'looped', 'file-info-gif-frames' => '$1 {{PLURAL:$1|frame|frames}}', +'file-info-png-looped' => 'looped', +'file-info-png-repeat' => 'played $1 times', +'file-info-png-frames' => '$1 {{PLURAL:$1|frame|frames}}', # Special:NewFiles 'newimages' => 'Gallery of new files', diff --git a/languages/messages/MessagesQqq.php b/languages/messages/MessagesQqq.php index 46d3e98b5d..e48bfbd1a2 100644 --- a/languages/messages/MessagesQqq.php +++ b/languages/messages/MessagesQqq.php @@ -2933,6 +2933,9 @@ The message appears after the name of the patroller.', 'show-big-image-thumb' => 'File info displayed on file description page.', 'file-info-gif-looped' => 'Part of the information provided about a [http://en.wikipedia.org/wiki/Gif .gif file] on its file description page. Looped means repeating in the context of an animated gif. It is a sequence of images, each displayed after the other, and the first one displayed after the last, in a never ending loop. For example of message in use see [[:File:Mouse10.gif]].', 'file-info-gif-frames' => 'Part of the information provided about a [http://en.wikipedia.org/wiki/Gif .gif file] on its file description page. +'file-info-png-looped' => 'Part of the information provided about a [http://en.wikipedia.org/wiki/APNG .apng file] on its file description page. Looped means repeating indefinetly in the context of an animated png. It is a sequence of images, each displayed after the other, and the first one displayed after the last, in a never ending loop.', +'file-info-png-repeat' => 'Part of the information provided about a [http://en.wikipedia.org/wiki/APNG .apng file] on its file description page. The sequence of images is repeating a limited amount of time. It is a sequence of images, each displayed after the other, and the first one displayed after the last, for $1 times.', +'file-info-png-frames' => 'Part of the information provided about a [http://en.wikipedia.org/wiki/APNG .apng file] on its file description page. The variable $1 is the number of individual frames in an animated gif file. diff --git a/maintenance/language/messages.inc b/maintenance/language/messages.inc index dee1c0e52d..4931c37a48 100644 --- a/maintenance/language/messages.inc +++ b/maintenance/language/messages.inc @@ -2510,6 +2510,9 @@ $wgMessageStructure = array( 'show-big-image-thumb', 'file-info-gif-looped', 'file-info-gif-frames', + 'file-info-png-looped', + 'file-info-png-repeat', + 'file-info-png-frames', ), 'newfiles' => array( 'newimages',