| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\MainConfigNames; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class ExifRotationTest extends MediaWikiMediaTestCase { |
| 15 |
|
| 16 |
|
| 17 |
private $handler; |
| 18 |
|
| 19 |
protected function setUp(): void { |
| 20 |
parent::setUp(); |
| 21 |
|
| 22 |
$this->handler = new BitmapHandler(); |
| 23 |
|
| 24 |
$this->overrideConfigValues( [ |
| 25 |
MainConfigNames::ShowEXIF => true, |
| 26 |
MainConfigNames::EnableAutoRotation => true, |
| 27 |
] ); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
protected function createsThumbnails() { |
| 35 |
return true; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public function testMetadata( $name, $type, $info ) { |
| 42 |
if ( !$this->handler->canRotate() ) { |
| 43 |
$this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." ); |
| 44 |
} |
| 45 |
$file = $this->dataFile( $name, $type ); |
| 46 |
$this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" ); |
| 47 |
$this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" ); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function testMetadataAutoRotate( $name, $type, $info ) { |
| 58 |
$this->overrideConfigValues( [ |
| 59 |
MainConfigNames::EnableAutoRotation => null, |
| 60 |
MainConfigNames::UseImageMagick => true, |
| 61 |
MainConfigNames::UseImageResize => true, |
| 62 |
] ); |
| 63 |
|
| 64 |
$file = $this->dataFile( $name, $type ); |
| 65 |
$this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" ); |
| 66 |
$this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" ); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function testRotationRendering( $name, $type, $info, $thumbs ) { |
| 73 |
if ( !$this->handler->canRotate() ) { |
| 74 |
$this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." ); |
| 75 |
} |
| 76 |
foreach ( $thumbs as $size => $out ) { |
| 77 |
if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) { |
| 78 |
$params = [ |
| 79 |
'width' => $matches[1], |
| 80 |
]; |
| 81 |
} elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) { |
| 82 |
$params = [ |
| 83 |
'width' => $matches[1], |
| 84 |
'height' => $matches[2] |
| 85 |
]; |
| 86 |
} else { |
| 87 |
throw new InvalidArgumentException( 'bogus test data format ' . $size ); |
| 88 |
} |
| 89 |
|
| 90 |
$file = $this->dataFile( $name, $type ); |
| 91 |
$thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE ); |
| 92 |
|
| 93 |
$this->assertEquals( |
| 94 |
$out[0], |
| 95 |
$thumb->getWidth(), |
| 96 |
"$name: thumb reported width check for $size" |
| 97 |
); |
| 98 |
$this->assertEquals( |
| 99 |
$out[1], |
| 100 |
$thumb->getHeight(), |
| 101 |
"$name: thumb reported height check for $size" |
| 102 |
); |
| 103 |
|
| 104 |
$gis = getimagesize( $thumb->getLocalCopyPath() ); |
| 105 |
if ( $out[0] > $info['width'] ) { |
| 106 |
|
| 107 |
$this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" ); |
| 108 |
$this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" ); |
| 109 |
} else { |
| 110 |
$this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" ); |
| 111 |
$this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" ); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
public static function provideFiles() { |
| 117 |
return [ |
| 118 |
[ |
| 119 |
'landscape-plain.jpg', |
| 120 |
'image/jpeg', |
| 121 |
[ |
| 122 |
'width' => 160, |
| 123 |
'height' => 120, |
| 124 |
], |
| 125 |
[ |
| 126 |
'80x60px' => [ 80, 60 ], |
| 127 |
'9999x80px' => [ 107, 80 ], |
| 128 |
'80px' => [ 80, 60 ], |
| 129 |
'60px' => [ 60, 45 ], |
| 130 |
] |
| 131 |
], |
| 132 |
[ |
| 133 |
'portrait-rotated.jpg', |
| 134 |
'image/jpeg', |
| 135 |
[ |
| 136 |
'width' => 120, |
| 137 |
'height' => 160, |
| 138 |
], |
| 139 |
[ |
| 140 |
'80x60px' => [ 45, 60 ], |
| 141 |
'9999x80px' => [ 60, 80 ], |
| 142 |
'80px' => [ 80, 107 ], |
| 143 |
'60px' => [ 60, 80 ], |
| 144 |
] |
| 145 |
] |
| 146 |
]; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function testMetadataNoAutoRotate( $name, $type, $info ) { |
| 154 |
$this->overrideConfigValue( MainConfigNames::EnableAutoRotation, false ); |
| 155 |
|
| 156 |
$file = $this->dataFile( $name, $type ); |
| 157 |
$this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" ); |
| 158 |
$this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" ); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public function testMetadataAutoRotateUnsupported( $name, $type, $info ) { |
| 166 |
$this->overrideConfigValues( [ |
| 167 |
MainConfigNames::EnableAutoRotation => null, |
| 168 |
MainConfigNames::UseImageResize => false, |
| 169 |
] ); |
| 170 |
|
| 171 |
$file = $this->dataFile( $name, $type ); |
| 172 |
$this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" ); |
| 173 |
$this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" ); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) { |
| 180 |
$this->overrideConfigValue( MainConfigNames::EnableAutoRotation, false ); |
| 181 |
|
| 182 |
foreach ( $thumbs as $size => $out ) { |
| 183 |
if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) { |
| 184 |
$params = [ |
| 185 |
'width' => $matches[1], |
| 186 |
]; |
| 187 |
} elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) { |
| 188 |
$params = [ |
| 189 |
'width' => $matches[1], |
| 190 |
'height' => $matches[2] |
| 191 |
]; |
| 192 |
} else { |
| 193 |
throw new InvalidArgumentException( 'bogus test data format ' . $size ); |
| 194 |
} |
| 195 |
|
| 196 |
$file = $this->dataFile( $name, $type ); |
| 197 |
$thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE ); |
| 198 |
|
| 199 |
if ( $thumb->isError() ) { |
| 200 |
|
| 201 |
$this->fail( $thumb->toText() ); |
| 202 |
} |
| 203 |
|
| 204 |
$this->assertEquals( |
| 205 |
$out[0], |
| 206 |
$thumb->getWidth(), |
| 207 |
"$name: thumb reported width check for $size" |
| 208 |
); |
| 209 |
$this->assertEquals( |
| 210 |
$out[1], |
| 211 |
$thumb->getHeight(), |
| 212 |
"$name: thumb reported height check for $size" |
| 213 |
); |
| 214 |
|
| 215 |
$gis = getimagesize( $thumb->getLocalCopyPath() ); |
| 216 |
if ( $out[0] > $info['width'] ) { |
| 217 |
|
| 218 |
$this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" ); |
| 219 |
$this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" ); |
| 220 |
} else { |
| 221 |
$this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" ); |
| 222 |
$this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" ); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
public static function provideFilesNoAutoRotate() { |
| 228 |
return [ |
| 229 |
[ |
| 230 |
'landscape-plain.jpg', |
| 231 |
'image/jpeg', |
| 232 |
[ |
| 233 |
'width' => 160, |
| 234 |
'height' => 120, |
| 235 |
], |
| 236 |
[ |
| 237 |
'80x60px' => [ 80, 60 ], |
| 238 |
'9999x80px' => [ 107, 80 ], |
| 239 |
'80px' => [ 80, 60 ], |
| 240 |
'60px' => [ 60, 45 ], |
| 241 |
] |
| 242 |
], |
| 243 |
[ |
| 244 |
'portrait-rotated.jpg', |
| 245 |
'image/jpeg', |
| 246 |
[ |
| 247 |
'width' => 160, |
| 248 |
'height' => 120, |
| 249 |
], |
| 250 |
[ |
| 251 |
'80x60px' => [ 80, 60 ], |
| 252 |
'9999x80px' => [ 107, 80 ], |
| 253 |
'80px' => [ 80, 60 ], |
| 254 |
'60px' => [ 60, 45 ], |
| 255 |
] |
| 256 |
] |
| 257 |
]; |
| 258 |
} |
| 259 |
|
| 260 |
private const TEST_WIDTH = 100; |
| 261 |
private const TEST_HEIGHT = 200; |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public function testBitmapExtractPreRotationDimensions( $rotation, $expected ) { |
| 267 |
$result = $this->handler->extractPreRotationDimensions( [ |
| 268 |
'physicalWidth' => self::TEST_WIDTH, |
| 269 |
'physicalHeight' => self::TEST_HEIGHT, |
| 270 |
], $rotation ); |
| 271 |
$this->assertEquals( $expected, $result ); |
| 272 |
} |
| 273 |
|
| 274 |
public static function provideBitmapExtractPreRotationDimensions() { |
| 275 |
return [ |
| 276 |
[ |
| 277 |
0, |
| 278 |
[ self::TEST_WIDTH, self::TEST_HEIGHT ] |
| 279 |
], |
| 280 |
[ |
| 281 |
90, |
| 282 |
[ self::TEST_HEIGHT, self::TEST_WIDTH ] |
| 283 |
], |
| 284 |
[ |
| 285 |
180, |
| 286 |
[ self::TEST_WIDTH, self::TEST_HEIGHT ] |
| 287 |
], |
| 288 |
[ |
| 289 |
270, |
| 290 |
[ self::TEST_HEIGHT, self::TEST_WIDTH ] |
| 291 |
], |
| 292 |
]; |
| 293 |
} |
| 294 |
} |