MediaWiki master
ExifBitmapHandler.php
Go to the documentation of this file.
1<?php
2
28
38 public const BROKEN_FILE = '-1';
39
41 public const OLD_BROKEN_FILE = '0';
42
43 public function convertMetadataVersion( $metadata, $version = 1 ) {
44 // basically flattens arrays.
45 $version = is_int( $version ) ? $version : (int)explode( ';', $version, 2 )[0];
46 if ( $version < 1 || $version >= 2 ) {
47 return $metadata;
48 }
49
50 if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) || $metadata['MEDIAWIKI_EXIF_VERSION'] !== 2 ) {
51 return $metadata;
52 }
53
54 // Treat Software as a special case because in can contain
55 // an array of (SoftwareName, Version).
56 if ( isset( $metadata['Software'] )
57 && is_array( $metadata['Software'] )
58 && is_array( $metadata['Software'][0] )
59 && isset( $metadata['Software'][0][0] )
60 && isset( $metadata['Software'][0][1] )
61 ) {
62 $metadata['Software'] = $metadata['Software'][0][0] . ' (Version '
63 . $metadata['Software'][0][1] . ')';
64 }
65
66 $formatter = new FormatMetadata;
67
68 // ContactInfo also has to be dealt with specially
69 if ( isset( $metadata['Contact'] ) ) {
70 $metadata['Contact'] = $formatter->collapseContactInfo(
71 is_array( $metadata['Contact'] ) ? $metadata['Contact'] : [ $metadata['Contact'] ]
72 );
73 }
74
75 // Ignore Location shown if it is not a simple string
76 if ( isset( $metadata['LocationShown'] ) && !is_string( $metadata['LocationShown'] ) ) {
77 unset( $metadata['LocationShown'] );
78 }
79
80 foreach ( $metadata as &$val ) {
81 if ( is_array( $val ) ) {
82 // @phan-suppress-next-line SecurityCheck-DoubleEscaped Ambiguous with the true for nohtml
83 $val = $formatter->flattenArrayReal( $val, 'ul', true );
84 }
85 }
86 unset( $val );
87 $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
88
89 return $metadata;
90 }
91
96 public function isFileMetadataValid( $image ) {
97 $showEXIF = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::ShowEXIF );
98 if ( !$showEXIF ) {
99 # Metadata disabled and so an empty field is expected
100 return self::METADATA_GOOD;
101 }
102 $exif = $image->getMetadataArray();
103 if ( !$exif ) {
104 wfDebug( __METHOD__ . ': error unserializing?' );
105 return self::METADATA_BAD;
106 }
107 if ( $exif === [ '_error' => self::OLD_BROKEN_FILE ] ) {
108 # Old special value indicating that there is no Exif data in the file.
109 # or that there was an error well extracting the metadata.
110 wfDebug( __METHOD__ . ": back-compat version" );
112 }
113
114 if ( $exif === [ '_error' => self::BROKEN_FILE ] ) {
115 return self::METADATA_GOOD;
116 }
117
118 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
119 || $exif['MEDIAWIKI_EXIF_VERSION'] !== Exif::version()
120 ) {
121 if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
122 && $exif['MEDIAWIKI_EXIF_VERSION'] === 1
123 ) {
124 // back-compatible but old
125 wfDebug( __METHOD__ . ": back-compat version" );
126
128 }
129 # Wrong (non-compatible) version
130 wfDebug( __METHOD__ . ": wrong version" );
131
132 return self::METADATA_BAD;
133 }
134
135 return self::METADATA_GOOD;
136 }
137
143 public function formatMetadata( $image, $context = false ) {
144 $meta = $this->getCommonMetaArray( $image );
145 if ( !$meta ) {
146 return false;
147 }
148
149 return $this->formatMetadataHelper( $meta, $context );
150 }
151
152 public function getCommonMetaArray( File $file ) {
153 $exif = $file->getMetadataArray();
154 if ( !$exif ) {
155 return [];
156 }
157 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
158
159 return $exif;
160 }
161
162 public function getMetadataType( $image ) {
163 return 'exif';
164 }
165
166 protected function applyExifRotation( $info, $metadata ) {
167 if ( $this->autoRotateEnabled() ) {
168 $rotation = $this->getRotationForExifFromOrientation( $metadata['Orientation'] ?? null );
169 } else {
170 $rotation = 0;
171 }
172
173 if ( $rotation === 90 || $rotation === 270 ) {
174 $width = $info['width'];
175 $info['width'] = $info['height'];
176 $info['height'] = $width;
177 }
178 return $info;
179 }
180
193 public function getRotation( $file ) {
194 if ( !$this->autoRotateEnabled() ) {
195 return 0;
196 }
197
198 $orientation = $file->getMetadataItem( 'Orientation' );
199 return $this->getRotationForExifFromOrientation( $orientation );
200 }
201
210 protected function getRotationForExifFromOrientation( $orientation ) {
211 if ( $orientation === null ) {
212 return 0;
213 }
214 # See http://sylvana.net/jpegcrop/exif_orientation.html
215 switch ( $orientation ) {
216 case 8:
217 return 90;
218 case 3:
219 return 180;
220 case 6:
221 return 270;
222 default:
223 return 0;
224 }
225 }
226}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Generic handler for bitmap images.
Stuff specific to JPEG and (built-in) TIFF handler.
getRotationForExifFromOrientation( $orientation)
Given a chunk of serialized Exif metadata, return the orientation as degrees of rotation.
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.
getCommonMetaArray(File $file)
Get an array of standard (FormatMetadata type) metadata values.
applyExifRotation( $info, $metadata)
const OLD_BROKEN_FILE
Outdated error extracting metadata.
convertMetadataVersion( $metadata, $version=1)
Convert metadata version.
getRotation( $file)
On supporting image formats, try to read out the low-level orientation of the file and return the ang...
const BROKEN_FILE
Error extracting metadata.
formatMetadata( $image, $context=false)
static version()
The version of the output format.
Definition Exif.php:706
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:74
getMetadataArray()
Get the unserialized handler-specific metadata STUB.
Definition File.php:760
Format Image metadata values into a human readable form.
collapseContactInfo(array $vals)
Format the contact info field into a single value.
const METADATA_COMPATIBLE
formatMetadataHelper( $metadataArray, $context=false)
sorts the visible/invisible field.
const METADATA_GOOD
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Interface for objects which can provide a MediaWiki context on request.