From: Michael J. Rubinsky Date: Sat, 20 Feb 2010 21:11:31 +0000 (-0500) Subject: Allow setting the image type directly from client code X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=8e39e7530a6d6323657d7e94ccd1c2869bf4a30f;p=horde.git Allow setting the image type directly from client code --- diff --git a/ansel/lib/Image.php b/ansel/lib/Image.php index 696de4a9a..343b0cbd6 100644 --- a/ansel/lib/Image.php +++ b/ansel/lib/Image.php @@ -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. */ diff --git a/framework/Image/lib/Horde/Image/Base.php b/framework/Image/lib/Horde/Image/Base.php index 0f9b74874..68548030a 100644 --- a/framework/Image/lib/Horde/Image/Base.php +++ b/framework/Image/lib/Horde/Image/Base.php @@ -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. diff --git a/framework/Image/lib/Horde/Image/Imagick.php b/framework/Image/lib/Horde/Image/Imagick.php index 3c4ef31d7..549bed30b 100644 --- a/framework/Image/lib/Horde/Image/Imagick.php +++ b/framework/Image/lib/Horde/Image/Imagick.php @@ -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. * diff --git a/framework/Image/tests/im.php b/framework/Image/tests/im.php index a9425c31e..175ecad56 100644 --- a/framework/Image/tests/im.php +++ b/framework/Image/tests/im.php @@ -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')); diff --git a/framework/Image/tests/runtest.php b/framework/Image/tests/runtest.php index 69b712930..e9cb656fb 100644 --- a/framework/Image/tests/runtest.php +++ b/framework/Image/tests/runtest.php @@ -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.',