Add Horde_Core_Factory_Image, and refactor to use it.
authorMichael J. Rubinsky <mrubinsk@horde.org>
Mon, 25 Oct 2010 13:28:41 +0000 (09:28 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Mon, 25 Oct 2010 13:29:23 +0000 (09:29 -0400)
12 files changed:
ansel/lib/Ansel.php
folks/lib/Driver.php
framework/Core/lib/Horde/Core/Factory/Image.php [new file with mode: 0644]
framework/Core/package.xml
framework/Image/lib/Horde/Image/Base.php
framework/Image/lib/Horde/Image/Effect/Im/PhotoStack.php
framework/Image/lib/Horde/Image/Effect/Imagick/PhotoStack.php
framework/Image/tests/im.php
horde/services/images/view.php
horde/util/barcode.php
imp/lib/Mime/Viewer/Images.php
imp/lib/Mime/Viewer/Pdf.php

index b5792a9..5e7326e 100644 (file)
@@ -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']));
     }
 
     /**
index 03399d7..e803ada 100644 (file)
@@ -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 (file)
index 0000000..19ed43a
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+/**
+ * @category Horde
+ * @package  Core
+ */
+class Horde_Core_Factory_Image
+{
+    /**
+     * The injector.
+     *
+     * @var Horde_Injector
+     */
+    private $_injector;
+
+    /**
+     * Constructor.
+     *
+     * @param Horde_Injector $injector  The injector to use.
+     */
+    public function __construct(Horde_Injector $injector)
+    {
+        $this->_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
index 5b41ed2..7612820 100644 (file)
@@ -23,8 +23,8 @@ Application Framework.</description>
   <email>slusarz@horde.org</email>
   <active>yes</active>
  </developer>
- <date>2010-10-22</date>
- <time>13:14:28</time>
+ <date>2010-10-25</date>
+ <time>09:15:23</time>
  <version>
   <release>0.1.0</release>
   <api>0.1.0</api>
@@ -137,6 +137,7 @@ Application Framework.</description>
        <file name="History.php" role="php" />
        <file name="HttpClient.php" role="php" />
        <file name="Identity.php" role="php" />
+       <file name="Image.php" role="php" />
        <file name="Imple.php" role="php" />
        <file name="KolabServer.php" role="php" />
        <file name="KolabSession.php" role="php" />
@@ -714,6 +715,7 @@ Application Framework.</description>
    <install as="Horde/Core/Factory/History.php" name="lib/Horde/Core/Factory/History.php" />
    <install as="Horde/Core/Factory/HttpClient.php" name="lib/Horde/Core/Factory/HttpClient.php" />
    <install as="Horde/Core/Factory/Identity.php" name="lib/Horde/Core/Factory/Identity.php" />
+   <install as="Horde/Core/Factory/Image.php" name="lib/Horde/Core/Factory/Image.php" />
    <install as="Horde/Core/Factory/Imple.php" name="lib/Horde/Core/Factory/Imple.php" />
    <install as="Horde/Core/Factory/KolabServer.php" name="lib/Horde/Core/Factory/KolabServer.php" />
    <install as="Horde/Core/Factory/KolabSession.php" name="lib/Horde/Core/Factory/KolabSession.php" />
@@ -901,7 +903,7 @@ Initial packaging
     <release>beta</release>
     <api>beta</api>
    </stability>
-   <date>2010-10-22</date>
+   <date>2010-10-25</date>
    <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
    <notes>
 * Add Horde_Session.
index 1a7c27f..3f1d858 100644 (file)
@@ -96,7 +96,10 @@ abstract class Horde_Image_Base extends EmptyIterator
      *   (optional)data   - The image binary data.
      *</pre>
      * @param array $context  The object context - configuration, injected objects
-     *
+     *<pre>
+     *   (required)tmpdir - Temporary directory
+     *   (optional)logger - The logger
+     *</pre>
      * @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'];
         }
index 15a7f2f..334fab5 100644 (file)
@@ -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();
 
index 078965b..bcea72a 100644 (file)
@@ -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'));
index 81e312a..e88ceb6 100644 (file)
@@ -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)
index 2bdb781..baa26c2 100644 (file)
@@ -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();
index 721d0d1..c0b1fc8 100644 (file)
@@ -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) {
index f14baa0..09958d8 100644 (file)
@@ -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;
         }
 
index 2ec340e..5aa44ed 100644 (file)
@@ -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')) {