25use Wikimedia\XMPReader\Reader as XMPReader;
49 60 => [
'iptc-good-hash',
'iptc-no-hash' ],
50 70 => [
'xmp-deprecated' ],
51 80 => [
'xmp-general' ],
53 100 => [
'iptc-bad-hash' ],
71 }
catch ( Exception $e ) {
75 wfDebug(
"Error parsing iptc data of file: " . $e->getMessage() );
76 $this->iptcType =
'iptc-no-hash';
93 public function getExif( $filename, $byteOrder ) {
96 $exif =
new Exif( $filename, $byteOrder );
97 $data = $exif->getFilteredData();
111 if ( isset( $this->metadata[
$type] ) ) {
113 $metaArray += $this->metadata[
$type];
116 $this->metadata[
$type] = $metaArray;
132 krsort( $this->metaPriority );
133 foreach ( $this->metaPriority as $pri ) {
134 foreach ( $pri as
$type ) {
135 if ( isset( $this->metadata[
$type] ) ) {
138 foreach ( $this->metadata[
$type] as $itemName => $item ) {
139 if ( is_array( $item ) && isset( $item[
'_type'] ) && $item[
'_type'] ===
'lang' &&
140 isset( $temp[$itemName] ) && !is_array( $temp[$itemName] )
142 $default = $temp[$itemName];
143 $temp[$itemName] = $item;
144 $temp[$itemName][
'x-default'] = $default;
145 unset( $this->metadata[
$type][$itemName] );
149 $temp += $this->metadata[
$type];
163 public static function Jpeg( $filename ) {
164 $showXMP = XMPReader::isSupported();
169 if ( isset( $seg[
'COM'] ) && isset( $seg[
'COM'][0] ) ) {
170 $meta->addMetadata( [
'JPEGFileComment' => $seg[
'COM'] ],
'native' );
172 if ( isset( $seg[
'PSIR'] ) && count( $seg[
'PSIR'] ) > 0 ) {
173 foreach ( $seg[
'PSIR'] as $curPSIRValue ) {
174 $meta->doApp13( $curPSIRValue );
177 if ( isset( $seg[
'XMP'] ) && $showXMP ) {
178 $xmp =
new XMPReader( LoggerFactory::getInstance(
'XMP' ), $filename );
179 $xmp->parse( $seg[
'XMP'] );
180 foreach ( $seg[
'XMP_ext'] as $xmpExt ) {
184 $xmp->parseExtended( $xmpExt );
186 $res = $xmp->getResults();
188 $meta->addMetadata( $array,
$type );
192 $meta->getExif( $filename, $seg[
'byteOrder'] ??
'BE' );
194 return $meta->getMetadataArray();
205 public static function PNG( $filename ) {
206 $showXMP = XMPReader::isSupported();
210 if ( isset( $array[
'text'][
'xmp'][
'x-default'] )
211 && $array[
'text'][
'xmp'][
'x-default'] !==
'' && $showXMP
213 $xmp =
new XMPReader( LoggerFactory::getInstance(
'XMP' ), $filename );
214 $xmp->parse( $array[
'text'][
'xmp'][
'x-default'] );
215 $xmpRes = $xmp->getResults();
216 foreach ( $xmpRes as
$type => $xmpSection ) {
217 $meta->addMetadata( $xmpSection,
$type );
220 unset( $array[
'text'][
'xmp'] );
221 $meta->addMetadata( $array[
'text'],
'native' );
222 unset( $array[
'text'] );
223 $array[
'metadata'] = $meta->getMetadataArray();
237 public static function GIF( $filename ) {
241 if ( count( $baseArray[
'comment'] ) > 0 ) {
242 $meta->addMetadata( [
'GIFFileComment' => $baseArray[
'comment'] ],
'native' );
245 if ( $baseArray[
'xmp'] !==
'' && XMPReader::isSupported() ) {
246 $xmp =
new XMPReader( LoggerFactory::getInstance(
'XMP' ), $filename );
247 $xmp->parse( $baseArray[
'xmp'] );
248 $xmpRes = $xmp->getResults();
249 foreach ( $xmpRes as
$type => $xmpSection ) {
250 $meta->addMetadata( $xmpSection,
$type );
254 unset( $baseArray[
'comment'] );
255 unset( $baseArray[
'xmp'] );
257 $baseArray[
'metadata'] = $meta->getMetadataArray();
276 public static function Tiff( $filename ) {
277 if ( file_exists( $filename ) ) {
280 throw new MWException(
"Error determining byte order of $filename" );
282 $exif =
new Exif( $filename, $byteOrder );
283 $data = $exif->getFilteredData();
289 throw new MWException(
"Could not extract data from tiff file $filename" );
292 throw new MWException(
"File doesn't exist - $filename" );
304 $fh = fopen( $filename,
'rb' );
308 $head = fread( $fh, 2 );
$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.
Class to extract and validate Exif data from jpeg (and possibly tiff) files.
static parse( $rawData)
This takes the results of iptcparse() and puts it into a form that can be handled by mediawiki.