2 class PNGMetadataExtractorTest
extends MediaWikiTestCase
{
4 protected function setUp() {
6 $this->filePath
= __DIR__
. '/../../data/media/';
10 * Tests zTXt tag (compressed textual metadata)
12 function testPngNativetZtxt() {
13 $this->checkPHPExtension( 'zlib' );
15 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
16 'Png-native-test.png' );
17 $expected = "foo bar baz foo foo foo foof foo foo foo foo";
18 $this->assertArrayHasKey( 'text', $meta );
19 $meta = $meta['text'];
20 $this->assertArrayHasKey( 'Make', $meta );
21 $this->assertArrayHasKey( 'x-default', $meta['Make'] );
23 $this->assertEquals( $expected, $meta['Make']['x-default'] );
27 * Test tEXt tag (Uncompressed textual metadata)
29 function testPngNativeText() {
30 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
31 'Png-native-test.png' );
32 $expected = "Some long image desc";
33 $this->assertArrayHasKey( 'text', $meta );
34 $meta = $meta['text'];
35 $this->assertArrayHasKey( 'ImageDescription', $meta );
36 $this->assertArrayHasKey( 'x-default', $meta['ImageDescription'] );
37 $this->assertArrayHasKey( '_type', $meta['ImageDescription'] );
39 $this->assertEquals( $expected, $meta['ImageDescription']['x-default'] );
43 * tEXt tags must be encoded iso-8859-1 (vs iTXt which are utf-8)
44 * Make sure non-ascii characters get converted properly
46 function testPngNativeTextNonAscii() {
47 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
48 'Png-native-test.png' );
50 // Note the Copyright symbol here is a utf-8 one
51 // (aka \xC2\xA9) where in the file its iso-8859-1
52 // encoded as just \xA9.
53 $expected = "© 2010 Bawolff";
56 $this->assertArrayHasKey( 'text', $meta );
57 $meta = $meta['text'];
58 $this->assertArrayHasKey( 'Copyright', $meta );
59 $this->assertArrayHasKey( 'x-default', $meta['Copyright'] );
61 $this->assertEquals( $expected, $meta['Copyright']['x-default'] );
65 * Test extraction of pHYs tags, which can tell what the
66 * actual resolution of the image is (aka in dots per meter).
69 function testPngPhysTag () {
70 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
71 'Png-native-test.png' );
73 $this->assertArrayHasKey( 'text', $meta );
74 $meta = $meta['text'];
76 $this->assertEquals( '2835/100', $meta['XResolution'] );
77 $this->assertEquals( '2835/100', $meta['YResolution'] );
78 $this->assertEquals( 3, $meta['ResolutionUnit'] ); // 3 = cm
83 * Given a normal static PNG, check the animation metadata returned.
85 function testStaticPngAnimationMetadata() {
86 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
87 'Png-native-test.png' );
89 $this->assertEquals( 0, $meta['frameCount'] );
90 $this->assertEquals( 1, $meta['loopCount'] );
91 $this->assertEquals( 0, $meta['duration'] );
95 * Given an animated APNG image file
96 * check it gets animated metadata right.
98 function testApngAnimationMetadata() {
99 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
100 'Animated_PNG_example_bouncing_beach_ball.png' );
102 $this->assertEquals( 20, $meta['frameCount'] );
103 // Note loop count of 0 = infinity
104 $this->assertEquals( 0, $meta['loopCount'] );
105 $this->assertEquals( 1.5, $meta['duration'], '', 0.00001 );
108 function testPngBitDepth8() {
109 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
110 'Png-native-test.png' );
112 $this->assertEquals( 8, $meta['bitDepth'] );
115 function testPngBitDepth1() {
116 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
118 $this->assertEquals( 1, $meta['bitDepth'] );
122 function testPngIndexColour() {
123 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
124 'Png-native-test.png' );
126 $this->assertEquals( 'index-coloured', $meta['colorType'] );
129 function testPngRgbColour() {
130 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
132 $this->assertEquals( 'truecolour-alpha', $meta['colorType'] );
135 function testPngRgbNoAlphaColour() {
136 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
138 $this->assertEquals( 'truecolour', $meta['colorType'] );
141 function testPngGreyscaleColour() {
142 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
143 'greyscale-png.png' );
144 $this->assertEquals( 'greyscale-alpha', $meta['colorType'] );
147 function testPngGreyscaleNoAlphaColour() {
148 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
149 'greyscale-na-png.png' );
150 $this->assertEquals( 'greyscale', $meta['colorType'] );