2 class PNGMetadataExtractorTest
extends MediaWikiTestCase
{
5 $this->filePath
= dirname( __FILE__
) . '/../../data/media/';
8 * Tests zTXt tag (compressed textual metadata)
10 function testPngNativetZtxt() {
11 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
12 'Png-native-test.png' );
13 $expected = "foo bar baz foo foo foo foof foo foo foo foo";
14 $this->assertArrayHasKey( 'text', $meta );
15 $meta = $meta['text'];
16 $this->assertArrayHasKey( 'Make', $meta );
17 $this->assertArrayHasKey( 'x-default', $meta['Make'] );
19 $this->assertEquals( $expected, $meta['Make']['x-default'] );
23 * Test tEXt tag (Uncompressed textual metadata)
25 function testPngNativeText() {
26 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
27 'Png-native-test.png' );
28 $expected = "Some long image desc";
29 $this->assertArrayHasKey( 'text', $meta );
30 $meta = $meta['text'];
31 $this->assertArrayHasKey( 'ImageDescription', $meta );
32 $this->assertArrayHasKey( 'x-default', $meta['ImageDescription'] );
33 $this->assertArrayHasKey( '_type', $meta['ImageDescription'] );
35 $this->assertEquals( $expected, $meta['ImageDescription']['x-default'] );
39 * tEXt tags must be encoded iso-8859-1 (vs iTXt which are utf-8)
40 * Make sure non-ascii characters get converted properly
42 function testPngNativeTextNonAscii() {
43 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
44 'Png-native-test.png' );
46 // Note the Copyright symbol here is a utf-8 one
47 // (aka \xC2\xA9) where in the file its iso-8859-1
48 // encoded as just \xA9.
49 $expected = "© 2010 Bawolff";
52 $this->assertArrayHasKey( 'text', $meta );
53 $meta = $meta['text'];
54 $this->assertArrayHasKey( 'Copyright', $meta );
55 $this->assertArrayHasKey( 'x-default', $meta['Copyright'] );
57 $this->assertEquals( $expected, $meta['Copyright']['x-default'] );
61 * Test extraction of pHYs tags, which can tell what the
62 * actual resolution of the image is (aka in dots per meter).
63 function testPngPhysTag () {
64 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
65 'Png-native-test.png' );
67 $this->assertArrayHasKey( 'text', $meta );
68 $meta = $meta['text'];
70 $this->assertEquals( '2835/100', $meta['XResolution'] );
71 $this->assertEquals( '2835/100', $meta['YResolution'] );
72 $this->assertEquals( 3, $meta['ResolutionUnit'] ); // 3 = cm
76 * Given a normal static PNG, check the animation metadata returned.
78 function testStaticPngAnimationMetadata() {
79 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
80 'Png-native-test.png' );
82 $this->assertEquals( 0, $meta['frameCount'] );
83 $this->assertEquals( 1, $meta['loopCount'] );
84 $this->assertEquals( 0, $meta['duration'] );
88 * Given an animated APNG image file
89 * check it gets animated metadata right.
91 function testApngAnimationMetadata() {
92 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
93 'Animated_PNG_example_bouncing_beach_ball.png' );
95 $this->assertEquals( 20, $meta['frameCount'] );
96 // Note loop count of 0 = infinity
97 $this->assertEquals( 0, $meta['loopCount'] );
98 $this->assertEquals( 1.5, $meta['duration'], '', 0.00001 );
101 function testPngBitDepth8() {
102 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
103 'Png-native-test.png' );
105 $this->assertEquals( 8, $meta['bitDepth'] );
107 function testPngBitDepth1() {
108 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
110 $this->assertEquals( 1, $meta['bitDepth'] );
114 function testPngIndexColour() {
115 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
116 'Png-native-test.png' );
118 $this->assertEquals( 'index-coloured', $meta['colorType'] );
120 function testPngRgbColour() {
121 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
123 $this->assertEquals( 'truecolour-alpha', $meta['colorType'] );
125 function testPngRgbNoAlphaColour() {
126 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
128 $this->assertEquals( 'truecolour', $meta['colorType'] );
130 function testPngGreyscaleColour() {
131 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
132 'greyscale-png.png' );
133 $this->assertEquals( 'greyscale-alpha', $meta['colorType'] );
135 function testPngGreyscaleNoAlphaColour() {
136 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
137 'greyscale-na-png.png' );
138 $this->assertEquals( 'greyscale', $meta['colorType'] );