Allow setting the image type directly from client code
authorMichael J. Rubinsky <mrubinsk@horde.org>
Sat, 20 Feb 2010 21:11:31 +0000 (16:11 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Sat, 20 Feb 2010 21:11:31 +0000 (16:11 -0500)
ansel/lib/Image.php
framework/Image/lib/Horde/Image/Base.php
framework/Image/lib/Horde/Image/Imagick.php
framework/Image/tests/im.php
framework/Image/tests/runtest.php

index 696de4a..343b0cb 100644 (file)
@@ -372,14 +372,9 @@ class Ansel_Image Implements Iterator
      */
     public function createView($view, $style = null)
     {
-        // HACK: Need to replace the image object with a JPG typed image if
-        //       we are generating a screen image. Need to do the replacement
-        //       and do it *here* for BC reasons with Horde_Image...and this
-        //       needs to be done FIRST, since the view might already be cached
-        //       in the VFS.
+        /* Force screen images to ALWAYS be jpegs for performance/size */
         if ($view == 'screen' && $GLOBALS['conf']['image']['type'] != 'jpeg') {
-            $this->_image = Ansel::getImageObject(array('type' => 'jpeg'));
-            $this->_image->reset();
+            $this->_image->setType('jpeg');
         }
 
         /* Get the VFS info. */
index 0f9b748..6854803 100644 (file)
@@ -170,6 +170,18 @@ abstract class Horde_Image_Base Implements Iterator
     }
 
     /**
+     * Setter for the image type.
+     *
+     * @param string $type  The simple type for the imag (png, jpg, etc...)
+     *
+     * @return void
+     */
+    public function setType($type)
+    {
+        $this->_type = $type;
+    }
+
+    /**
      * Draw a shaped point at the specified (x,y) point. Useful for
      * scatter diagrams, debug points, etc. Draws squares, circles,
      * diamonds, and triangles.
index 3c4ef31..549bed3 100644 (file)
@@ -54,7 +54,7 @@ class Horde_Image_Imagick extends Horde_Image_Base
         if (!Horde_Util::loadExtension('imagick')) {
             throw new Horde_Image_Exception('Required PECL Imagick extension not found.');
         }
-        parent::__construct($params, $context);   
+        parent::__construct($params, $context);
         ini_set('imagick.locale_fix', 1);
         $this->_imagick = new Imagick();
         if (!empty($params['filename'])) {
@@ -71,7 +71,11 @@ class Horde_Image_Imagick extends Horde_Image_Base
             }
         }
 
-        $this->_imagick->setImageFormat($this->_type);
+        try {
+            $this->_imagick->setImageFormat($this->_type);
+        } catch (ImagickException $e) {
+            throw new Horde_Image_Exception($e);
+        }
     }
 
     /**
@@ -88,12 +92,12 @@ class Horde_Image_Imagick extends Horde_Image_Base
         $this->_imagick->clear();
         try {
             $this->_imagick->readImageBlob($this->_data);
+            $this->_imagick->setImageFormat($this->_type);
+            $this->_imagick->setIteratorIndex(0);
         } catch (ImagickException $e) {
             throw new Horde_Image_Exception($e);
         }
-        $this->_imagick->setImageFormat($this->_type);
         unset($this->_data);
-        $this->_imagick->setIteratorIndex(0);
     }
 
     /**
@@ -112,14 +116,29 @@ class Horde_Image_Imagick extends Horde_Image_Base
         $this->_imagick->clear();
         try {
             $this->_imagick->readImageBlob($this->_data);
+            $this->_imagick->setImageFormat($this->_type);
+            $this->_imagick->setIteratorIndex(0);
         } catch (ImagickException $e) {
             throw new Horde_Image_Exception($e);
         }
-        $this->_imagick->setImageFormat($this->_type);
-        $this->_imagick->setIteratorIndex(0);
         unset($this->_data);
     }
 
+    /**
+     * Set the image type
+     *
+     * @see Horde_Image_Base::setType()
+     */
+    public function setType($type)
+    {
+        parent::setType($type);
+        try {
+            $this->_imagick->setImageFormat($this->_type);
+        } catch (ImagickException $e) {
+            // Don't care about an empty wand here.
+        }
+    }
+
     /*
      * Return the raw image data.
      *
index a9425c3..175ecad 100644 (file)
@@ -56,6 +56,24 @@ case 'testInitialStateAfterLoad':
     $image->display();
     break;
 
+case 'testDefaultImageFormatDuringLoad':
+    // Tests image format during load
+    $image = getImageObject(array('filename' => 'img1.jpg'));
+    $image->display();
+    break;
+
+case 'testForceImageFormatDuringLoad':
+    // Tests forcing image format during load
+    $image = getImageObject(array('filename' => 'img1.jpg', 'type' => 'jpeg'));
+    $image->display();
+    break;
+case 'testChangeImageFormatAfterLoad':
+    // Tests changing image format after load
+    $image = getImageObject(array('filename' => 'img1.jpg')); // Loads as PNG
+    $image->setType('jpeg');
+    $image->display();
+    break;
+
 case 'testResize':
     $time = xdebug_time_index();
     $image = getImageObject(array('filename' => 'img2.jpg'));
index 69b7129..e9cb656 100644 (file)
@@ -8,6 +8,9 @@ Horde_Registry::appInit('horde', array('authentication' => 'none'));
 
 $allTests = array(
     'testInitialState' => 'Test initial state. Solid blue square',
+    'testDefaultImageFormatDuringLoad' => 'Should load as default image type of PNG even though source file is JPG',
+    'testForceImageFormatDuringLoad' => 'Forces image format to JPG during loadFile (Default is PNG)',
+    'testChangeImageFormatAfterLoad' => 'Changes image format after loaded from file (Should be JPG)',
     'testPrimitivesTransparentBG' => 'Transparent background, various primitives. Cirlce should be above the rectangles.',
     'testTransparentBGWithBorder' => 'Test transparent background with border preserving transparency.',
     'testTransparentPrimitivesReversed' => 'Test ordering of primitives. This should show the circle *below* the rectangles.',