function getMetadata($image, $filename) { try { $meta = BitmapMetadataHandler::Jpeg($filename); if (!is_array($meta)) { // This should never happen, but doesn't hurt to be paranoid. throw new MWException('Metadata array is not an array'); } $meta['MEDIAWIKI_EXIF_VERSION'] = Exif::version(); return serialize($meta); } catch (MWException $e) { // BitmapMetadataHandler throws an exception in certain exceptional // cases like if file does not exist. wfDebug(__METHOD__ . ': ' . $e->getMessage() . "\n"); /* This used to use 0 (ExifBitmapHandler::OLD_BROKEN_FILE) for the cases * * No metadata in the file * * Something is broken in the file. * However, if the metadata support gets expanded then you can't tell if the 0 is from * a broken file, or just no props found. A broken file is likely to stay broken, but * a file which had no props could have props once the metadata support is improved. * Thus switch to using -1 to denote only a broken file, and use an array with only * MEDIAWIKI_EXIF_VERSION to denote no props. */ return ExifBitmapHandler::BROKEN_FILE; } }
function isMetadataValid($image, $metadata) { global $wgShowEXIF; if (!$wgShowEXIF) { # Metadata disabled and so an empty field is expected return self::METADATA_GOOD; } if ($metadata === self::OLD_BROKEN_FILE) { # Old special value indicating that there is no EXIF data in the file. # or that there was an error well extracting the metadata. wfDebug(__METHOD__ . ": back-compat version\n"); return self::METADATA_COMPATIBLE; } if ($metadata === self::BROKEN_FILE) { return self::METADATA_GOOD; } wfSuppressWarnings(); $exif = unserialize($metadata); wfRestoreWarnings(); if (!isset($exif['MEDIAWIKI_EXIF_VERSION']) || $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version()) { if (isset($exif['MEDIAWIKI_EXIF_VERSION']) && $exif['MEDIAWIKI_EXIF_VERSION'] == 1) { //back-compatible but old wfDebug(__METHOD__ . ": back-compat version\n"); return self::METADATA_COMPATIBLE; } # Wrong (non-compatible) version wfDebug(__METHOD__ . ": wrong version\n"); return self::METADATA_BAD; } return self::METADATA_GOOD; }
/** * @param $image * @param $filename * @return string */ function getMetadata($image, $filename) { global $wgShowEXIF; if ($wgShowEXIF) { try { $meta = BitmapMetadataHandler::Tiff($filename); if (!is_array($meta)) { // This should never happen, but doesn't hurt to be paranoid. throw new MWException('Metadata array is not an array'); } $meta['MEDIAWIKI_EXIF_VERSION'] = Exif::version(); return serialize($meta); } catch (MWException $e) { // BitmapMetadataHandler throws an exception in certain exceptional // cases like if file does not exist. wfDebug(__METHOD__ . ': ' . $e->getMessage() . "\n"); return ExifBitmapHandler::BROKEN_FILE; } } else { return ''; } }
function isMetadataValid($image, $metadata) { global $wgShowEXIF; if (!$wgShowEXIF) { # Metadata disabled and so an empty field is expected return true; } if ($metadata === '0') { # Special value indicating that there is no EXIF data in the file return true; } $exif = @unserialize($metadata); if (!isset($exif['MEDIAWIKI_EXIF_VERSION']) || $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version()) { # Wrong version wfDebug(__METHOD__ . ": wrong version\n"); return false; } return true; }
function getExifData() { global $wgRequest; if ($this->metadata === '0') { return array(); } $purge = $wgRequest->getVal('action') == 'purge'; $ret = unserialize($this->metadata); $oldver = isset($ret['MEDIAWIKI_EXIF_VERSION']) ? $ret['MEDIAWIKI_EXIF_VERSION'] : 0; $newver = Exif::version(); if (!count($ret) || $purge || $oldver != $newver) { $this->updateExifData($newver); } if (isset($ret['MEDIAWIKI_EXIF_VERSION'])) { unset($ret['MEDIAWIKI_EXIF_VERSION']); } $format = new FormatExif($ret); return $format->getFormattedData(); }
/** * This doesn't do much yet, but eventually I plan to add * XMP support for Tiff. (PHP's exif support already extracts * but needs some further processing because PHP's exif support * is stupid...) * * @todo Add XMP support, so this function actually makes * sense to put here. * * The various exceptions this throws are caught later. * @param $filename String * @throws MWException * @return Array The metadata. */ public static function Tiff($filename) { if (file_exists($filename)) { $byteOrder = self::getTiffByteOrder($filename); if (!$byteOrder) { throw new MWException("Error determining byte order of {$filename}"); } $exif = new Exif($filename, $byteOrder); $data = $exif->getFilteredData(); if ($data) { $data['MEDIAWIKI_EXIF_VERSION'] = Exif::version(); return $data; } else { throw new MWException("Could not extract data from tiff file {$filename}"); } } else { throw new MWException("File doesn't exist - {$filename}"); } }
/** * Reads metadata of the tiff file via shell command and returns an associative array. * layout: * meta['page_count'] = number of pages * meta['first_page'] = number of first page * meta['last_page'] = number of last page * meta['page_data'] = metadata per page * meta['exif'] = Exif, XMP and IPTC * meta['errors'] = identify-errors * meta['warnings'] = identify-warnings */ public function retrieveMetaData() { global $wgImageMagickIdentifyCommand, $wgExiv2Command, $wgTiffUseExiv; global $wgTiffUseTiffinfo, $wgTiffTiffinfoCommand; global $wgShowEXIF; if ( $this->_meta === null ) { wfProfileIn( 'PagedTiffImage::retrieveMetaData' ); //fetch base info: number of pages, size and alpha for each page. //run hooks first, then optionally tiffinfo or, per default, ImageMagic's identify command if ( !wfRunHooks( 'PagedTiffHandlerTiffData', array( $this->mFilename, &$this->_meta ) ) ) { wfDebug( __METHOD__ . ": hook PagedTiffHandlerTiffData overrides TIFF data extraction\n" ); } elseif ( $wgTiffUseTiffinfo ) { // read TIFF directories using libtiff's tiffinfo, see // http://www.libtiff.org/man/tiffinfo.1.html $cmd = wfEscapeShellArg( $wgTiffTiffinfoCommand ) . ' ' . wfEscapeShellArg( $this->mFilename ) . ' 2>&1'; wfProfileIn( 'tiffinfo' ); wfDebug( __METHOD__ . ": $cmd\n" ); $retval = ''; $dump = wfShellExec( $cmd, $retval ); wfProfileOut( 'tiffinfo' ); if ( $retval ) { $data['errors'][] = "tiffinfo command failed: $cmd"; wfDebug( __METHOD__ . ": tiffinfo command failed: $cmd\n" ); return $data; // fail. we *need* that info } $this->_meta = $this->parseTiffinfoOutput( $dump ); } else { $cmd = wfEscapeShellArg( $wgImageMagickIdentifyCommand ) . ' -format "[BEGIN]page=%p\nalpha=%A\nalpha2=%r\nheight=%h\nwidth=%w\ndepth=%z[END]" ' . wfEscapeShellArg( $this->mFilename ) . ' 2>&1'; wfProfileIn( 'identify' ); wfDebug( __METHOD__ . ": $cmd\n" ); $retval = ''; $dump = wfShellExec( $cmd, $retval ); wfProfileOut( 'identify' ); if ( $retval ) { $data['errors'][] = "identify command failed: $cmd"; wfDebug( __METHOD__ . ": identify command failed: $cmd\n" ); return $data; // fail. we *need* that info } $this->_meta = $this->parseIdentifyOutput( $dump ); } $this->_meta['exif'] = array(); //fetch extended info: EXIF/IPTC/XMP //run hooks first, then optionally Exiv2 or, per default, the internal EXIF class if ( !empty( $this->_meta['errors'] ) ) { wfDebug( __METHOD__ . ": found errors, skipping EXIF extraction\n" ); } elseif ( !wfRunHooks( 'PagedTiffHandlerExifData', array( $this->mFilename, &$this->_meta['exif'] ) ) ) { wfDebug( __METHOD__ . ": hook PagedTiffHandlerExifData overrides EXIF extraction\n" ); } elseif ( $wgTiffUseExiv ) { // read EXIF, XMP, IPTC as name-tag => interpreted data // -ignore unknown fields // see exiv2-doc @link http://www.exiv2.org/sample.html // NOTE: the linux version of exiv2 has a bug: it can only // read one type of meta-data at a time, not all at once. $cmd = wfEscapeShellArg( $wgExiv2Command ) . ' -u -psix -Pnt ' . wfEscapeShellArg( $this->mFilename ) . ' 2>&1'; wfProfileIn( 'exiv2' ); wfDebug( __METHOD__ . ": $cmd\n" ); $retval = ''; $dump = wfShellExec( $cmd, $retval ); wfProfileOut( 'exiv2' ); if ( $retval ) { $data['errors'][] = "exiv command failed: $cmd"; wfDebug( __METHOD__ . ": exiv command failed: $cmd\n" ); // don't fail - we are missing info, just report } $data = $this->parseExiv2Output( $dump ); $this->_meta['exif'] = $data; } elseif ( $wgShowEXIF ) { wfDebug( __METHOD__ . ": using internal Exif( {$this->mFilename} )\n" ); if ( method_exists( 'BitmapMetadataHandler', 'Tiff' ) ) { $data = BitmapMetadataHandler::Tiff( $this->mFilename ); } else { // old method for back compat. $exif = new Exif( $this->mFilename ); $data = $exif->getFilteredData(); } if ( $data ) { $data['MEDIAWIKI_EXIF_VERSION'] = Exif::version(); $this->_meta['exif'] = $data; } } unset( $this->_meta['exif']['Image'] ); unset( $this->_meta['exif']['filename'] ); unset( $this->_meta['exif']['Base filename'] ); unset( $this->_meta['exif']['XMLPacket'] ); unset( $this->_meta['exif']['ImageResources'] ); $this->_meta['TIFF_METADATA_VERSION'] = TIFF_METADATA_VERSION; wfProfileOut( 'PagedTiffImage::retrieveMetaData' ); } return $this->_meta; }