Make $file->getLength() return duration of GIF and APNG files.
authorBrian Wolff <bawolff+wn@gmail.com>
Sun, 14 Sep 2014 21:26:05 +0000 (18:26 -0300)
committerGergő Tisza <gtisza@wikimedia.org>
Thu, 24 Sep 2015 00:46:17 +0000 (00:46 +0000)
Its not just ogg files that have a duration, animated GIFs
and APNGs do too.

After If172a1cba is merged, this will show up in the api.

Change-Id: Ie7cf00df34950ae404fcf655dd16397a273e5523

includes/media/GIF.php
includes/media/PNG.php
tests/phpunit/includes/media/GIFTest.php
tests/phpunit/includes/media/PNGTest.php

index 94aca61..b998d18 100644 (file)
@@ -187,4 +187,25 @@ class GIFHandler extends BitmapHandler {
 
                return $wgLang->commaList( $info );
        }
+
+       /**
+        * Return the duration of the GIF file.
+        *
+        * Shown in the &query=imageinfo&iiprop=size api query.
+        *
+        * @param $file File
+        * @return float The duration of the file.
+        */
+       public function getLength( $file ) {
+               $serMeta = $file->getMetadata();
+               MediaWiki\suppressWarnings();
+               $metadata = unserialize( $serMeta );
+               MediaWiki\restoreWarnings();
+
+               if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
+                       return 0.0;
+               } else {
+                       return (float)$metadata['duration'];
+               }
+       }
 }
index c3f0832..e297fe1 100644 (file)
@@ -175,9 +175,29 @@ class PNGHandler extends BitmapHandler {
                return $wgLang->commaList( $info );
        }
 
+       /**
+        * Return the duration of an APNG file.
+        *
+        * Shown in the &query=imageinfo&iiprop=size api query.
+        *
+        * @param $file File
+        * @return float The duration of the file.
+        */
+       public function getLength( $file ) {
+               $serMeta = $file->getMetadata();
+               MediaWiki\suppressWarnings();
+               $metadata = unserialize( $serMeta );
+               MediaWiki\restoreWarnings();
+
+               if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
+                       return 0.0;
+               } else {
+                       return (float)$metadata['duration'];
+               }
+       }
+
        // PNGs should be easy to support, but it will need some sharpening applied
        // and another user test to check if the perceived quality change is noticeable
-
        public function supportsBucketing() {
                return false;
        }
index 87ffd99..de1e153 100644 (file)
@@ -139,4 +139,24 @@ class GIFHandlerTest extends MediaWikiMediaTestCase {
                        ),
                );
        }
+
+       /**
+        * @param $filename string
+        * @param $expectedLength float
+        * @dataProvider provideGetLength
+        */
+       public function testGetLength( $filename, $expectedLength ) {
+               $file = $this->dataFile( $filename, 'image/gif' );
+               $actualLength = $file->getLength();
+               $this->assertEquals( $expectedLength, $actualLength, '', 0.00001 );
+       }
+
+       public function provideGetLength() {
+               return array(
+                       array( 'animated.gif', 2.4 ),
+                       array( 'animated-xmp.gif', 2.4 ),
+                       array( 'nonanimated', 0.0 ),
+                       array( 'Bishzilla_blink.gif', 1.4 ),
+               );
+       }
 }
index 36872a7..96ede90 100644 (file)
@@ -128,4 +128,24 @@ class PNGHandlerTest extends MediaWikiMediaTestCase {
                        ),
                );
        }
+
+       /**
+        * @param $filename string
+        * @param $expectedLength float
+        * @dataProvider provideGetLength
+        */
+       public function testGetLength( $filename, $expectedLength ) {
+               $file = $this->dataFile( $filename, 'image/png' );
+               $actualLength = $file->getLength();
+               $this->assertEquals( $expectedLength, $actualLength, '', 0.00001 );
+       }
+
+       public function provideGetLength() {
+               return array(
+                       array( 'Animated_PNG_example_bouncing_beach_ball.png', 1.5 ),
+                       array( 'Png-native-test.png', 0.0 ),
+                       array( 'greyscale-png.png', 0.0 ),
+                       array( '1bit-png.png', 0.0 ),
+               );
+       }
 }