2011-06-13 22:13:38 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-12-03 08:14:32 +00:00
|
|
|
* Tests related to auto rotation.
|
|
|
|
|
*
|
2014-07-15 17:31:07 +00:00
|
|
|
* @group Media
|
2012-12-03 08:14:32 +00:00
|
|
|
* @group medium
|
2013-10-23 22:12:39 +00:00
|
|
|
*
|
2017-12-28 08:22:35 +00:00
|
|
|
* @covers BitmapHandler
|
2011-06-13 22:13:38 +00:00
|
|
|
*/
|
2014-05-26 20:16:07 +00:00
|
|
|
class ExifRotationTest extends MediaWikiMediaTestCase {
|
2011-06-13 22:13:38 +00:00
|
|
|
|
2017-12-28 08:22:35 +00:00
|
|
|
/** @var BitmapHandler */
|
|
|
|
|
private $handler;
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
2011-07-15 15:13:18 +00:00
|
|
|
parent::setUp();
|
2013-11-23 19:20:23 +00:00
|
|
|
$this->checkPHPExtension( 'exif' );
|
2013-10-09 16:46:57 +00:00
|
|
|
|
2011-09-09 20:13:09 +00:00
|
|
|
$this->handler = new BitmapHandler();
|
2011-11-11 04:09:05 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
2013-03-21 19:35:44 +00:00
|
|
|
'wgShowEXIF' => true,
|
|
|
|
|
'wgEnableAutoRotation' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2011-06-13 22:13:38 +00:00
|
|
|
}
|
|
|
|
|
|
2014-05-26 20:16:07 +00:00
|
|
|
/**
|
|
|
|
|
* Mark this test as creating thumbnail files.
|
|
|
|
|
*/
|
|
|
|
|
protected function createsThumbnails() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-13 22:13:38 +00:00
|
|
|
/**
|
2012-10-08 10:56:20 +00:00
|
|
|
* @dataProvider provideFiles
|
2011-06-13 22:13:38 +00:00
|
|
|
*/
|
2013-10-23 22:12:39 +00:00
|
|
|
public function testMetadata( $name, $type, $info ) {
|
2014-07-09 20:03:31 +00:00
|
|
|
if ( !$this->handler->canRotate() ) {
|
2011-11-11 04:09:05 +00:00
|
|
|
$this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
|
|
|
|
|
}
|
2011-12-20 03:52:06 +00:00
|
|
|
$file = $this->dataFile( $name, $type );
|
2011-09-20 20:04:26 +00:00
|
|
|
$this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
|
|
|
|
|
$this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
|
2011-06-13 22:13:38 +00:00
|
|
|
}
|
|
|
|
|
|
2014-07-09 20:03:31 +00:00
|
|
|
/**
|
|
|
|
|
* Same as before, but with auto-rotation set to auto.
|
|
|
|
|
*
|
|
|
|
|
* This sets scaler to image magick, which we should detect as
|
|
|
|
|
* supporting rotation.
|
|
|
|
|
* @dataProvider provideFiles
|
|
|
|
|
*/
|
|
|
|
|
public function testMetadataAutoRotate( $name, $type, $info ) {
|
|
|
|
|
$this->setMwGlobals( 'wgEnableAutoRotation', null );
|
|
|
|
|
$this->setMwGlobals( 'wgUseImageMagick', true );
|
|
|
|
|
$this->setMwGlobals( 'wgUseImageResize', true );
|
|
|
|
|
|
|
|
|
|
$file = $this->dataFile( $name, $type );
|
|
|
|
|
$this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
|
|
|
|
|
$this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-20 19:39:17 +00:00
|
|
|
/**
|
|
|
|
|
*
|
2012-10-08 10:56:20 +00:00
|
|
|
* @dataProvider provideFiles
|
2011-09-20 19:39:17 +00:00
|
|
|
*/
|
2013-10-23 22:12:39 +00:00
|
|
|
public function testRotationRendering( $name, $type, $info, $thumbs ) {
|
2014-07-09 20:03:31 +00:00
|
|
|
if ( !$this->handler->canRotate() ) {
|
2011-11-11 04:09:05 +00:00
|
|
|
$this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
|
|
|
|
|
}
|
2013-02-15 10:17:52 +00:00
|
|
|
foreach ( $thumbs as $size => $out ) {
|
|
|
|
|
if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$params = [
|
2011-09-20 20:04:26 +00:00
|
|
|
'width' => $matches[1],
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-09-20 20:04:26 +00:00
|
|
|
} elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$params = [
|
2011-09-20 20:04:26 +00:00
|
|
|
'width' => $matches[1],
|
|
|
|
|
'height' => $matches[2]
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-09-20 20:04:26 +00:00
|
|
|
} else {
|
2013-02-15 10:17:52 +00:00
|
|
|
throw new MWException( 'bogus test data format ' . $size );
|
2011-09-20 20:04:26 +00:00
|
|
|
}
|
2011-09-20 19:39:17 +00:00
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
$file = $this->dataFile( $name, $type );
|
|
|
|
|
$thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
|
2011-09-20 19:39:17 +00:00
|
|
|
|
2014-04-24 17:51:33 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$out[0],
|
|
|
|
|
$thumb->getWidth(),
|
|
|
|
|
"$name: thumb reported width check for $size"
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$out[1],
|
|
|
|
|
$thumb->getHeight(),
|
|
|
|
|
"$name: thumb reported height check for $size"
|
|
|
|
|
);
|
2011-09-20 20:04:26 +00:00
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
$gis = getimagesize( $thumb->getLocalCopyPath() );
|
2013-02-15 10:17:52 +00:00
|
|
|
if ( $out[0] > $info['width'] ) {
|
2011-09-20 20:04:26 +00:00
|
|
|
// Physical image won't be scaled bigger than the original.
|
2013-02-15 10:17:52 +00:00
|
|
|
$this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
|
|
|
|
|
$this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
|
2011-09-20 20:04:26 +00:00
|
|
|
} else {
|
2013-02-15 10:17:52 +00:00
|
|
|
$this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
|
|
|
|
|
$this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
|
2011-09-20 20:04:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-09-20 19:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function provideFiles() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
2011-06-13 22:13:38 +00:00
|
|
|
'landscape-plain.jpg',
|
|
|
|
|
'image/jpeg',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2011-06-13 22:13:38 +00:00
|
|
|
'width' => 1024,
|
|
|
|
|
'height' => 768,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'800x600px' => [ 800, 600 ],
|
|
|
|
|
'9999x800px' => [ 1067, 800 ],
|
|
|
|
|
'800px' => [ 800, 600 ],
|
|
|
|
|
'600px' => [ 600, 450 ],
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[
|
2011-06-13 22:13:38 +00:00
|
|
|
'portrait-rotated.jpg',
|
|
|
|
|
'image/jpeg',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2011-06-13 22:13:38 +00:00
|
|
|
'width' => 768, // as rotated
|
|
|
|
|
'height' => 1024, // as rotated
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'800x600px' => [ 450, 600 ],
|
|
|
|
|
'9999x800px' => [ 600, 800 ],
|
|
|
|
|
'800px' => [ 800, 1067 ],
|
|
|
|
|
'600px' => [ 600, 800 ],
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
];
|
2011-06-13 22:13:38 +00:00
|
|
|
}
|
2011-11-11 04:09:05 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Same as before, but with auto-rotation disabled.
|
2012-10-08 10:56:20 +00:00
|
|
|
* @dataProvider provideFilesNoAutoRotate
|
2011-11-11 04:09:05 +00:00
|
|
|
*/
|
2013-10-23 22:12:39 +00:00
|
|
|
public function testMetadataNoAutoRotate( $name, $type, $info ) {
|
2013-03-21 19:35:44 +00:00
|
|
|
$this->setMwGlobals( 'wgEnableAutoRotation', false );
|
2011-11-11 04:09:05 +00:00
|
|
|
|
2014-07-09 20:03:31 +00:00
|
|
|
$file = $this->dataFile( $name, $type );
|
|
|
|
|
$this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
|
|
|
|
|
$this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Same as before, but with auto-rotation set to auto and an image scaler that doesn't support it.
|
|
|
|
|
* @dataProvider provideFilesNoAutoRotate
|
|
|
|
|
*/
|
|
|
|
|
public function testMetadataAutoRotateUnsupported( $name, $type, $info ) {
|
|
|
|
|
$this->setMwGlobals( 'wgEnableAutoRotation', null );
|
|
|
|
|
$this->setMwGlobals( 'wgUseImageResize', false );
|
|
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
$file = $this->dataFile( $name, $type );
|
2011-11-11 04:09:05 +00:00
|
|
|
$this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
|
|
|
|
|
$this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
2012-10-08 10:56:20 +00:00
|
|
|
* @dataProvider provideFilesNoAutoRotate
|
2011-11-11 04:09:05 +00:00
|
|
|
*/
|
2013-10-23 22:12:39 +00:00
|
|
|
public function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
|
2013-03-21 19:35:44 +00:00
|
|
|
$this->setMwGlobals( 'wgEnableAutoRotation', false );
|
2011-11-11 04:09:05 +00:00
|
|
|
|
2013-02-15 10:17:52 +00:00
|
|
|
foreach ( $thumbs as $size => $out ) {
|
|
|
|
|
if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$params = [
|
2011-11-11 04:09:05 +00:00
|
|
|
'width' => $matches[1],
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-11-11 04:09:05 +00:00
|
|
|
} elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$params = [
|
2011-11-11 04:09:05 +00:00
|
|
|
'width' => $matches[1],
|
|
|
|
|
'height' => $matches[2]
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-11-11 04:09:05 +00:00
|
|
|
} else {
|
2013-02-15 10:17:52 +00:00
|
|
|
throw new MWException( 'bogus test data format ' . $size );
|
2011-11-11 04:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
$file = $this->dataFile( $name, $type );
|
|
|
|
|
$thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
|
2011-11-11 04:09:05 +00:00
|
|
|
|
2014-04-24 17:51:33 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$out[0],
|
|
|
|
|
$thumb->getWidth(),
|
|
|
|
|
"$name: thumb reported width check for $size"
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$out[1],
|
|
|
|
|
$thumb->getHeight(),
|
|
|
|
|
"$name: thumb reported height check for $size"
|
|
|
|
|
);
|
2011-11-11 04:09:05 +00:00
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
$gis = getimagesize( $thumb->getLocalCopyPath() );
|
2013-02-15 10:17:52 +00:00
|
|
|
if ( $out[0] > $info['width'] ) {
|
2011-11-11 04:09:05 +00:00
|
|
|
// Physical image won't be scaled bigger than the original.
|
2013-02-15 10:17:52 +00:00
|
|
|
$this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
|
|
|
|
|
$this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
|
2011-11-11 04:09:05 +00:00
|
|
|
} else {
|
2013-02-15 10:17:52 +00:00
|
|
|
$this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
|
|
|
|
|
$this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
|
2011-11-11 04:09:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function provideFilesNoAutoRotate() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
2011-11-11 04:09:05 +00:00
|
|
|
'landscape-plain.jpg',
|
|
|
|
|
'image/jpeg',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2011-11-11 04:09:05 +00:00
|
|
|
'width' => 1024,
|
|
|
|
|
'height' => 768,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'800x600px' => [ 800, 600 ],
|
|
|
|
|
'9999x800px' => [ 1067, 800 ],
|
|
|
|
|
'800px' => [ 800, 600 ],
|
|
|
|
|
'600px' => [ 600, 450 ],
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[
|
2011-11-11 04:09:05 +00:00
|
|
|
'portrait-rotated.jpg',
|
|
|
|
|
'image/jpeg',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2011-11-11 04:09:05 +00:00
|
|
|
'width' => 1024, // since not rotated
|
|
|
|
|
'height' => 768, // since not rotated
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'800x600px' => [ 800, 600 ],
|
|
|
|
|
'9999x800px' => [ 1067, 800 ],
|
|
|
|
|
'800px' => [ 800, 600 ],
|
|
|
|
|
'600px' => [ 600, 450 ],
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
];
|
2011-11-11 04:09:05 +00:00
|
|
|
}
|
2013-01-28 10:27:15 +00:00
|
|
|
|
2011-07-15 15:13:18 +00:00
|
|
|
const TEST_WIDTH = 100;
|
|
|
|
|
const TEST_HEIGHT = 200;
|
2013-01-28 10:27:15 +00:00
|
|
|
|
2011-07-15 15:13:18 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideBitmapExtractPreRotationDimensions
|
|
|
|
|
*/
|
2013-10-23 22:12:39 +00:00
|
|
|
public function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$result = $this->handler->extractPreRotationDimensions( [
|
2013-02-15 10:17:52 +00:00
|
|
|
'physicalWidth' => self::TEST_WIDTH,
|
|
|
|
|
'physicalHeight' => self::TEST_HEIGHT,
|
2016-02-17 09:09:32 +00:00
|
|
|
], $rotation );
|
2011-07-15 15:13:18 +00:00
|
|
|
$this->assertEquals( $expected, $result );
|
|
|
|
|
}
|
2013-01-28 10:27:15 +00:00
|
|
|
|
2013-03-22 02:12:37 +00:00
|
|
|
public static function provideBitmapExtractPreRotationDimensions() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
2011-07-15 15:13:18 +00:00
|
|
|
0,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ self::TEST_WIDTH, self::TEST_HEIGHT ]
|
|
|
|
|
],
|
|
|
|
|
[
|
2011-07-15 15:13:18 +00:00
|
|
|
90,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ self::TEST_HEIGHT, self::TEST_WIDTH ]
|
|
|
|
|
],
|
|
|
|
|
[
|
2011-07-15 15:13:18 +00:00
|
|
|
180,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ self::TEST_WIDTH, self::TEST_HEIGHT ]
|
|
|
|
|
],
|
|
|
|
|
[
|
2011-07-15 15:13:18 +00:00
|
|
|
270,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ self::TEST_HEIGHT, self::TEST_WIDTH ]
|
|
|
|
|
],
|
|
|
|
|
];
|
2011-07-15 15:13:18 +00:00
|
|
|
}
|
2011-06-13 22:13:38 +00:00
|
|
|
}
|