From 4aa85e6a50206e4043ee997fc4314eb6ab842237 Mon Sep 17 00:00:00 2001
From: Chris Hallberg <crhallberg@gmail.com>
Date: Fri, 18 Jul 2014 11:25:46 -0400
Subject: [PATCH] Added solid dynamic covers.

---
 config/vufind/config.ini                     | 10 +-
 module/VuFind/src/VuFind/Cover/Generator.php | 96 ++++++++++++++++++--
 module/VuFind/src/VuFind/Cover/Loader.php    | 10 +-
 3 files changed, 103 insertions(+), 13 deletions(-)

diff --git a/config/vufind/config.ini b/config/vufind/config.ini
index 6c60334db29..dfbcf642fb5 100644
--- a/config/vufind/config.ini
+++ b/config/vufind/config.ini
@@ -505,11 +505,13 @@ database          = mysql://root@localhost/vufind
 ; some services will never have images cached even if caching is enabled.
 coverimagesCache = true
 
-; This setting controls the image to display when no book cover is available.
-; If makeDynamicCovers is true and the GD library is installed, VuFind will draw
-; cover images on the fly. Otherwise, you can use noCoverAvailableImage to specify a
-; path relative to the base of your theme directory for a static image to display.
+; These settings control the image to display when no book cover is available.
+; If makeDynamicCovers is not false and the GD library is installed, VuFind will draw
+; cover images on the fly. Possible values: false, solid, grid
 ;makeDynamicCovers = true
+
+; Otherwise, you can use noCoverAvailableImage to specify a
+; path relative to the base of your theme directory for a static image to display.
 noCoverAvailableImage = images/noCover2.gif
 
 ; You can select from Syndetics, SyndeticsPlus, Amazon Editorial, Amazon, Booksite
