$metadata = MediaWiki\quietCall( 'unserialize', $metadata );
}
- return $handler->getContentHeaders( $metadata );
+ return $handler->getContentHeaders( $metadata, $this->getWidth(), $this->getHeight() );
}
}
if ( $handler ) {
$metadata = MediaWiki\quietCall( 'unserialize', $props['metadata'] );
- $options['headers'] = $handler->getContentHeaders( $metadata );
+ $options['headers'] = $handler->getContentHeaders(
+ $metadata, $props['width'], $props['height']
+ );
} else {
$options['headers'] = [];
}
/**
* Get useful response headers for GET/HEAD requests for a file with the given metadata
* @param $metadata Array Contains this handler's unserialized getMetadata() for a file
- * @return array
+ * @param $fallbackWidth int|null Width to fall back to if metadata doesn't have any
+ * @param $fallbackHeight int|null Height to fall back to if metadata doesn't have any
+ * @return Array
+ * @since 1.30
*/
- public function getContentHeaders( $metadata ) {
+ public function getContentHeaders( $metadata, $fallbackWidth = null, $fallbackHeight = null ) {
if ( !is_array( $metadata ) || !isset( $metadata['xml'] ) ) {
return [];
}
* @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
*/
$this->mExifTags = [
- 'COMPUTED' => [
- 'Width' => Exif::SHORT_OR_LONG, # Image width
- 'Height' => Exif::SHORT_OR_LONG, # Image height
- ],
-
# TIFF Rev. 6.0 Attribute Information (p22)
'IFD0' => [
# Tags relating to image structure
return 0;
}
-
- /**
- * Get useful response headers for GET/HEAD requests for a file with the given metadata
- * @param $metadata Array Contains this handler's unserialized getMetadata() for a file
- * @return Array
- */
- public function getContentHeaders( $metadata ) {
- if ( !isset( $metadata['Width'] ) || !isset( $metadata['Height'] ) ) {
- return [];
- }
-
- $dimensionsMetadata = [];
-
- if ( $this->autoRotateEnabled() && isset( $metadata['Orientation'] ) ) {
- switch ( $metadata['Orientation'] ) {
- case 5: // CCW flipped
- case 6: // CCW
- case 7: // CW flipped
- case 8: // CW
- $dimensionsMetadata['width'] = $metadata['Height'];
- $dimensionsMetadata['height'] = $metadata['Width'];
- break;
- }
- }
-
- if ( !isset( $dimensionsMetadata['width'] ) ) {
- $dimensionsMetadata['width'] = $metadata['Width'];
- $dimensionsMetadata['height'] = $metadata['Height'];
- }
-
- return parent::getContentHeaders( $dimensionsMetadata );
- }
}
public function makeFormattedData( $tags ) {
$resolutionunit = !isset( $tags['ResolutionUnit'] ) || $tags['ResolutionUnit'] == 2 ? 2 : 3;
unset( $tags['ResolutionUnit'] );
- // Width and height are for internal use and already available & displayed outside of metadata
- unset( $tags['Width'] );
- unset( $tags['Height'] );
foreach ( $tags as $tag => &$vals ) {
// This seems ugly to wrap non-array's in an array just to unwrap again,
'duration' => $duration,
'xmp' => $xmp,
'comment' => $comment,
- 'width' => $width,
- 'height' => $height,
];
}
/**
* Get useful response headers for GET/HEAD requests for a file with the given metadata
* @param $metadata Array Contains this handler's unserialized getMetadata() for a file
+ * @param $fallbackWidth int|null Width to fall back to if metadata doesn't have any
+ * @param $fallbackHeight int|null Height to fall back to if metadata doesn't have any
* @return Array
* @since 1.30
*/
- public function getContentHeaders( $metadata ) {
- if ( !isset( $metadata['width'] ) || !isset( $metadata['height'] ) ) {
- return [];
+ public function getContentHeaders( $metadata, $fallbackWidth = null, $fallbackHeight = null ) {
+ if ( !isset( $metadata['width'] ) ) {
+ if ( is_null( $fallbackWidth ) ) {
+ return [];
+ }
+
+ $metadata['width'] = $fallbackWidth;
+ }
+
+ if ( !isset( $metadata['height'] ) ) {
+ if ( is_null( $fallbackHeight ) ) {
+ return [];
+ }
+
+ $metadata['height'] = $fallbackHeight;
}
$dimensionString = $metadata['width'] . 'x' . $metadata['height'];
'text' => $text,
'bitDepth' => $bitDepth,
'colorType' => $colorType,
- 'width' => $width,
- 'height' => $height,
];
}
$metadata['colorType'] = 'unknown';
}
-
- $metadata['width'] = $header['width'];
- $metadata['height'] = $header['height'];
} else {
// Marker to prevent repeated attempted extraction
$metadata['error'] = true;
if ( $handler ) {
$metadata = MediaWiki\quietCall( 'unserialize', $props['metadata'] );
- $publishOptions['headers'] = $handler->getContentHeaders( $metadata );
+ $publishOptions['headers'] = $handler->getContentHeaders(
+ $metadata, $props['width'], $props['height']
+ );
} else {
$publishOptions['headers'] = [];
}
'SerialNumber' => '123456789',
'_MW_PNG_VERSION' => 1,
],
- 'width' => 50,
- 'height' => 50,
];
$this->assertEquals( $expected, $result );
}
'GPSAltitude' => -3.141592653,
'GPSDOP' => '5/1',
'GPSVersionID' => '2.2.0.0',
- 'Height' => 10,
- 'Width' => 40,
];
$this->assertEquals( $expected, $data, '', 0.0000000001 );
}
$expected = [
'UserComment' => 'test⁔comment',
- 'Height' => 10,
- 'Width' => 40,
];
$this->assertEquals( $expected, $data );
}
'frameCount' => 1,
'looped' => false,
'xmp' => '',
- 'width' => 45,
- 'height' => 30,
]
],
[
'frameCount' => 4,
'looped' => true,
'xmp' => '',
- 'width' => 45,
- 'height' => 30,
]
],
'frameCount' => 4,
'looped' => true,
'comment' => [ 'GIƒ·test·file' ],
- 'width' => 45,
- 'height' => 30,
]
],
];
[ 'Something invalid!', GIFHandler::METADATA_BAD ],
// @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
[
- 'a:6:{s:10:"frameCount";i:1;s:6:"looped";b:0;s:8:"duration";d:0.1000000000000000055511151231257827021181583404541015625;s:8:"metadata";a:2:{s:14:"GIFFileComment";a:1:{i:0;s:35:"GIF test file ⁕ Created with GIMP";}s:15:"_MW_GIF_VERSION";i:1;}s:5:"width";i:45;s:6:"height";i:30;}',
+ 'a:4:{s:10:"frameCount";i:1;s:6:"looped";b:0;s:8:"duration";d:0.1000000000000000055511151231257827021181583404541015625;s:8:"metadata";a:2:{s:14:"GIFFileComment";a:1:{i:0;s:35:"GIF test file ⁕ Created with GIMP";}s:15:"_MW_GIF_VERSION";i:1;}}',
GIFHandler::METADATA_GOOD
],
// @codingStandardsIgnoreEnd
// @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
[
'nonanimated.gif',
- 'a:6:{s:10:"frameCount";i:1;s:6:"looped";b:0;s:8:"duration";d:0.1000000000000000055511151231257827021181583404541015625;s:8:"metadata";a:2:{s:14:"GIFFileComment";a:1:{i:0;s:35:"GIF test file ⁕ Created with GIMP";}s:15:"_MW_GIF_VERSION";i:1;}s:5:"width";i:45;s:6:"height";i:30;}'
+ 'a:4:{s:10:"frameCount";i:1;s:6:"looped";b:0;s:8:"duration";d:0.1000000000000000055511151231257827021181583404541015625;s:8:"metadata";a:2:{s:14:"GIFFileComment";a:1:{i:0;s:35:"GIF test file ⁕ Created with GIMP";}s:15:"_MW_GIF_VERSION";i:1;}}'
],
[
'animated-xmp.gif',
- 'a:6:{s:10:"frameCount";i:4;s:6:"looped";b:1;s:8:"duration";d:2.399999999999999911182158029987476766109466552734375;s:8:"metadata";a:5:{s:6:"Artist";s:7:"Bawolff";s:16:"ImageDescription";a:2:{s:9:"x-default";s:18:"A file to test GIF";s:5:"_type";s:4:"lang";}s:15:"SublocationDest";s:13:"The interwebs";s:14:"GIFFileComment";a:1:{i:0;s:16:"GIƒ·test·file";}s:15:"_MW_GIF_VERSION";i:1;}s:5:"width";i:45;s:6:"height";i:30;}'
+ 'a:4:{s:10:"frameCount";i:4;s:6:"looped";b:1;s:8:"duration";d:2.399999999999999911182158029987476766109466552734375;s:8:"metadata";a:5:{s:6:"Artist";s:7:"Bawolff";s:16:"ImageDescription";a:2:{s:9:"x-default";s:18:"A file to test GIF";s:5:"_type";s:4:"lang";}s:15:"SublocationDest";s:13:"The interwebs";s:14:"GIFFileComment";a:1:{i:0;s:16:"GIƒ·test·file";}s:15:"_MW_GIF_VERSION";i:1;}}'
],
// @codingStandardsIgnoreEnd
];
$file = $this->dataFile( 'test.jpg', 'image/jpeg' );
$res = $this->handler->getMetadata( $file, $this->filePath . 'test.jpg' );
// @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
- $expected = 'a:9:{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;s:5:"Width";i:20;s:6:"Height";i:20;}';
+ $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;}';
// @codingStandardsIgnoreEnd
// Unserialize in case serialization format ever changes.
$file = $this->dataFile( 'test.jpg', 'image/jpeg' );
$res = $this->handler->getCommonMetaArray( $file );
$expected = [
- 'Height' => 20,
- 'Width' => 20,
'ImageDescription' => 'Test file',
'XResolution' => '72/1',
'YResolution' => '72/1',
[ 'Something invalid!', PNGHandler::METADATA_BAD ],
// @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
[
- 'a:8:{s:10:"frameCount";i:0;s:9:"loopCount";i:1;s:8:"duration";d:0;s:8:"bitDepth";i:8;s:9:"colorType";s:10:"truecolour";s:5:"width";i:50;s:6:"height";i:50;s:8:"metadata";a:1:{s:15:"_MW_PNG_VERSION";i:1;}}',
+ 'a:6:{s:10:"frameCount";i:0;s:9:"loopCount";i:1;s:8:"duration";d:0;s:8:"bitDepth";i:8;s:9:"colorType";s:10:"truecolour";s:8:"metadata";a:1:{s:15:"_MW_PNG_VERSION";i:1;}}',
PNGHandler::METADATA_GOOD
],
// @codingStandardsIgnoreEnd
// @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
[
'rgb-na-png.png',
- 'a:8:{s:10:"frameCount";i:0;s:9:"loopCount";i:1;s:8:"duration";d:0;s:8:"bitDepth";i:8;s:9:"colorType";s:10:"truecolour";s:5:"width";i:50;s:6:"height";i:50;s:8:"metadata";a:1:{s:15:"_MW_PNG_VERSION";i:1;}}'
+ 'a:6:{s:10:"frameCount";i:0;s:9:"loopCount";i:1;s:8:"duration";d:0;s:8:"bitDepth";i:8;s:9:"colorType";s:10:"truecolour";s:8:"metadata";a:1:{s:15:"_MW_PNG_VERSION";i:1;}}'
],
[
'xmp.png',
- 'a:8:{s:10:"frameCount";i:0;s:9:"loopCount";i:1;s:8:"duration";d:0;s:8:"bitDepth";i:1;s:9:"colorType";s:14:"index-coloured";s:5:"width";i:50;s:6:"height";i:50;s:8:"metadata";a:2:{s:12:"SerialNumber";s:9:"123456789";s:15:"_MW_PNG_VERSION";i:1;}}'
+ 'a:6:{s:10:"frameCount";i:0;s:9:"loopCount";i:1;s:8:"duration";d:0;s:8:"bitDepth";i:1;s:9:"colorType";s:14:"index-coloured";s:8:"metadata";a:2:{s:12:"SerialNumber";s:9:"123456789";s:15:"_MW_PNG_VERSION";i:1;}}'
],
// @codingStandardsIgnoreEnd
];
$res = $this->handler->getMetadata( null, $this->filePath . 'test.tiff' );
// @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
- $expected = 'a:18:{s:10:"ImageWidth";i:20;s:11:"ImageLength";i:20;s:13:"BitsPerSample";a:3:{i:0;i:8;i:1;i:8;i:2;i:8;}s:11:"Compression";i:5;s:25:"PhotometricInterpretation";i:2;s:16:"ImageDescription";s:17:"Created with GIMP";s:12:"StripOffsets";i:8;s:11:"Orientation";i:1;s:15:"SamplesPerPixel";i:3;s:12:"RowsPerStrip";i:64;s:15:"StripByteCounts";i:238;s:11:"XResolution";s:19:"1207959552/16777216";s:11:"YResolution";s:19:"1207959552/16777216";s:19:"PlanarConfiguration";i:1;s:14:"ResolutionUnit";i:2;s:22:"MEDIAWIKI_EXIF_VERSION";i:2;s:5:"Width";i:20;s:6:"Height";i:20;}';
+ $expected = 'a:16:{s:10:"ImageWidth";i:20;s:11:"ImageLength";i:20;s:13:"BitsPerSample";a:3:{i:0;i:8;i:1;i:8;i:2;i:8;}s:11:"Compression";i:5;s:25:"PhotometricInterpretation";i:2;s:16:"ImageDescription";s:17:"Created with GIMP";s:12:"StripOffsets";i:8;s:11:"Orientation";i:1;s:15:"SamplesPerPixel";i:3;s:12:"RowsPerStrip";i:64;s:15:"StripByteCounts";i:238;s:11:"XResolution";s:19:"1207959552/16777216";s:11:"YResolution";s:19:"1207959552/16777216";s:19:"PlanarConfiguration";i:1;s:14:"ResolutionUnit";i:2;s:22:"MEDIAWIKI_EXIF_VERSION";i:2;}';
// @codingStandardsIgnoreEnd
// Re-unserialize in case there are subtle differences between how versions
public static function provideGetMetadata() {
return [
[ '80x60-2layers.xcf',
- 'a:3:{s:9:"colorType";s:16:"truecolour-alpha";s:5:"width";i:80;s:6:"height";i:60;}'
+ 'a:1:{s:9:"colorType";s:16:"truecolour-alpha";}'
],
[ '80x60-RGB.xcf',
- 'a:3:{s:9:"colorType";s:16:"truecolour-alpha";s:5:"width";i:80;s:6:"height";i:60;}'
+ 'a:1:{s:9:"colorType";s:16:"truecolour-alpha";}'
],
[ '80x60-Greyscale.xcf',
- 'a:3:{s:9:"colorType";s:15:"greyscale-alpha";s:5:"width";i:80;s:6:"height";i:60;}'
+ 'a:1:{s:9:"colorType";s:15:"greyscale-alpha";}'
],
];
}