From: Michael J. Rubinsky Date: Mon, 25 Oct 2010 13:28:41 +0000 (-0400) Subject: Add Horde_Core_Factory_Image, and refactor to use it. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=30df656fbd7abe6f4896e14365c3203523da3998;p=horde.git Add Horde_Core_Factory_Image, and refactor to use it. --- diff --git a/ansel/lib/Ansel.php b/ansel/lib/Ansel.php index b5792a901..5e7326e6f 100644 --- a/ansel/lib/Ansel.php +++ b/ansel/lib/Ansel.php @@ -448,19 +448,9 @@ class Ansel */ static public function getImageObject($params = array()) { - global $conf; - $context = array('tmpdir' => Horde::getTempDir(), - 'logger' => $GLOBALS['injector']->getInstance('Horde_Log_Logger')); - if (!empty($conf['image']['convert'])) { - $context['convert'] = $conf['image']['convert']; - $context['identify'] = $conf['image']['identify']; - } - $params = array_merge(array('type' => $conf['image']['type'], - 'context' => $context), - $params); - - $driver = $conf['image']['driver']; - return Horde_Image::factory($driver, $params); + return $GLOBALS['injector'] + ->getInstance('Horde_Core_Factory_Image') + ->create(array('type' => $GLOBALS['conf']['image']['type'])); } /** diff --git a/folks/lib/Driver.php b/folks/lib/Driver.php index 03399d7ee..e803adab9 100644 --- a/folks/lib/Driver.php +++ b/folks/lib/Driver.php @@ -74,18 +74,10 @@ class Folks_Driver { $p = hash('md5', $user); $vfspath = Folks::VFS_PATH . '/' . substr(str_pad($p, 2, 0, STR_PAD_LEFT), -2) . '/'; $vfs_name = $p . '.' . $conf['images']['image_type']; - $driver = $conf['image']['driver']; - $context = array('tmpdir' => Horde::getTempDir()); - if (!empty($conf['image']['convert'])) { - $context['convert'] = $conf['image']['convert']; - $context['identify'] = $conf['image']['identify']; - } - $img = Horde_Image::factory($driver, - array('type' => $conf['images']['image_type'], - 'context' => $context)); try { + $img = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Image')->create(array('type' => $conf['images']['image_type'])); $result = $img->loadFile($file); - } catch (Horde_Image_Exception $e) { + } catch (Horde_Exception $e) { throw new Horde_Exception_Prior($e); } $dimensions = $img->getDimensions(); diff --git a/framework/Core/lib/Horde/Core/Factory/Image.php b/framework/Core/lib/Horde/Core/Factory/Image.php new file mode 100644 index 000000000..19ed43a8f --- /dev/null +++ b/framework/Core/lib/Horde/Core/Factory/Image.php @@ -0,0 +1,53 @@ +_injector = $injector; + } + + /** + * Returns an appropriate Horde_Image object based on Horde's configuration. + * + * @param array $params An array of image parameters. @see Horde_Image_Base + * @return Horde_Image + * @throws Horde_Exception + */ + public function create(array $params = array()) + { + global $conf; + + $driver = $conf['image']['driver']; + $context = array( + 'tmpdir' => Horde::getTempdir(), + 'logger' => $this->_injector->getInstance('Horde_Log_Logger')); + if ($driver == 'Im') { + $context['convert'] = $conf['image']['convert']; + $context['identify'] = $conf['image']['identify']; + } + // Use the default + $class = 'Horde_Image_' . $driver; + if (class_exists($class)) { + return new $class($params, $context); + } else { + throw new Horde_Exception('Invalid Image driver specified: ' . $class . ' not found.'); + } + } + +} \ No newline at end of file diff --git a/framework/Core/package.xml b/framework/Core/package.xml index 5b41ed2b7..761282035 100644 --- a/framework/Core/package.xml +++ b/framework/Core/package.xml @@ -23,8 +23,8 @@ Application Framework. slusarz@horde.org yes - 2010-10-22 - + 2010-10-25 + 0.1.0 0.1.0 @@ -137,6 +137,7 @@ Application Framework. + @@ -714,6 +715,7 @@ Application Framework. + @@ -901,7 +903,7 @@ Initial packaging beta beta - 2010-10-22 + 2010-10-25 LGPL * Add Horde_Session. diff --git a/framework/Image/lib/Horde/Image/Base.php b/framework/Image/lib/Horde/Image/Base.php index 1a7c27f10..3f1d858de 100644 --- a/framework/Image/lib/Horde/Image/Base.php +++ b/framework/Image/lib/Horde/Image/Base.php @@ -96,7 +96,10 @@ abstract class Horde_Image_Base extends EmptyIterator * (optional)data - The image binary data. * * @param array $context The object context - configuration, injected objects - * + *
+     *   (required)tmpdir - Temporary directory
+     *   (optional)logger - The logger
+     *
* @throws InvalidArgumentException */ protected function __construct($params, $context = array()) @@ -117,8 +120,8 @@ abstract class Horde_Image_Base extends EmptyIterator } if (!empty($params['type'])) { // We only want the extension, not the full mimetype. - if (strpos($type, 'image/') !== false) { - $type = substr($type, 6); + if (strpos($params['type'], 'image/') !== false) { + $params['type'] = substr($params['type'], 6); } $this->_type = $params['type']; } diff --git a/framework/Image/lib/Horde/Image/Effect/Im/PhotoStack.php b/framework/Image/lib/Horde/Image/Effect/Im/PhotoStack.php index 15a7f2f5e..334fab5cf 100644 --- a/framework/Image/lib/Horde/Image/Effect/Im/PhotoStack.php +++ b/framework/Image/lib/Horde/Image/Effect/Im/PhotoStack.php @@ -154,7 +154,7 @@ class Horde_Image_Effect_Im_PhotoStack extends Horde_Image_Effect 'convert' => $this->_image->getConvertPath()); $size = $image->getDimensions(); - $new = Horde_Image::factory('im', array('data' => $image->raw(), 'context' => $context)); + $new = new Horde_Image_Im(array('data' => $image->raw()), $context); $new->addEffect('RoundCorners', array('border' => 2, 'bordercolor' => '#111', 'background' => 'none')); $new->applyEffects(); diff --git a/framework/Image/lib/Horde/Image/Effect/Imagick/PhotoStack.php b/framework/Image/lib/Horde/Image/Effect/Imagick/PhotoStack.php index 078965b30..bcea72a1a 100644 --- a/framework/Image/lib/Horde/Image/Effect/Imagick/PhotoStack.php +++ b/framework/Image/lib/Horde/Image/Effect/Imagick/PhotoStack.php @@ -198,7 +198,7 @@ class Horde_Image_Effect_Imagick_PhotoStack extends Horde_Image_Effect { $context = array('tmpdir' => $this->_image->getTmpDir()); $size = $image->getImageGeometry(); - $new = Horde_Image::factory('Imagick', array('context' => $context)); + $new = new Horde_Image_Imagick(array(), $context); $new->loadString($image->getImageBlob()); $image->destroy(); $new->addEffect('RoundCorners', array('border' => 2, 'bordercolor' => '#111')); diff --git a/framework/Image/tests/im.php b/framework/Image/tests/im.php index 81e312ad8..e88ceb602 100644 --- a/framework/Image/tests/im.php +++ b/framework/Image/tests/im.php @@ -485,9 +485,8 @@ function getImageObject($params = array()) 'convert' => $GLOBALS['convert'], 'logger' => $GLOBALS['injector']->getInstance('Horde_Log_Logger'), 'identify' => $GLOBALS['identify']); - $params['context'] = $context; - return Horde_Image::factory($GLOBALS['driver'], $params); + return new Horde_Image_Im($params, $context); } function logThis($effect, $time, $memory) diff --git a/horde/services/images/view.php b/horde/services/images/view.php index 2bdb78109..baa26c2e4 100644 --- a/horde/services/images/view.php +++ b/horde/services/images/view.php @@ -68,17 +68,12 @@ case 'tmp': } /* Load the image object. */ -$context = array('tmpdir' => Horde::getTempDir()); -$driver = $conf['image']['driver']; -if (!empty($conf['image']['convert'])) { - $context['convert'] = $conf['image']['convert']; - $context['identify'] = $conf['image']['identify']; -} $type = Horde_Mime_Magic::analyzeData($file_data); -$image = Horde_Image::factory($driver, - array('context' => $context)); -$image->loadString($file_data); -$image->setType($type); +$image = $GLOBALS['injector'] + ->getInstance('Horde_Core_Factory_Image') + ->create(array('type' => $type, + 'data' => $file_data)); + /* Check if no editing action required and send the image to browser. */ if (empty($action)) { $image->display(); diff --git a/horde/util/barcode.php b/horde/util/barcode.php index 721d0d103..c0b1fc88a 100644 --- a/horde/util/barcode.php +++ b/horde/util/barcode.php @@ -73,14 +73,13 @@ $codingmap = array( '/' => '010100010' ); -$image = Horde_Image::factory($vars->get('type', 'Png'), array( +$image = new Horde_Image_Png(array( 'background' => 'white', 'context' => array( 'tmpdir' => Horde::getTempDir() ), 'height' => $height, - 'width' => $width -)); + 'width' => $width)); $xpos = 0; for ($idx = 0; $idx < $textlen; ++$idx) { diff --git a/imp/lib/Mime/Viewer/Images.php b/imp/lib/Mime/Viewer/Images.php index f14baa080..09958d8d6 100644 --- a/imp/lib/Mime/Viewer/Images.php +++ b/imp/lib/Mime/Viewer/Images.php @@ -265,12 +265,8 @@ EOD; //@TODO: Pass in a Horde_Logger in $context if desired. $context = array('tmpdir' => Horde::getTempDir()); try { - if (!empty($GLOBALS['conf']['image']['convert'])) { - $context['convert'] = $GLOBALS['conf']['image']['convert']; - $context['identify'] = $GLOBALS['conf']['image']['identify']; - } - $img = Horde_Image::factory($GLOBALS['conf']['image']['driver'], array('context' => $context)); - } catch (Horde_Image_Exception $e) { + $img = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Image')->create(); + } catch (Horde_Exception $e) { return false; } diff --git a/imp/lib/Mime/Viewer/Pdf.php b/imp/lib/Mime/Viewer/Pdf.php index 2ec340e23..5aa44ed45 100644 --- a/imp/lib/Mime/Viewer/Pdf.php +++ b/imp/lib/Mime/Viewer/Pdf.php @@ -113,14 +113,9 @@ class IMP_Mime_Viewer_Pdf extends Horde_Mime_Viewer_Pdf return false; } - $context = array('tmpdir' => Horde::getTempdir()); - if (!empty($GLOBALS['conf']['image']['convert'])) { - $context['convert'] = $GLOBALS['conf']['image']['convert']; - $context['identify'] = $GLOBALS['conf']['image']['identify']; - } try { - $img = Horde_Image::factory($GLOBALS['conf']['image']['driver'], array('context' => $context)); - } catch (Horde_Image_Exception $e) { + $img = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Image')->create(); + } catch (Horde_Exception $e) { return false; } if (!$img->hasCapability('multipage') && !$img->hasCapability('pdf')) {