diff --git a/module/VuFind/src/VuFind/Cover/Generator.php b/module/VuFind/src/VuFind/Cover/Generator.php
index f06aa6b308e..6de71005f0b 100644
--- a/module/VuFind/src/VuFind/Cover/Generator.php
+++ b/module/VuFind/src/VuFind/Cover/Generator.php
@@ -64,6 +64,7 @@ class Generator
     {
         $this->themeTools = $themeTools;
         $default = array(
+            'mode'         => 'grid',
             'authorFont'   => 'DroidSerif-Bold.ttf',
             'fontSize'     => 7,
             'lightness'    => 220,
@@ -94,6 +95,87 @@ class Generator
      * @return string contents of image file
      */
     public function generate($title, $author, $callnumber = null)
+    {
+        if ($this->settings->mode == 'solid') {
+            return $this->generateSolid($title, $author, $callnumber);
+        } else {
+            return $this->generateGrid($title, $author, $callnumber);
+        }
+    }
+
+    /**
+     * Generates a solid color background, ala Google
+     *
+     * @param string $title      Title of the book
+     * @param string $author     Author of the book
+     * @param string $callnumber Callnumber of the book
+     *
+     * @return string contents of image file
+     */
+    protected function generateSolid($title, $author, $callnumber)
+    {
+        $half = $this->settings->size/2;
+        // Create image
+        if (!($im = imagecreate($this->settings->size, $this->settings->size))) {
+            throw new \Exception("Cannot Initialize new GD image stream");
+        }
+        // this->white backdrop
+        $this->white = imagecolorallocate($im, 255, 255, 255);
+        // this->black
+        $this->black = imagecolorallocate($im, 0, 0, 0);
+
+        // Generate seed from callnumber, title back up
+        $seed = $this->createSeed($title, $callnumber);
+        // Number to color, hsb to control saturation and lightness
+        $color = $this->makeHSBColor(
+            $im,
+            $seed%256,
+            $this->settings->saturation,
+            $this->settings->lightness
+        );
+
+        // Fill solid color
+        imagefilledrectangle(
+            $im,
+            0,
+            0,
+            $this->settings->size,
+            $this->settings->size,
+            $color
+        );
+
+        $this->drawText(
+            $im,
+            strtoupper($title[0]),
+            $half,
+            $half+30,
+            $this->settings->titleFont,
+            60,
+            $this->white
+        );
+
+        // Output png CHECK THE PARAM
+        ob_start();
+        imagepng($im);
+        $img = ob_get_contents();
+        ob_end_clean();
+
+        // Clear memory
+        imagedestroy($im);
+        // GTFO
+        return $img;
+    }
+
+    /**
+     * Generates a grid of colors as primary feature
+     *
+     * @param string $title      Title of the book
+     * @param string $author     Author of the book
+     * @param string $callnumber Callnumber of the book
+     *
+     * @return string contents of image file
+     */
+    protected function generateGrid($title, $author, $callnumber)
     {
         // Set up common variables
         $half = $this->settings->size/2;
@@ -351,7 +433,7 @@ class Generator
      * @return void
      */
     protected function drawText($im, $text, $x, $y,
-        $font, $fontSize, $mcolor, $scolor, $align = null
+        $font, $fontSize, $mcolor, $scolor=false, $align=null
     ) {
         $txtWidth = $this->textWidth(
             $text,
@@ -376,11 +458,13 @@ class Generator
             $x = $this->settings->size-$txtWidth-$x;
         }
 
-        // Generate 5 lines of text, 4 offset in a border color
-        imagettftext($im, $fontSize, 0, $x,   $y+1, $scolor, $font, $text);
-        imagettftext($im, $fontSize, 0, $x,   $y-1, $scolor, $font, $text);
-        imagettftext($im, $fontSize, 0, $x+1, $y,   $scolor, $font, $text);
-        imagettftext($im, $fontSize, 0, $x-1, $y,   $scolor, $font, $text);
+        // Generate 5 lines of text, 4 offset in a border color
+        if ($scolor) {
+            imagettftext($im, $fontSize, 0, $x,   $y+1, $scolor, $font, $text);
+            imagettftext($im, $fontSize, 0, $x,   $y-1, $scolor, $font, $text);
+            imagettftext($im, $fontSize, 0, $x+1, $y,   $scolor, $font, $text);
+            imagettftext($im, $fontSize, 0, $x-1, $y,   $scolor, $font, $text);
+        }
         // 1 centered in main color
         imagettftext($im, $fontSize, 0, $x,   $y,   $mcolor, $font, $text);
     }
diff --git a/module/VuFind/src/VuFind/Cover/Loader.php b/module/VuFind/src/VuFind/Cover/Loader.php
index fa1f2e45d85..ac5941784ba 100644
--- a/module/VuFind/src/VuFind/Cover/Loader.php
+++ b/module/VuFind/src/VuFind/Cover/Loader.php
@@ -230,7 +230,7 @@ class Loader implements \Zend\Log\LoggerAwareInterface
         }
         return $this->contentType;
     }
-    
+
     /**
      * Get Cover Generator Object
      *
@@ -238,8 +238,12 @@ class Loader implements \Zend\Log\LoggerAwareInterface
      */
     public function getCoverGenerator()
     {
-        return new \VuFind\Cover\Generator($this->themeTools);
+        return new \VuFind\Cover\Generator(
+            $this->themeTools,
+            array('mode'=>$this->config->Content->makeDynamicCovers)
+        );
     }
+
     /**
      * Load an image given an ISBN and/or content type.
      *
@@ -277,7 +281,7 @@ class Loader implements \Zend\Log\LoggerAwareInterface
             && !$this->fetchFromContentType()
         ) {
             if (isset($this->config->Content->makeDynamicCovers)
-                && true == $this->config->Content->makeDynamicCovers
+                && false !== $this->config->Content->makeDynamicCovers
             ) {
                 $this->image = $this->getCoverGenerator()
                     ->generate($title, $author, $callnumber);
-- 
GitLab