Commit initial code for a Exiftool (http://www.sno.phy.queensu.ca/~phil/exiftool/)
authorMichael J. Rubinsky <mrubinsk@horde.org>
Mon, 3 Aug 2009 20:09:45 +0000 (16:09 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Mon, 3 Aug 2009 20:09:45 +0000 (16:09 -0400)
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
framework/Image/lib/Horde/Image/Exif/Base.php
framework/Image/lib/Horde/Image/Exif/Bundled.php
framework/Image/lib/Horde/Image/Exif/Exiftool.php [new file with mode: 0644]
framework/Image/lib/Horde/Image/Exif/Php.php
framework/Image/package.xml

index ed8cab0..ab06df8 100644 (file)
@@ -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/)
  *
  */
 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()
     {
index 60918aa..54afa14 100644 (file)
@@ -1,10 +1,41 @@
 <?php
 /**
+ * Base class for Horde_Image_Exif drivers.
  *
+ * 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 <mrubinsk@horde.org>
+ * @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
index 9818906..9ab0b99 100644 (file)
@@ -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 <mrubinsk@horde.org>
+ * @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 (file)
index 0000000..665b39a
--- /dev/null
@@ -0,0 +1,70 @@
+<?php
+/**
+ * Exiftool driver for reading/writing image meta data
+ *
+ * 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 <mrubinsk@horde.org>
+ * @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
index e68606e..bf380d7 100644 (file)
@@ -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 <mrubinsk@horde.org>
+ * @package Horde_Image
  */
 class Horde_Image_Exif_Php extends Horde_Image_Exif_Base
 {
index 9c52463..3611e30 100644 (file)
@@ -85,6 +85,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
         <file name="Base.php" role="php" />
         <file name="Bundled.php" role="php" />
         <file name="Php.php" role="php" />
+        <file name="Exiftool.php" role="php" />
       </dir> <!-- /Horde/Image/Exif -->
       <file name="Effect.php" role="php" />
       <file name="Gd.php" role="php" />
@@ -142,6 +143,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <install name="lib/Horde/Image/Exif/Base.php" as="Horde/Image/Exif/Base.php" />
    <install name="lib/Horde/Image/Exif/Bundled.php" as="Horde/Image/Exif/Bundled.php" />
    <install name="lib/Horde/Image/Exif/Php.php" as="Horde/Image/Exif/Php.php" />
+   <install name="lib/Horde/Image/Exif/Exiftool.php" as="Horde/Image/Exif/Exiftool.php" />
    <install name="lib/Horde/Image/Exif.php" as="Horde/Image/Exif.php" />
    <install name="lib/Horde/Image/Effect/Border.php" as="Horde/Image/Effect/Border.php" />
    <install name="lib/Horde/Image/Effect/Im/DropShadow.php" as="Horde/Image/Effect/Im/DropShadow.php" />