MediaWiki REL1_37
ExifBitmapHandler.php
Go to the documentation of this file.
1<?php
33 public const BROKEN_FILE = '-1';
34
36 public const OLD_BROKEN_FILE = '0';
37
38 public function convertMetadataVersion( $metadata, $version = 1 ) {
39 // basically flattens arrays.
40 $version = intval( explode( ';', $version, 2 )[0] );
41 if ( $version < 1 || $version >= 2 ) {
42 return $metadata;
43 }
44
45 if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) || $metadata['MEDIAWIKI_EXIF_VERSION'] != 2 ) {
46 return $metadata;
47 }
48
49 // Treat Software as a special case because in can contain
50 // an array of (SoftwareName, Version).
51 if ( isset( $metadata['Software'] )
52 && is_array( $metadata['Software'] )
53 && is_array( $metadata['Software'][0] )
54 && isset( $metadata['Software'][0][0] )
55 && isset( $metadata['Software'][0][1] )
56 ) {
57 $metadata['Software'] = $metadata['Software'][0][0] . ' (Version '
58 . $metadata['Software'][0][1] . ')';
59 }
60
61 $formatter = new FormatMetadata;
62
63 // ContactInfo also has to be dealt with specially
64 if ( isset( $metadata['Contact'] ) ) {
65 $metadata['Contact'] = $formatter->collapseContactInfo(
66 is_array( $metadata['Contact'] ) ? $metadata['Contact'] : [ $metadata['Contact'] ]
67 );
68 }
69
70 foreach ( $metadata as &$val ) {
71 if ( is_array( $val ) ) {
72 // @phan-suppress-next-line SecurityCheck-DoubleEscaped Ambiguous with the true for nohtml
73 $val = $formatter->flattenArrayReal( $val, 'ul', true );
74 }
75 }
76 $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
77
78 return $metadata;
79 }
80
85 public function isFileMetadataValid( $image ) {
86 global $wgShowEXIF;
87 if ( !$wgShowEXIF ) {
88 # Metadata disabled and so an empty field is expected
90 }
91 $exif = $image->getMetadataArray();
92 if ( !$exif ) {
93 wfDebug( __METHOD__ . ': error unserializing?' );
94 return self::METADATA_BAD;
95 }
96 if ( $exif === [ '_error' => self::OLD_BROKEN_FILE ] ) {
97 # Old special value indicating that there is no Exif data in the file.
98 # or that there was an error well extracting the metadata.
99 wfDebug( __METHOD__ . ": back-compat version" );
101 }
102
103 if ( $exif === [ '_error' => self::BROKEN_FILE ] ) {
104 return self::METADATA_GOOD;
105 }
106
107 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
108 || $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version()
109 ) {
110 if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
111 && $exif['MEDIAWIKI_EXIF_VERSION'] == 1
112 ) {
113 // back-compatible but old
114 wfDebug( __METHOD__ . ": back-compat version" );
115
117 }
118 # Wrong (non-compatible) version
119 wfDebug( __METHOD__ . ": wrong version" );
120
121 return self::METADATA_BAD;
122 }
123
124 return self::METADATA_GOOD;
125 }
126
132 public function formatMetadata( $image, $context = false ) {
133 $meta = $this->getCommonMetaArray( $image );
134 if ( !$meta ) {
135 return false;
136 }
137
138 return $this->formatMetadataHelper( $meta, $context );
139 }
140
141 public function getCommonMetaArray( File $file ) {
142 $exif = $file->getMetadataArray();
143 if ( !$exif ) {
144 return [];
145 }
146 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
147
148 return $exif;
149 }
150
151 public function getMetadataType( $image ) {
152 return 'exif';
153 }
154
155 protected function applyExifRotation( $info, $metadata ) {
156 if ( $this->autoRotateEnabled() ) {
157 $rotation = $this->getRotationForExifFromOrientation( $metadata['Orientation'] ?? null );
158 } else {
159 $rotation = 0;
160 }
161
162 if ( $rotation == 90 || $rotation == 270 ) {
163 $width = $info['width'];
164 $info['width'] = $info['height'];
165 $info['height'] = $width;
166 }
167 return $info;
168 }
169
182 public function getRotation( $file ) {
183 if ( !$this->autoRotateEnabled() ) {
184 return 0;
185 }
186
187 $orientation = $file->getMetadataItem( 'Orientation' );
188 return $this->getRotationForExifFromOrientation( $orientation );
189 }
190
199 protected function getRotationForExifFromOrientation( $orientation ) {
200 if ( $orientation === null ) {
201 return 0;
202 }
203 # See http://sylvana.net/jpegcrop/exif_orientation.html
204 switch ( $orientation ) {
205 case 8:
206 return 90;
207 case 3:
208 return 180;
209 case 6:
210 return 270;
211 default:
212 return 0;
213 }
214 }
215}
$wgShowEXIF
Show Exif data, on by default if available.
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()
#-
Definition Exif.php:590
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:66
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
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42