From: Brian Wolff Date: Sun, 14 Sep 2014 21:26:05 +0000 (-0300) Subject: Make $file->getLength() return duration of GIF and APNG files. X-Git-Tag: 1.31.0-rc.0~9874^2 X-Git-Url: http://git.cyclocoop.org/%7B%24admin_url%7Dmes_infos.php?a=commitdiff_plain;h=20f90051034c1e5e3c75c1add0b0fd7ae1e802b5;p=lhc%2Fweb%2Fwiklou.git Make $file->getLength() return duration of GIF and APNG files. 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 --- diff --git a/includes/media/GIF.php b/includes/media/GIF.php index 94aca615ae..b998d185f9 100644 --- a/includes/media/GIF.php +++ b/includes/media/GIF.php @@ -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']; + } + } } diff --git a/includes/media/PNG.php b/includes/media/PNG.php index c3f08325bc..e297fe1b45 100644 --- a/includes/media/PNG.php +++ b/includes/media/PNG.php @@ -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; } diff --git a/tests/phpunit/includes/media/GIFTest.php b/tests/phpunit/includes/media/GIFTest.php index 87ffd995e9..de1e153e36 100644 --- a/tests/phpunit/includes/media/GIFTest.php +++ b/tests/phpunit/includes/media/GIFTest.php @@ -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 ), + ); + } } diff --git a/tests/phpunit/includes/media/PNGTest.php b/tests/phpunit/includes/media/PNGTest.php index 36872a75d2..96ede90de1 100644 --- a/tests/phpunit/includes/media/PNGTest.php +++ b/tests/phpunit/includes/media/PNGTest.php @@ -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 ), + ); + } }