From 0fa67a14433c5f7488ca0da5213df290e8c128a7 Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Tue, 16 Feb 2010 16:37:06 -0500 Subject: [PATCH] Support for multipage images. 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 | 40 ++++++++++++ ansel/lib/Image.php | 170 ++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 171 insertions(+), 39 deletions(-) diff --git a/ansel/lib/Gallery.php b/ansel/lib/Gallery.php index 2e892d2ea..2ea62f7b0 100644 --- a/ansel/lib/Gallery.php +++ b/ansel/lib/Gallery.php @@ -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); diff --git a/ansel/lib/Image.php b/ansel/lib/Image.php index 4b2af48c0..a4af37e8f 100644 --- a/ansel/lib/Image.php +++ b/ansel/lib/Image.php @@ -11,63 +11,59 @@ * @author Michael J. Rubinsky * @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; + } + } -- 2.11.0