Always load the image before iterating, but make sure it's not already loaded first.
authorMichael J. Rubinsky <mrubinsk@horde.org>
Fri, 19 Feb 2010 16:03:44 +0000 (11:03 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Fri, 19 Feb 2010 16:03:44 +0000 (11:03 -0500)
If we attempt to call #load() twice, it will reload the raw data into Horde_Image,
thus resetting the iterator and causing an infinite loop. Also, use consistent method names.

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

index 9513cf7..9ad0982 100644 (file)
@@ -233,7 +233,7 @@ class Ansel_Gallery extends Horde_Share_Object_sql_hierarchical
 
         /* Check for a supported multi-page image */
         if ($image->isMultiPage() === true) {
-            $params['name'] = $image->getPageCount() . ' page image: ' . $image->filename;
+            $params['name'] = $image->getImagePageCount() . ' page image: ' . $image->filename;
             $mGallery = $GLOBALS['ansel_storage']->createGallery($params, $this->getPermission(), $this->getId());
             $i = 1;
             foreach ($image as $page) {
index 1db6750..a34fddc 100644 (file)
@@ -1271,8 +1271,12 @@ class Ansel_Image Implements Iterator
      * @return integer  The number of pages.
      * @throws Ansel_Exception
      */
-    public function getPageCount()
+    public function getImagePageCount()
     {
+        if (empty($this->_loaded['full'])) {
+            $this->load();
+        }
+
         try {
             return $this->_image->getImagePageCount();
         } catch (Horde_Image_Exception $e) {
@@ -1288,7 +1292,9 @@ class Ansel_Image Implements Iterator
      */
     public function rewind()
     {
-        $this->load();
+        if (empty($this->_loaded['full'])) {
+            $this->load();
+        }
         try {
             $this->_image->rewind();
         } catch (Horde_Image_Exception $e) {
@@ -1303,8 +1309,14 @@ class Ansel_Image Implements Iterator
      */
     public function current()
     {
-        $this->load();
-        return $this->_buildImageObject($this->_image->current());
+        if (empty($this->_loaded['full'])) {
+            $this->load();
+        }
+        try {
+            return $this->_buildImageObject($this->_image->current());
+        } catch (Horde_Image_Exception $e) {
+            throw new Ansel_Exception($e);
+        }
     }
 
     /**
@@ -1315,7 +1327,9 @@ class Ansel_Image Implements Iterator
      */
     public function key()
     {
-        $this->load();
+        if (empty($this->_loaded['full'])) {
+            $this->load();
+        }
         try {
             return $this->_image->key();
         } catch (Horde_Image_Exception $e) {
@@ -1330,7 +1344,9 @@ class Ansel_Image Implements Iterator
      */
     public function next()
     {
-        $this->load();
+        if (empty($this->_loaded['full'])) {
+            $this->load();
+        }
         if ($next = $this->_image->next()) {
             return $this->_buildImageObject($next);
         }
@@ -1346,7 +1362,9 @@ class Ansel_Image Implements Iterator
      */
     public function valid()
     {
-        $this->load();
+        if (empty($this->_loaded['full'])) {
+            $this->load();
+        }
         try {
             return $this->_image->valid();
         } catch (Horde_Image_Exception $e) {