Start work on implementing an Iterator for Horde_Image for supporting multi-page...
authorMichael J. Rubinsky <mrubinsk@horde.org>
Sun, 14 Feb 2010 16:48:06 +0000 (11:48 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Sun, 14 Feb 2010 16:51:05 +0000 (11:51 -0500)
Add Iterator interface to Horde_Image_Base and implement it in Horde_Image_Imagick. Other
backend support to come.  Allows iterating through all images in a multi-page image such
as TIFF or animatted GIFs

This also goes towards a solution for Request: 6022

framework/Image/lib/Horde/Image/Base.php
framework/Image/lib/Horde/Image/Imagick.php

index 037ff2d..3313dc1 100644 (file)
@@ -16,7 +16,7 @@
  * @TODO: - Can we depend on the Horde_Util:: class or some other solution needed?
  *        - Exceptions
  */
-class Horde_Image_Base
+class Horde_Image_Base Implements Iterator
 {
     /**
      * Background color.
@@ -418,4 +418,33 @@ class Horde_Image_Base
         }
     }
 
+    /**
+     * Iterator interface
+     */
+    public function rewind()
+    {
+
+    }
+
+    public function current()
+
+    {
+
+    }
+
+    public function key()
+    {
+
+    }
+
+    public function next()
+    {
+
+    }
+
+    public function valid()
+    {
+
+    }
+
 }
index d6a9603..082d8cb 100644 (file)
  */
 class Horde_Image_Imagick extends Horde_Image_Base
 {
+    /**
+     * The underlaying Imagick object
+     *
+     * @var Imagick
+     */
     protected $_imagick;
 
     /**
+     * Flag for iterator, since calling nextImage on Imagick would result in a
+     * fatal error if there are no more images.
+     *
+     * @var boolean
+     */
+    private $_noMoreImages = false;
+
+    /**
      * Capabilites of this driver.
      *
      * @var array
@@ -125,6 +138,7 @@ class Horde_Image_Imagick extends Horde_Image_Base
     {
         parent::reset();
         $this->_imagick->clear();
+        $this->_noMoreImages = false;
     }
 
     /**
@@ -520,4 +534,64 @@ class Horde_Image_Imagick extends Horde_Image_Base
         $border->destroy();
     }
 
+    /**
+     * Reset the imagick iterator to the first image in the set.
+     *
+     * @return void
+     */
+    public function rewind()
+    {
+        $this->_logDebug('Horde_Image_Imagick#rewind');
+        $this->_imagick->setFirstIterator();
+        $this->_noMoreImages = false;
+    }
+
+    /**
+     * Return the current image from the internal iterator.
+     *
+     * @return Horde_Image_Imagick
+     */
+    public function current()
+    {
+        $this->_logDebug('Horde_Image_Imagick#current');
+        $params = array('data' => $this->raw());
+        $context = array('tmpdir' => $this->_tmpdir,
+                         'logger' => $this->_logger);
+        $image = new Horde_Image_Imagick($params, $context);
+        $this->_logDebug(print_r($image, true));
+        return $image;
+    }
+
+    /**
+     * Get the index of the internal iterator.
+     *
+     * @return integer
+     */
+    public function key()
+    {
+        $this->_logDebug('Horde_Image_Imagick#key: ' . $this->_imagick->getIteratorIndex());
+        return $this->_imagick->getIteratorIndex();
+    }
+
+    /**
+     * Advance the iterator
+     *
+     * @return Horde_Image_Imagick
+     */
+    public function next()
+    {
+        if ($this->_imagick->hasNextImage()) {
+            $this->_imagick->nextImage();
+            return $this->current();
+        } else {
+            $this->_noMoreImages = true;
+            return false;
+        }
+    }
+
+    public function valid()
+    {
+        $this->_logDebug('Horde_Image_Imagick#valid:' . print_r(!$this->moreImages, true));
+        return !$this->_noMoreImages;
+    }
  }