From d2e9b983c1ccb7661a544ba5346961a138308e68 Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Mon, 3 Aug 2009 16:09:45 -0400 Subject: [PATCH] Commit initial code for a Exiftool (http://www.sno.phy.queensu.ca/~phil/exiftool/) driver for Horde_Image_Exif Exiftool gives us drastically improved coverage for image metadata tags. Supports EXIF, IPTC, XMP and a slew of others. Also, it can give us *write* ability for all supported tags. --- framework/Image/lib/Horde/Image/Exif.php | 25 ++++++-- framework/Image/lib/Horde/Image/Exif/Base.php | 50 +++++++++++++++- framework/Image/lib/Horde/Image/Exif/Bundled.php | 7 +++ framework/Image/lib/Horde/Image/Exif/Exiftool.php | 70 +++++++++++++++++++++++ framework/Image/lib/Horde/Image/Exif/Php.php | 7 +++ framework/Image/package.xml | 2 + 6 files changed, 154 insertions(+), 7 deletions(-) create mode 100644 framework/Image/lib/Horde/Image/Exif/Exiftool.php diff --git a/framework/Image/lib/Horde/Image/Exif.php b/framework/Image/lib/Horde/Image/Exif.php index ed8cab05c..ab06df863 100644 --- a/framework/Image/lib/Horde/Image/Exif.php +++ b/framework/Image/lib/Horde/Image/Exif.php @@ -3,7 +3,8 @@ * General class for fetching and parsing EXIF information from images. * * Works equally well with either the built in php exif functions (if PHP - * compiled with exif support) or the (slower) bundled exif library. + * compiled with exif support), the Exiftool package (more complete but slower), + * or the bundled exif library. * * Copyright 2003-2009 The Horde Project (http://www.horde.org/) * @@ -12,7 +13,15 @@ */ class Horde_Image_Exif { - static public function factory($driver = null) + /** + * Factory method for instantiating a Horde_Image_Exif object. + * + * @param string $driver + * @param array $params + * + * @return Horde_Image_Exif + */ + static public function factory($driver = null, $params = array()) { if (empty($driver) && function_exists('exif_read_data')) { $driver = 'Php'; @@ -24,15 +33,16 @@ class Horde_Image_Exif $class = 'Horde_Image_Exif_' . $driver; - return new $class; + return new $class($params); } /** * Converts from Intel to Motorola endien. Just reverses the bytes * (assumes hex is passed in) * - * @param $num - * @return unknown_type + * @param $intel + * + * @return */ static public function intel2Moto($intel) { @@ -46,8 +56,11 @@ class Horde_Image_Exif } /** + * Obtain an array of supported meta data fields. + * + * @TODO: This should probably be extended by the subclass? * - * @return unknown_type + * @return array */ static public function getFields() { diff --git a/framework/Image/lib/Horde/Image/Exif/Base.php b/framework/Image/lib/Horde/Image/Exif/Base.php index 60918aabd..54afa14ea 100644 --- a/framework/Image/lib/Horde/Image/Exif/Base.php +++ b/framework/Image/lib/Horde/Image/Exif/Base.php @@ -1,10 +1,41 @@ + * @package Horde_Image */ abstract class Horde_Image_Exif_Base { - abstract public function getData($image); + /** + * Instance parameters. + * + * @var array + */ + protected $_params; + + /** + * Optional Logger + */ + protected $_logger; + + /** + * + * @param $params + */ + public function __construct($params = array()) + { + if (!empty($params['logger'])) { + $this->_logger = $params['logger']; + unset($params['logger']); + } + $this->_params = $params; + } /** * @@ -95,4 +126,21 @@ abstract class Horde_Image_Exif_Base return round($degs, 6); } + protected function _logDebug($message) + { + if (!empty($this->_logger)) { + $this->_logger->debug($message); + } + } + + protected function _logErr($message) + { + if (!empty($this->_logger)) { + $this->_logger->err($message); + } + } + + abstract public function getData($image); + + } \ No newline at end of file diff --git a/framework/Image/lib/Horde/Image/Exif/Bundled.php b/framework/Image/lib/Horde/Image/Exif/Bundled.php index 981890678..9ab0b99e7 100644 --- a/framework/Image/lib/Horde/Image/Exif/Bundled.php +++ b/framework/Image/lib/Horde/Image/Exif/Bundled.php @@ -3,6 +3,13 @@ * Class for dealing with Exif data using a bundled PHP library based on Exifer. * * + * Copyright 2009 The Horde Project (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (LGPL). If you + * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. + * + * @author Michael J. Rubinsky + * @package Horde_Image */ class Horde_Image_Exif_Bundled extends Horde_Image_Exif_Base { diff --git a/framework/Image/lib/Horde/Image/Exif/Exiftool.php b/framework/Image/lib/Horde/Image/Exif/Exiftool.php new file mode 100644 index 000000000..665b39a83 --- /dev/null +++ b/framework/Image/lib/Horde/Image/Exif/Exiftool.php @@ -0,0 +1,70 @@ + + * @package Horde_Image + */ +class Horde_Image_Exif_Exiftool extends Horde_Image_Exif_Base +{ + /** + * Path to exiftool binary + * + * @var string + */ + protected $_exiftool; + + public function __construct($params) + { + parent::__construct($params); + if (!empty($this->_params['exiftool'])) { + $this->_exiftool = $this->_params['exiftool']; + } else { + throw new InvalidArgumentException('Missing required exiftool path'); + } + } + + /** + * + * @return unknown_type + */ + public function getData($image) + { + // Request the full stream of meta data in JSON format. + $command = '-j ' . $image; + $test = $this->_execute($command); + $results = json_decode($this->_execute($command)); + var_dump($results); + if ($results instanceof stdClass) { + return $results; + } + + throw new Horde_Image_Exception('Unknown error running exiftool command.'); + } + + /** + * Executes a exiftool command. + * + * @param string $command The command to run + * + * @return mixed The result of the command. + */ + protected function _execute($command) + { + exec($this->_exiftool . ' ' . escapeshellcmd($command), $output, $retval); + if ($retval) { + $this->_logErr(sprintf("Error running command: %s"), $command . "\n" . implode("\n", $output)); + } + if (is_array($output)) { + $output = implode('', $output); + } + + return $output; + } + +} \ No newline at end of file diff --git a/framework/Image/lib/Horde/Image/Exif/Php.php b/framework/Image/lib/Horde/Image/Exif/Php.php index e68606eb9..bf380d725 100644 --- a/framework/Image/lib/Horde/Image/Exif/Php.php +++ b/framework/Image/lib/Horde/Image/Exif/Php.php @@ -2,6 +2,13 @@ /** * Exif driver for Horde_Image utilizing PHP's compiled-in exif functions * + * Copyright 2009 The Horde Project (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (LGPL). If you + * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. + * + * @author Michael J. Rubinsky + * @package Horde_Image */ class Horde_Image_Exif_Php extends Horde_Image_Exif_Base { diff --git a/framework/Image/package.xml b/framework/Image/package.xml index 9c5246355..3611e301a 100644 --- a/framework/Image/package.xml +++ b/framework/Image/package.xml @@ -85,6 +85,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + @@ -142,6 +143,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + -- 2.11.0