Support for multipage images.
authorMichael J. Rubinsky <mrubinsk@horde.org>
Tue, 16 Feb 2010 21:37:06 +0000 (16:37 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Tue, 16 Feb 2010 21:41:13 +0000 (16:41 -0500)
Adding a multipage TIFF image will now create a new subgallery in the current
gallery, with each page added as an image to that gallery. This completes
Request: #6022

ansel/lib/Gallery.php
ansel/lib/Image.php

index 2e892d2..2ea62f7 100644 (file)
@@ -176,6 +176,30 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
     }
 
     /**
+     * Adds an Ansel_Image object to this gallery.
+     *
+     * @param $image Ansel_Image
+     */
+    public function addImageObject(Ansel_Image $image, $default = false)
+    {
+        // Make sure it's taken as a new image
+        $image->id = null;
+        $image->gallery = $this->getId();
+        $image->sort = $this->countImages();
+        $image->save();
+        $this->updateImageCount(1);
+
+        /* Should this be the default image? */
+        $this->data['attribute_default'] = $image->id;
+        $this->clearStacks();
+
+        /* Save all changes to the gallery */
+        $this->save();
+
+        return $image->id();
+    }
+
+    /**
      * Add an image to this gallery.
      *
      * @param array $image_data  The image to add. Required keys include
@@ -206,6 +230,22 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
 
         /* Create the image object */
         $image = new Ansel_Image($image_data);
+
+        /* Check for a supported multi-page image */
+        if ($image->isMultiPage() === true) {
+            $params['name'] = $image->getPageCount() . ' page image: ' . $image->filename;
+            $mGallery = $GLOBALS['ansel_storage']->createGallery($params, $this->getPermission(), $this->getId());
+            $i = 1;
+            foreach ($image as $page) {
+                $page->caption = sprintf(_("Page %d"), $i++);
+                $mGallery->addImageObject($page);
+            }
+            $mGallery->save();
+
+            return $page->id;
+        }
+
+        /* If this was a single, normal image, continue */
         $result = $image->save();
         if (empty($image_data['image_id'])) {
             $this->updateImageCount(1);
index 4b2af48..a4af37e 100644 (file)
  * @author Michael J. Rubinsky <mrubinsk@horde.org>
  * @package Ansel
  */
-class Ansel_Image
+class Ansel_Image Implements Iterator
 {
     /**
      * @var integer  The gallery id of this image's parent gallery
      */
-    var $gallery;
+    public $gallery;
 
     /**
-     * @var Horde_Image  Horde_Image object for this image.
+     * @var Horde_Image_Base  Horde_Image object for this image.
      */
-    var $_image;
-
-    var $id = null;
-    var $filename = 'Untitled';
-    var $caption = '';
-    var $type = 'image/jpeg';
+    public $_image;
+    protected $_dirty;
+    protected $_loaded = array();
+    protected $_data = array();
+    /**
+     * Holds an array of tags for this image
+     * @var array
+     */
+    protected $_tags = array();
 
     /**
-     * timestamp of uploaded date
+     * Cache the raw EXIF data locally
      *
-     * @var integer
+     * @var array
      */
-    var $uploaded;
-
-    var $sort;
-    var $commentCount;
-    var $facesCount;
-    var $lat;
-    var $lng;
-    var $location;
-    var $geotag_timestamp;
-
-    var $_dirty;
+    protected $_exif = array();
 
+    public $id = null;
+    public $filename = 'Untitled';
+    public $caption = '';
+    public $type = 'image/jpeg';
 
     /**
-     * Timestamp of original date.
+     * timestamp of uploaded date
      *
      * @var integer
      */
-    var $originalDate;
+    public $uploaded;
 
-    /**
-     * Holds an array of tags for this image
-     * @var array
-     */
-    var $_tags = array();
-
-    var $_loaded = array();
-    var $_data = array();
+    public $sort;
+    public $commentCount;
+    public $facesCount;
+    public $lat;
+    public $lng;
+    public $location;
+    public $geotag_timestamp;
 
     /**
-     * Cache the raw EXIF data locally
+     * Timestamp of original date.
      *
-     * @var array
+     * @var integer
      */
-    var $_exif = array();
+    public $originalDate;
 
     /**
      * TODO: refactor Ansel_Image to use a ::get() method like Ansel_Gallery
@@ -76,13 +72,22 @@ class Ansel_Image
      * @param unknown_type $image
      * @return Ansel_Image
      */
-    function Ansel_Image($image = array())
+    public function __construct($image = array())
     {
         if ($image) {
             $this->filename = $image['image_filename'];
-            $this->caption = $image['image_caption'];
-            $this->sort = $image['image_sort'];
-            $this->gallery = $image['gallery_id'];
+
+            if  (!empty($image['gallery_id'])) {
+                $this->gallery = $image['gallery_id'];
+            }
+
+            if (!empty($image['image_caption'])) {
+                $this->caption = $image['image_caption'];
+            }
+
+            if (isset($image['image_sort'])) {
+                $this->sort = $image['image_sort'];
+            }
 
             // New image?
             if (!empty($image['image_id'])) {
@@ -1075,4 +1080,91 @@ class Ansel_Image
         return $output;
     }
 
+    /**
+     * Indicates if this image represents a multipage image.
+     *
+     * @return boolean
+     */
+    public function isMultiPage()
+    {
+        $this->load();
+        return $this->_image->getImagePageCount() > 1;
+    }
+
+    public function getPageCount()
+    {
+        return $this->_image->getImagePageCount();
+    }
+
+    /**
+     * Reset the iterator to the first image in the set.
+     *
+     * @return void
+     */
+    public function rewind()
+    {
+        $this->load();
+        $this->_image->rewind();
+    }
+
+    /**
+     * Return the current image from the internal iterator.
+     *
+     * @return Horde_Image_Imagick
+     */
+    public function current()
+    {
+        $this->load();
+        return $this->_buildImageObject($this->_image->current());
+    }
+
+    /**
+     * Get the index of the internal iterator.
+     *
+     * @return integer
+     */
+    public function key()
+    {
+        $this->load();
+        return $this->_image->key();
+    }
+
+    /**
+     * Advance the iterator
+     *
+     * @return Horde_Image_Im
+     */
+    public function next()
+    {
+        $this->load();
+        if ($next = $this->_image->next()) {
+            return $this->_buildImageObject($next);
+        }
+
+        return false;
+    }
+
+    /**
+     * Deterimines if the current iterator item is valid.
+     *
+     * @return boolean
+     */
+    public function valid()
+    {
+        $this->load();
+        return $this->_image->valid();
+    }
+
+    protected function _buildImageObject(Horde_Image_Base $image)
+    {
+        $params = array(
+            'image_filename' => $this->filename,
+            'data' => $image->raw(),
+        );
+
+        $newImage = new Ansel_Image($params);
+
+        return $newImage;
+    }
+
 }