2 class PNGMetadataExtractorTest
extends MediaWikiTestCase
{
4 protected function setUp() {
5 $this->filePath
= __DIR__
. '/../../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).
65 function testPngPhysTag () {
66 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
67 'Png-native-test.png' );
69 $this->assertArrayHasKey( 'text', $meta );
70 $meta = $meta['text'];
72 $this->assertEquals( '2835/100', $meta['XResolution'] );
73 $this->assertEquals( '2835/100', $meta['YResolution'] );
74 $this->assertEquals( 3, $meta['ResolutionUnit'] ); // 3 = cm
79 * Given a normal static PNG, check the animation metadata returned.
81 function testStaticPngAnimationMetadata() {
82 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
83 'Png-native-test.png' );
85 $this->assertEquals( 0, $meta['frameCount'] );
86 $this->assertEquals( 1, $meta['loopCount'] );
87 $this->assertEquals( 0, $meta['duration'] );
91 * Given an animated APNG image file
92 * check it gets animated metadata right.
94 function testApngAnimationMetadata() {
95 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
96 'Animated_PNG_example_bouncing_beach_ball.png' );
98 $this->assertEquals( 20, $meta['frameCount'] );
99 // Note loop count of 0 = infinity
100 $this->assertEquals( 0, $meta['loopCount'] );
101 $this->assertEquals( 1.5, $meta['duration'], '', 0.00001 );
104 function testPngBitDepth8() {
105 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
106 'Png-native-test.png' );
108 $this->assertEquals( 8, $meta['bitDepth'] );
110 function testPngBitDepth1() {
111 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
113 $this->assertEquals( 1, $meta['bitDepth'] );
117 function testPngIndexColour() {
118 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
119 'Png-native-test.png' );
121 $this->assertEquals( 'index-coloured', $meta['colorType'] );
123 function testPngRgbColour() {
124 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
126 $this->assertEquals( 'truecolour-alpha', $meta['colorType'] );
128 function testPngRgbNoAlphaColour() {
129 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
131 $this->assertEquals( 'truecolour', $meta['colorType'] );
133 function testPngGreyscaleColour() {
134 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
135 'greyscale-png.png' );
136 $this->assertEquals( 'greyscale-alpha', $meta['colorType'] );
138 function testPngGreyscaleNoAlphaColour() {
139 $meta = PNGMetadataExtractor
::getMetadata( $this->filePath
.
140 'greyscale-na-png.png' );
141 $this->assertEquals( 'greyscale', $meta['colorType'] );