2 class PNGMetadataExtractorTest
extends MediaWikiTestCase
{
4 protected function setUp() {
6 $this->filePath
= __DIR__
. '/../../data/media/';
9 * Tests zTXt tag (compressed textual metadata)
11 function testPngNativetZtxt() {
12 $this->checkPHPExtension( 'zlib' );
14 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
15 'Png-native-test.png' );
16 $expected = "foo bar baz foo foo foo foof foo foo foo foo";
17 $this->assertArrayHasKey( 'text', $meta );
18 $meta = $meta['text'];
19 $this->assertArrayHasKey( 'Make', $meta );
20 $this->assertArrayHasKey( 'x-default', $meta['Make'] );
22 $this->assertEquals( $expected, $meta['Make']['x-default'] );
26 * Test tEXt tag (Uncompressed textual metadata)
28 function testPngNativeText() {
29 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
30 'Png-native-test.png' );
31 $expected = "Some long image desc";
32 $this->assertArrayHasKey( 'text', $meta );
33 $meta = $meta['text'];
34 $this->assertArrayHasKey( 'ImageDescription', $meta );
35 $this->assertArrayHasKey( 'x-default', $meta['ImageDescription'] );
36 $this->assertArrayHasKey( '_type', $meta['ImageDescription'] );
38 $this->assertEquals( $expected, $meta['ImageDescription']['x-default'] );
42 * tEXt tags must be encoded iso-8859-1 (vs iTXt which are utf-8)
43 * Make sure non-ascii characters get converted properly
45 function testPngNativeTextNonAscii() {
46 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
47 'Png-native-test.png' );
49 // Note the Copyright symbol here is a utf-8 one
50 // (aka \xC2\xA9) where in the file its iso-8859-1
51 // encoded as just \xA9.
52 $expected = "© 2010 Bawolff";
55 $this->assertArrayHasKey( 'text', $meta );
56 $meta = $meta['text'];
57 $this->assertArrayHasKey( 'Copyright', $meta );
58 $this->assertArrayHasKey( 'x-default', $meta['Copyright'] );
60 $this->assertEquals( $expected, $meta['Copyright']['x-default'] );
64 * Test extraction of pHYs tags, which can tell what the
65 * actual resolution of the image is (aka in dots per meter).
68 function testPngPhysTag () {
69 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
70 'Png-native-test.png' );
72 $this->assertArrayHasKey( 'text', $meta );
73 $meta = $meta['text'];
75 $this->assertEquals( '2835/100', $meta['XResolution'] );
76 $this->assertEquals( '2835/100', $meta['YResolution'] );
77 $this->assertEquals( 3, $meta['ResolutionUnit'] ); // 3 = cm
82 * Given a normal static PNG, check the animation metadata returned.
84 function testStaticPngAnimationMetadata() {
85 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
86 'Png-native-test.png' );
88 $this->assertEquals( 0, $meta['frameCount'] );
89 $this->assertEquals( 1, $meta['loopCount'] );
90 $this->assertEquals( 0, $meta['duration'] );
94 * Given an animated APNG image file
95 * check it gets animated metadata right.
97 function testApngAnimationMetadata() {
98 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
99 'Animated_PNG_example_bouncing_beach_ball.png' );
101 $this->assertEquals( 20, $meta['frameCount'] );
102 // Note loop count of 0 = infinity
103 $this->assertEquals( 0, $meta['loopCount'] );
104 $this->assertEquals( 1.5, $meta['duration'], '', 0.00001 );
107 function testPngBitDepth8() {
108 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
109 'Png-native-test.png' );
111 $this->assertEquals( 8, $meta['bitDepth'] );
113 function testPngBitDepth1() {
114 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
116 $this->assertEquals( 1, $meta['bitDepth'] );
120 function testPngIndexColour() {
121 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
122 'Png-native-test.png' );
124 $this->assertEquals( 'index-coloured', $meta['colorType'] );
126 function testPngRgbColour() {
127 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
129 $this->assertEquals( 'truecolour-alpha', $meta['colorType'] );
131 function testPngRgbNoAlphaColour() {
132 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
134 $this->assertEquals( 'truecolour', $meta['colorType'] );
136 function testPngGreyscaleColour() {
137 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
138 'greyscale-png.png' );
139 $this->assertEquals( 'greyscale-alpha', $meta['colorType'] );
141 function testPngGreyscaleNoAlphaColour() {
142 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
143 'greyscale-na-png.png' );
144 $this->assertEquals( 'greyscale', $meta['colorType'] );