Add a square thumbnail generator
authorMichael J. Rubinsky <mrubinsk@horde.org>
Sat, 25 Dec 2010 05:44:25 +0000 (00:44 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Sun, 26 Dec 2010 05:41:20 +0000 (00:41 -0500)
ansel/lib/ImageGenerator.php
ansel/lib/ImageGenerator/SquareThumb.php [new file with mode: 0644]

index 288a2f0..a2bf8ed 100644 (file)
  * See the enclosed file COPYING for license information (GPL). If you
  * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
  *
- * Copyright 2007-2010 The Horde Project (http://www.horde.org/)
- *
- * See the enclosed file COPYING for license information (GPL). If you
- * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
- *
  * @author Michael J. Rubinsky <mrubinsk@horde.org>
  * @package Ansel
  */
diff --git a/ansel/lib/ImageGenerator/SquareThumb.php b/ansel/lib/ImageGenerator/SquareThumb.php
new file mode 100644 (file)
index 0000000..9b961ca
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+/**
+ * ImageGenerator to create a square thumbnail.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author Michael J. Rubinsky <mrubinsk@horde.org>
+ * @package Ansel
+ */
+class Ansel_ImageGenerator_SquareThumb extends Ansel_ImageGenerator
+{
+    public function __construct($params)
+    {
+        parent::__construct($params);
+        $this->title = _("Square Thumbnails");
+    }
+
+    /**
+     *
+     * @return Horde_Image
+     */
+    protected function _create()
+    {
+        // Take the largest requested dimension
+        if (empty($this->_dimensions['width'])) {
+            $size = max($GLOBALS['conf']['thumbnail']['height'], $GLOBALS['conf']['thumbnail']['width']);
+        } else {
+            $size = max($this->_dimensions['width'], $this->_dimensions['height']);
+        }
+
+        // Use smartcrop algorithm if we have it, otherwise a plain center crop.
+        if (Ansel::isAvailable('SmartCrop')) {
+            $this->_image->addEffect('SmartCrop', array('width' => $size, 'height' => $size));
+        } else {
+            $this->_image->addEffect('CenterCrop', array('width' => $size, 'height' => $size));
+        }
+        $this->_image->applyEffects();
+
+        if ($GLOBALS['conf']['thumbnail']['unsharp'] && Ansel::isAvailable('Unsharpmask')) {
+            try {
+                $this->_image->addEffect('Unsharpmask',
+                                         array('radius' => $GLOBALS['conf']['thumbnail']['radius'],
+                                               'threshold' => $GLOBALS['conf']['thumbnail']['threshold'],
+                                               'amount' => $GLOBALS['conf']['thumbnail']['amount']));
+                $this->_image->applyEffects();
+            } catch (Horde_Image_Exception $e) {
+                throw new Ansel_Exception($e);
+            }
+        }
+
+        return $this->_image->getHordeImage();
+    }
+
+}