4
* Tests related to auto rotation
6
class ExifRotationTest extends MediaWikiTestCase {
8
/** track directories creations. Content will be deleted. */
9
private $createdDirs = array();
13
$this->handler = new BitmapHandler();
14
$filePath = dirname( __FILE__ ) . '/../../data/media';
16
$tmpDir = wfTempDir() . '/exif-test-' . time() . '-' . mt_rand();
17
$this->createdDirs[] = $tmpDir;
19
$this->repo = new FSRepo( array(
21
'url' => 'http://localhost/thumbtest',
22
'backend' => new FSFileBackend( array(
23
'name' => 'localtesting',
24
'lockManager' => 'nullLockManager',
25
'containerPaths' => array( 'temp-thumb' => $tmpDir, 'data' => $filePath )
28
if ( !wfDl( 'exif' ) ) {
29
$this->markTestSkipped( "This test needs the exif extension." );
32
$this->show = $wgShowEXIF;
35
global $wgEnableAutoRotation;
36
$this->oldAuto = $wgEnableAutoRotation;
37
$wgEnableAutoRotation = true;
40
public function tearDown() {
41
global $wgShowEXIF, $wgEnableAutoRotation;
42
$wgShowEXIF = $this->show;
43
$wgEnableAutoRotation = $this->oldAuto;
45
$this->tearDownFiles();
48
private function tearDownFiles() {
49
foreach( $this->createdDirs as $dir ) {
50
wfRecursiveRemoveDir( $dir );
54
function __destruct() {
55
$this->tearDownFiles();
60
* @dataProvider providerFiles
62
function testMetadata( $name, $type, $info ) {
63
if ( !BitmapHandler::canRotate() ) {
64
$this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
66
$file = $this->dataFile( $name, $type );
67
$this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
68
$this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
73
* @dataProvider providerFiles
75
function testRotationRendering( $name, $type, $info, $thumbs ) {
76
if ( !BitmapHandler::canRotate() ) {
77
$this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
79
foreach( $thumbs as $size => $out ) {
80
if( preg_match('/^(\d+)px$/', $size, $matches ) ) {
82
'width' => $matches[1],
84
} elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
86
'width' => $matches[1],
87
'height' => $matches[2]
90
throw new MWException('bogus test data format ' . $size);
93
$file = $this->dataFile( $name, $type );
94
$thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
96
$this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
97
$this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
99
$gis = getimagesize( $thumb->getLocalCopyPath() );
100
if ($out[0] > $info['width']) {
101
// Physical image won't be scaled bigger than the original.
102
$this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size");
103
$this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size");
105
$this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size");
106
$this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size");
111
private function dataFile( $name, $type ) {
112
return new UnregisteredLocalFile( false, $this->repo,
113
"mwstore://localtesting/data/$name", $type );
116
function providerFiles() {
119
'landscape-plain.jpg',
126
'800x600px' => array( 800, 600 ),
127
'9999x800px' => array( 1067, 800 ),
128
'800px' => array( 800, 600 ),
129
'600px' => array( 600, 450 ),
133
'portrait-rotated.jpg',
136
'width' => 768, // as rotated
137
'height' => 1024, // as rotated
140
'800x600px' => array( 450, 600 ),
141
'9999x800px' => array( 600, 800 ),
142
'800px' => array( 800, 1067 ),
143
'600px' => array( 600, 800 ),
150
* Same as before, but with auto-rotation disabled.
151
* @dataProvider providerFilesNoAutoRotate
153
function testMetadataNoAutoRotate( $name, $type, $info ) {
154
global $wgEnableAutoRotation;
155
$wgEnableAutoRotation = false;
157
$file = $this->dataFile( $name, $type );
158
$this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
159
$this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
161
$wgEnableAutoRotation = true;
166
* @dataProvider providerFilesNoAutoRotate
168
function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
169
global $wgEnableAutoRotation;
170
$wgEnableAutoRotation = false;
172
foreach( $thumbs as $size => $out ) {
173
if( preg_match('/^(\d+)px$/', $size, $matches ) ) {
175
'width' => $matches[1],
177
} elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
179
'width' => $matches[1],
180
'height' => $matches[2]
183
throw new MWException('bogus test data format ' . $size);
186
$file = $this->dataFile( $name, $type );
187
$thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
189
$this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
190
$this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
192
$gis = getimagesize( $thumb->getLocalCopyPath() );
193
if ($out[0] > $info['width']) {
194
// Physical image won't be scaled bigger than the original.
195
$this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size");
196
$this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size");
198
$this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size");
199
$this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size");
202
$wgEnableAutoRotation = true;
205
function providerFilesNoAutoRotate() {
208
'landscape-plain.jpg',
215
'800x600px' => array( 800, 600 ),
216
'9999x800px' => array( 1067, 800 ),
217
'800px' => array( 800, 600 ),
218
'600px' => array( 600, 450 ),
222
'portrait-rotated.jpg',
225
'width' => 1024, // since not rotated
226
'height' => 768, // since not rotated
229
'800x600px' => array( 800, 600 ),
230
'9999x800px' => array( 1067, 800 ),
231
'800px' => array( 800, 600 ),
232
'600px' => array( 600, 450 ),
239
const TEST_WIDTH = 100;
240
const TEST_HEIGHT = 200;
243
* @dataProvider provideBitmapExtractPreRotationDimensions
245
function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
246
$result = $this->handler->extractPreRotationDimensions( array(
247
'physicalWidth' => self::TEST_WIDTH,
248
'physicalHeight' => self::TEST_HEIGHT,
250
$this->assertEquals( $expected, $result );
253
function provideBitmapExtractPreRotationDimensions() {
257
array( self::TEST_WIDTH, self::TEST_HEIGHT )
261
array( self::TEST_HEIGHT, self::TEST_WIDTH )
265
array( self::TEST_WIDTH, self::TEST_HEIGHT )
269
array( self::TEST_HEIGHT, self::TEST_WIDTH )