MediaWiki  1.33.0
ExifRotationTest.php
Go to the documentation of this file.
1 <?php
11 
13  private $handler;
14 
15  protected function setUp() {
16  parent::setUp();
17  $this->checkPHPExtension( 'exif' );
18 
19  $this->handler = new BitmapHandler();
20 
21  $this->setMwGlobals( [
22  'wgShowEXIF' => true,
23  'wgEnableAutoRotation' => true,
24  ] );
25  }
26 
30  protected function createsThumbnails() {
31  return true;
32  }
33 
37  public function testMetadata( $name, $type, $info ) {
38  if ( !$this->handler->canRotate() ) {
39  $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
40  }
41  $file = $this->dataFile( $name, $type );
42  $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
43  $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
44  }
45 
53  public function testMetadataAutoRotate( $name, $type, $info ) {
54  $this->setMwGlobals( 'wgEnableAutoRotation', null );
55  $this->setMwGlobals( 'wgUseImageMagick', true );
56  $this->setMwGlobals( 'wgUseImageResize', true );
57 
58  $file = $this->dataFile( $name, $type );
59  $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
60  $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
61  }
62 
67  public function testRotationRendering( $name, $type, $info, $thumbs ) {
68  if ( !$this->handler->canRotate() ) {
69  $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
70  }
71  foreach ( $thumbs as $size => $out ) {
72  if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
73  $params = [
74  'width' => $matches[1],
75  ];
76  } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
77  $params = [
78  'width' => $matches[1],
79  'height' => $matches[2]
80  ];
81  } else {
82  throw new MWException( 'bogus test data format ' . $size );
83  }
84 
85  $file = $this->dataFile( $name, $type );
86  $thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
87 
88  $this->assertEquals(
89  $out[0],
90  $thumb->getWidth(),
91  "$name: thumb reported width check for $size"
92  );
93  $this->assertEquals(
94  $out[1],
95  $thumb->getHeight(),
96  "$name: thumb reported height check for $size"
97  );
98 
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" );
104  } else {
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" );
107  }
108  }
109  }
110 
111  public static function provideFiles() {
112  return [
113  [
114  'landscape-plain.jpg',
115  'image/jpeg',
116  [
117  'width' => 1024,
118  'height' => 768,
119  ],
120  [
121  '800x600px' => [ 800, 600 ],
122  '9999x800px' => [ 1067, 800 ],
123  '800px' => [ 800, 600 ],
124  '600px' => [ 600, 450 ],
125  ]
126  ],
127  [
128  'portrait-rotated.jpg',
129  'image/jpeg',
130  [
131  'width' => 768, // as rotated
132  'height' => 1024, // as rotated
133  ],
134  [
135  '800x600px' => [ 450, 600 ],
136  '9999x800px' => [ 600, 800 ],
137  '800px' => [ 800, 1067 ],
138  '600px' => [ 600, 800 ],
139  ]
140  ]
141  ];
142  }
143 
148  public function testMetadataNoAutoRotate( $name, $type, $info ) {
149  $this->setMwGlobals( 'wgEnableAutoRotation', false );
150 
151  $file = $this->dataFile( $name, $type );
152  $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
153  $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
154  }
155 
160  public function testMetadataAutoRotateUnsupported( $name, $type, $info ) {
161  $this->setMwGlobals( 'wgEnableAutoRotation', null );
162  $this->setMwGlobals( 'wgUseImageResize', false );
163 
164  $file = $this->dataFile( $name, $type );
165  $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
166  $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
167  }
168 
173  public function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
174  $this->setMwGlobals( 'wgEnableAutoRotation', false );
175 
176  foreach ( $thumbs as $size => $out ) {
177  if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
178  $params = [
179  'width' => $matches[1],
180  ];
181  } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
182  $params = [
183  'width' => $matches[1],
184  'height' => $matches[2]
185  ];
186  } else {
187  throw new MWException( 'bogus test data format ' . $size );
188  }
189 
190  $file = $this->dataFile( $name, $type );
191  $thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
192 
193  if ( $thumb->isError() ) {
195  $this->fail( $thumb->toText() );
196  }
197 
198  $this->assertEquals(
199  $out[0],
200  $thumb->getWidth(),
201  "$name: thumb reported width check for $size"
202  );
203  $this->assertEquals(
204  $out[1],
205  $thumb->getHeight(),
206  "$name: thumb reported height check for $size"
207  );
208 
209  $gis = getimagesize( $thumb->getLocalCopyPath() );
210  if ( $out[0] > $info['width'] ) {
211  // Physical image won't be scaled bigger than the original.
212  $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
213  $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
214  } else {
215  $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
216  $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
217  }
218  }
219  }
220 
221  public static function provideFilesNoAutoRotate() {
222  return [
223  [
224  'landscape-plain.jpg',
225  'image/jpeg',
226  [
227  'width' => 1024,
228  'height' => 768,
229  ],
230  [
231  '800x600px' => [ 800, 600 ],
232  '9999x800px' => [ 1067, 800 ],
233  '800px' => [ 800, 600 ],
234  '600px' => [ 600, 450 ],
235  ]
236  ],
237  [
238  'portrait-rotated.jpg',
239  'image/jpeg',
240  [
241  'width' => 1024, // since not rotated
242  'height' => 768, // since not rotated
243  ],
244  [
245  '800x600px' => [ 800, 600 ],
246  '9999x800px' => [ 1067, 800 ],
247  '800px' => [ 800, 600 ],
248  '600px' => [ 600, 450 ],
249  ]
250  ]
251  ];
252  }
253 
254  const TEST_WIDTH = 100;
255  const TEST_HEIGHT = 200;
256 
260  public function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
261  $result = $this->handler->extractPreRotationDimensions( [
262  'physicalWidth' => self::TEST_WIDTH,
263  'physicalHeight' => self::TEST_HEIGHT,
264  ], $rotation );
265  $this->assertEquals( $expected, $result );
266  }
267 
268  public static function provideBitmapExtractPreRotationDimensions() {
269  return [
270  [
271  0,
273  ],
274  [
275  90,
277  ],
278  [
279  180,
281  ],
282  [
283  270,
285  ],
286  ];
287  }
288 }
ExifRotationTest\provideFilesNoAutoRotate
static provideFilesNoAutoRotate()
Definition: ExifRotationTest.php:221
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
ExifRotationTest\createsThumbnails
createsThumbnails()
Mark this test as creating thumbnail files.
Definition: ExifRotationTest.php:30
ExifRotationTest\testMetadataNoAutoRotate
testMetadataNoAutoRotate( $name, $type, $info)
Same as before, but with auto-rotation disabled.
Definition: ExifRotationTest.php:148
ExifRotationTest\TEST_WIDTH
const TEST_WIDTH
Definition: ExifRotationTest.php:254
ExifRotationTest\testMetadataAutoRotateUnsupported
testMetadataAutoRotateUnsupported( $name, $type, $info)
Same as before, but with auto-rotation set to auto and an image scaler that doesn't support it.
Definition: ExifRotationTest.php:160
$result
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page. Return false to stop further processing of the tag $reader:XMLReader object & $pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUnknownUser':When a user doesn 't exist locally, this hook is called to give extensions an opportunity to auto-create it. If the auto-creation is successful, return false. $name:User name 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports. & $fullInterwikiPrefix:Interwiki prefix, may contain colons. & $pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable. Can be used to lazy-load the import sources list. & $importSources:The value of $wgImportSources. Modify as necessary. See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. & $title:Title object for the current page & $request:WebRequest & $ignoreRedirect:boolean to skip redirect check & $target:Title/string of redirect target & $article:Article object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) & $article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() & $ip:IP being check & $result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Array with elements of the form "language:title" in the order that they will be output. & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LanguageSelector':Hook to change the language selector available on a page. $out:The output page. $cssClassName:CSS class name of the language selector. 'LinkBegin':DEPRECATED since 1.28! Use HtmlPageLinkRendererBegin instead. Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1983
$out
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:780
File\RENDER_FORCE
const RENDER_FORCE
Force rendering even if thumbnail already exist and using RENDER_NOW I.e.
Definition: File.php:65
$params
$params
Definition: styleTest.css.php:44
ExifRotationTest\testMetadataAutoRotate
testMetadataAutoRotate( $name, $type, $info)
Same as before, but with auto-rotation set to auto.
Definition: ExifRotationTest.php:53
ExifRotationTest\testBitmapExtractPreRotationDimensions
testBitmapExtractPreRotationDimensions( $rotation, $expected)
provideBitmapExtractPreRotationDimensions
Definition: ExifRotationTest.php:260
MediaWikiMediaTestCase\dataFile
dataFile( $name, $type=false)
Utility function: Get a new file object for a file on disk but not actually in db.
Definition: MediaWikiMediaTestCase.php:76
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ExifRotationTest\testMetadata
testMetadata( $name, $type, $info)
provideFiles
Definition: ExifRotationTest.php:37
MediaWikiMediaTestCase
Specificly for testing Media handlers.
Definition: MediaWikiMediaTestCase.php:5
MWException
MediaWiki exception.
Definition: MWException.php:26
BitmapHandler
Generic handler for bitmap images.
Definition: BitmapHandler.php:31
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:709
$matches
$matches
Definition: NoLocalSettings.php:24
ExifRotationTest\testRotationRendering
testRotationRendering( $name, $type, $info, $thumbs)
provideFiles
Definition: ExifRotationTest.php:67
ExifRotationTest\provideFiles
static provideFiles()
Definition: ExifRotationTest.php:111
handler
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves set to a MediaTransformOutput the error message to be returned in an array you should do so by altering $wgNamespaceProtection and $wgNamespaceContentModels outside the handler
Definition: hooks.txt:780
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
ExifRotationTest\testRotationRenderingNoAutoRotate
testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs)
provideFilesNoAutoRotate
Definition: ExifRotationTest.php:173
ExifRotationTest\setUp
setUp()
Definition: ExifRotationTest.php:15
ExifRotationTest
Tests related to auto rotation.
Definition: ExifRotationTest.php:10
ExifRotationTest\TEST_HEIGHT
const TEST_HEIGHT
Definition: ExifRotationTest.php:255
File\RENDER_NOW
const RENDER_NOW
Force rendering in the current process.
Definition: File.php:60
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
MediaWikiTestCase\checkPHPExtension
checkPHPExtension( $extName)
Check if $extName is a loaded PHP extension, will skip the test whenever it is not loaded.
Definition: MediaWikiTestCase.php:2288
ExifRotationTest\provideBitmapExtractPreRotationDimensions
static provideBitmapExtractPreRotationDimensions()
Definition: ExifRotationTest.php:268
ExifRotationTest\$handler
BitmapHandler $handler
Definition: ExifRotationTest.php:13
$type
$type
Definition: testCompression.php:48