JpegTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * @group Media
  4. * @covers JpegHandler
  5. */
  6. class JpegTest extends MediaWikiMediaTestCase {
  7. protected function setUp() {
  8. parent::setUp();
  9. $this->checkPHPExtension( 'exif' );
  10. $this->setMwGlobals( 'wgShowEXIF', true );
  11. $this->handler = new JpegHandler;
  12. }
  13. public function testInvalidFile() {
  14. $file = $this->dataFile( 'README', 'image/jpeg' );
  15. $res = $this->handler->getMetadata( $file, $this->filePath . 'README' );
  16. $this->assertEquals( ExifBitmapHandler::BROKEN_FILE, $res );
  17. }
  18. public function testJpegMetadataExtraction() {
  19. $file = $this->dataFile( 'test.jpg', 'image/jpeg' );
  20. $res = $this->handler->getMetadata( $file, $this->filePath . 'test.jpg' );
  21. // phpcs:ignore Generic.Files.LineLength
  22. $expected = 'a:7:{s:16:"ImageDescription";s:9:"Test file";s:11:"XResolution";s:4:"72/1";s:11:"YResolution";s:4:"72/1";s:14:"ResolutionUnit";i:2;s:16:"YCbCrPositioning";i:1;s:15:"JPEGFileComment";a:1:{i:0;s:17:"Created with GIMP";}s:22:"MEDIAWIKI_EXIF_VERSION";i:2;}';
  23. // Unserialize in case serialization format ever changes.
  24. $this->assertEquals( unserialize( $expected ), unserialize( $res ) );
  25. }
  26. /**
  27. * @covers JpegHandler::getCommonMetaArray
  28. */
  29. public function testGetIndependentMetaArray() {
  30. $file = $this->dataFile( 'test.jpg', 'image/jpeg' );
  31. $res = $this->handler->getCommonMetaArray( $file );
  32. $expected = [
  33. 'ImageDescription' => 'Test file',
  34. 'XResolution' => '72/1',
  35. 'YResolution' => '72/1',
  36. 'ResolutionUnit' => 2,
  37. 'YCbCrPositioning' => 1,
  38. 'JPEGFileComment' => [
  39. 'Created with GIMP',
  40. ],
  41. ];
  42. $this->assertEquals( $res, $expected );
  43. }
  44. /**
  45. * @dataProvider provideSwappingICCProfile
  46. * @covers JpegHandler::swapICCProfile
  47. */
  48. public function testSwappingICCProfile(
  49. $sourceFilename, $controlFilename, $newProfileFilename, $oldProfileName
  50. ) {
  51. global $wgExiftool;
  52. if ( !$wgExiftool || !is_file( $wgExiftool ) ) {
  53. $this->markTestSkipped( "Exiftool not installed, cannot test ICC profile swapping" );
  54. }
  55. $this->setMwGlobals( 'wgUseTinyRGBForJPGThumbnails', true );
  56. $sourceFilepath = $this->filePath . $sourceFilename;
  57. $controlFilepath = $this->filePath . $controlFilename;
  58. $profileFilepath = $this->filePath . $newProfileFilename;
  59. $filepath = $this->getNewTempFile();
  60. copy( $sourceFilepath, $filepath );
  61. $file = $this->dataFile( $sourceFilename, 'image/jpeg' );
  62. $this->handler->swapICCProfile(
  63. $filepath,
  64. [ 'sRGB', '-' ],
  65. [ $oldProfileName ],
  66. $profileFilepath
  67. );
  68. $this->assertEquals(
  69. sha1( file_get_contents( $filepath ) ),
  70. sha1( file_get_contents( $controlFilepath ) )
  71. );
  72. }
  73. public function provideSwappingICCProfile() {
  74. return [
  75. // File with sRGB should end up with TinyRGB
  76. [
  77. 'srgb.jpg',
  78. 'tinyrgb.jpg',
  79. 'tinyrgb.icc',
  80. 'sRGB IEC61966-2.1'
  81. ],
  82. // File with TinyRGB should be left unchanged
  83. [
  84. 'tinyrgb.jpg',
  85. 'tinyrgb.jpg',
  86. 'tinyrgb.icc',
  87. 'sRGB IEC61966-2.1'
  88. ],
  89. // File without profile should end up with TinyRGB
  90. [
  91. 'missingprofile.jpg',
  92. 'tinyrgb.jpg',
  93. 'tinyrgb.icc',
  94. 'sRGB IEC61966-2.1'
  95. ],
  96. // Non-sRGB file should be left untouched
  97. [
  98. 'adobergb.jpg',
  99. 'adobergb.jpg',
  100. 'tinyrgb.icc',
  101. 'sRGB IEC61966-2.1'
  102. ]
  103. ];
  104. }
  105. }