2 class PNGMetadataExtractorTest
extends MediaWikiTestCase
{
4 * Tests zTXt tag (compressed textual metadata)
6 function testPNGNativetzTXt() {
7 $meta = PNGMetadataExtractor
::getMetadata( dirname( __FILE__
) .
8 '/Png-native-test.png' );
9 $expected = "foo bar baz foo foo foo foof foo foo foo foo";
10 $this->assertArrayHasKey( 'text', $meta );
11 $meta = $meta['text'];
12 $this->assertArrayHasKey( 'Make', $meta );
13 $this->assertArrayHasKey( 'x-default', $meta['Make'] );
15 $this->assertEquals( $expected, $meta['Make']['x-default'] );
19 * Test tEXt tag (Uncompressed textual metadata)
21 function testPNGNativetEXt() {
22 $meta = PNGMetadataExtractor
::getMetadata( dirname( __FILE__
) .
23 '/Png-native-test.png' );
24 $expected = "Some long image desc";
25 $this->assertArrayHasKey( 'text', $meta );
26 $meta = $meta['text'];
27 $this->assertArrayHasKey( 'ImageDescription', $meta );
28 $this->assertArrayHasKey( 'x-default', $meta['ImageDescription'] );
29 $this->assertArrayHasKey( '_type', $meta['ImageDescription'] );
31 $this->assertEquals( $expected, $meta['ImageDescription']['x-default'] );
35 * tEXt tags must be encoded iso-8859-1 (vs iTXt which are utf-8)
36 * Make sure non-ascii characters get converted properly
38 function testPNGNativettEXtNonASCII() {
39 $meta = PNGMetadataExtractor
::getMetadata( dirname( __FILE__
) .
40 '/Png-native-test.png' );
42 // Note the Copyright symbol here is a utf-8 one
43 // (aka \xC2\xA9) where in the file its iso-8859-1
44 // encoded as just \xA9.
45 $expected = "© 2010 Bawolff";
48 $this->assertArrayHasKey( 'text', $meta );
49 $meta = $meta['text'];
50 $this->assertArrayHasKey( 'Copyright', $meta );
51 $this->assertArrayHasKey( 'x-default', $meta['Copyright'] );
53 $this->assertEquals( $expected, $meta['Copyright']['x-default'] );
57 * Test extraction of pHYs tags, which can tell what the
58 * actual resolution of the image is (aka in dots per meter).
59 function testPNGpHYsTag () {
60 $meta = PNGMetadataExtractor::getMetadata( dirname( __FILE__ ) .
61 '/Png-native-test.png' );
63 $this->assertArrayHasKey( 'text', $meta );
64 $meta = $meta['text'];
66 $this->assertEquals( '2835/100', $meta['XResolution'] );
67 $this->assertEquals( '2835/100', $meta['YResolution'] );
68 $this->assertEquals( 3, $meta['ResolutionUnit'] ); // 3 = cm
72 * Given a normal static PNG, check the animation metadata returned.
74 function testStaticPNGAnimationMetadata() {
75 $meta = PNGMetadataExtractor
::getMetadata( dirname( __FILE__
) .
76 '/Png-native-test.png' );
78 $this->assertEquals( 0, $meta['frameCount'] );
79 $this->assertEquals( 1, $meta['loopCount'] );
80 $this->assertEquals( 0, $meta['duration'] );
84 * Given an animated APNG image file
85 * check it gets animated metadata right.
87 function testAPNGAnimationMetadata() {
88 $meta = PNGMetadataExtractor
::getMetadata( dirname( __FILE__
) .
89 '/Animated_PNG_example_bouncing_beach_ball.png' );
91 $this->assertEquals( 20, $meta['frameCount'] );
92 // Note loop count of 0 = infinity
93 $this->assertEquals( 0, $meta['loopCount'] );
94 $this->assertEquals( 1.5, $meta['duration'], '', 0.00001 );
97 function testPNGBitDepth8() {
98 $meta = PNGMetadataExtractor
::getMetadata( dirname( __FILE__
) .
99 '/Png-native-test.png' );
101 $this->assertEquals( 8, $meta['bitDepth'] );
103 function testPNGBitDepth1() {
104 $meta = PNGMetadataExtractor
::getMetadata( dirname( __FILE__
) .
106 $this->assertEquals( 1, $meta['bitDepth'] );
110 function testPNGindexColour() {
111 $meta = PNGMetadataExtractor
::getMetadata( dirname( __FILE__
) .
112 '/Png-native-test.png' );
114 $this->assertEquals( 'index-coloured', $meta['colorType'] );
116 function testPNGrgbColour() {
117 $meta = PNGMetadataExtractor
::getMetadata( dirname( __FILE__
) .
119 $this->assertEquals( 'truecolour-alpha', $meta['colorType'] );
121 function testPNGrgbNoAlphaColour() {
122 $meta = PNGMetadataExtractor
::getMetadata( dirname( __FILE__
) .
124 $this->assertEquals( 'truecolour', $meta['colorType'] );
126 function testPNGgreyscaleColour() {
127 $meta = PNGMetadataExtractor
::getMetadata( dirname( __FILE__
) .
128 '/greyscale-png.png' );
129 $this->assertEquals( 'greyscale-alpha', $meta['colorType'] );
131 function testPNGgreyscaleNoAlphaColour() {
132 $meta = PNGMetadataExtractor
::getMetadata( dirname( __FILE__
) .
133 '/greyscale-na-png.png' );
134 $this->assertEquals( 'greyscale', $meta['colorType'] );