MediaWiki master
ExifBitmapHandler.php
Go to the documentation of this file.
1<?php
2
29
39 public const BROKEN_FILE = '-1';
40
42 public const OLD_BROKEN_FILE = '0';
43
44 public function convertMetadataVersion( $metadata, $version = 1 ) {
45 // basically flattens arrays.
46 $version = is_int( $version ) ? $version : (int)explode( ';', $version, 2 )[0];
47 if ( $version < 1 || $version >= 2 ) {
48 return $metadata;
49 }
50
51 if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) || $metadata['MEDIAWIKI_EXIF_VERSION'] !== 2 ) {
52 return $metadata;
53 }
54
55 // Treat Software as a special case because in can contain
56 // an array of (SoftwareName, Version).
57 if ( isset( $metadata['Software'] )
58 && is_array( $metadata['Software'] )
59 && is_array( $metadata['Software'][0] )
60 && isset( $metadata['Software'][0][0] )
61 && isset( $metadata['Software'][0][1] )
62 ) {
63 $metadata['Software'] = $metadata['Software'][0][0] . ' (Version '
64 . $metadata['Software'][0][1] . ')';
65 }
66
67 $formatter = new FormatMetadata;
68
69 // ContactInfo also has to be dealt with specially
70 if ( isset( $metadata['Contact'] ) ) {
71 $metadata['Contact'] = $formatter->collapseContactInfo(
72 is_array( $metadata['Contact'] ) ? $metadata['Contact'] : [ $metadata['Contact'] ]
73 );
74 }
75
76 // Ignore Location shown if it is not a simple string
77 if ( isset( $metadata['LocationShown'] ) && !is_string( $metadata['LocationShown'] ) ) {
78 unset( $metadata['LocationShown'] );
79 }
80
81 foreach ( $metadata as &$val ) {
82 if ( is_array( $val ) ) {
83 // @phan-suppress-next-line SecurityCheck-DoubleEscaped Ambiguous with the true for nohtml
84 $val = $formatter->flattenArrayReal( $val, 'ul', true );
85 }
86 }
87 unset( $val );
88 $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
89
90 return $metadata;
91 }
92
97 public function isFileMetadataValid( $image ) {
98 $showEXIF = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::ShowEXIF );
99 if ( !$showEXIF ) {
100 # Metadata disabled and so an empty field is expected
101 return self::METADATA_GOOD;
102 }
103 $exif = $image->getMetadataArray();
104 if ( !$exif ) {
105 wfDebug( __METHOD__ . ': error unserializing?' );
106 return self::METADATA_BAD;
107 }
108 if ( $exif === [ '_error' => self::OLD_BROKEN_FILE ] ) {
109 # Old special value indicating that there is no Exif data in the file.
110 # or that there was an error well extracting the metadata.
111 wfDebug( __METHOD__ . ": back-compat version" );
113 }
114
115 if ( $exif === [ '_error' => self::BROKEN_FILE ] ) {
116 return self::METADATA_GOOD;
117 }
118
119 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
120 || $exif['MEDIAWIKI_EXIF_VERSION'] !== Exif::version()
121 ) {
122 if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
123 && $exif['MEDIAWIKI_EXIF_VERSION'] === 1
124 ) {
125 // back-compatible but old
126 wfDebug( __METHOD__ . ": back-compat version" );
127
129 }
130 # Wrong (non-compatible) version
131 wfDebug( __METHOD__ . ": wrong version" );
132
133 return self::METADATA_BAD;
134 }
135
136 return self::METADATA_GOOD;
137 }
138
144 public function formatMetadata( $image, $context = false ) {
145 $meta = $this->getCommonMetaArray( $image );
146 if ( !$meta ) {
147 return false;
148 }
149
150 return $this->formatMetadataHelper( $meta, $context );
151 }
152
153 public function getCommonMetaArray( File $file ) {
154 $exif = $file->getMetadataArray();
155 if ( !$exif ) {
156 return [];
157 }
158 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
159
160 return $exif;
161 }
162
163 public function getMetadataType( $image ) {
164 return 'exif';
165 }
166
167 protected function applyExifRotation( $info, $metadata ) {
168 if ( $this->autoRotateEnabled() ) {
169 $rotation = $this->getRotationForExifFromOrientation( $metadata['Orientation'] ?? null );
170 } else {
171 $rotation = 0;
172 }
173
174 if ( $rotation === 90 || $rotation === 270 ) {
175 $width = $info['width'];
176 $info['width'] = $info['height'];
177 $info['height'] = $width;
178 }
179 return $info;
180 }
181
194 public function getRotation( $file ) {
195 if ( !$this->autoRotateEnabled() ) {
196 return 0;
197 }
198
199 $orientation = $file->getMetadataItem( 'Orientation' );
200 return $this->getRotationForExifFromOrientation( $orientation );
201 }
202
211 protected function getRotationForExifFromOrientation( $orientation ) {
212 if ( $orientation === null ) {
213 return 0;
214 }
215 # See http://sylvana.net/jpegcrop/exif_orientation.html
216 switch ( $orientation ) {
217 case 8:
218 return 90;
219 case 3:
220 return 180;
221 case 6:
222 return 270;
223 default:
224 return 0;
225 }
226 }
227}
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:704
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
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:93
getMetadataArray()
Get the unserialized handler-specific metadata STUB.
Definition File.php:791
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.