Continued with renaming-season:
[core.git] / framework / main / classes / helper / captcha / images / class_ImageHelper.php
diff --git a/framework/main/classes/helper/captcha/images/class_ImageHelper.php b/framework/main/classes/helper/captcha/images/class_ImageHelper.php
new file mode 100644 (file)
index 0000000..a7a1235
--- /dev/null
@@ -0,0 +1,400 @@
+<?php
+// Own namespace
+namespace CoreFramework\Helper;
+
+// Import framework stuff
+use CoreFramework\Template\CompileableTemplate;
+
+/**
+ * A helper for creating images
+ *
+ * @author             Roland Haeder <webmaster@shipsimu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.shipsimu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+class ImageHelper extends BaseCaptcha implements HelpableTemplate {
+       /**
+        * The image type
+        */
+       private $imageType = 'png';
+
+       /**
+        * The image name
+        */
+       private $imageName = '';
+
+       /**
+        * Width of the image in pixel
+        */
+       private $width = 0;
+
+       /**
+        * Height of the image in pixel
+        */
+       private $height = 0;
+
+       /**
+        * Array for background color values
+        */
+       private $backgroundColor = array(
+               'red'   => 0,
+               'green' => 0,
+               'blue'  => 0
+       );
+
+       /**
+        * Array for foreground color values
+        */
+       private $foregroundColor = array(
+               'red'   => 0,
+               'green' => 0,
+               'blue'  => 0
+       );
+
+       /**
+        * All image strings
+        */
+       private $imageStrings = array();
+
+       /**
+        * Current string name
+        */
+       private $currString = '';
+
+       /**
+        * Base image
+        */
+       private $baseImage = '';
+
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+       }
+
+       /**
+        * Creates the helper class
+        *
+        * @param       $templateInstance       An instance of a template engine
+        * @param       $imageType                      Type of the image
+        * @return      $helperInstance         A preparedf instance of this helper
+        */
+       public static final function createImageHelper (CompileableTemplate $templateInstance, $imageType) {
+               // Get new instance
+               $helperInstance = new ImageHelper();
+
+               // Set template instance
+               $helperInstance->setTemplateInstance($templateInstance);
+
+               // Set image type (blindly)
+               $helperInstance->setImageType($imageType);
+
+               // Initialize RNG
+               $helperInstance->initializeRandomNumberGenerator($templateInstance);
+
+               // Return the prepared instance
+               return $helperInstance;
+       }
+
+       /**
+        * Setter for image type
+        *
+        * @param       $imageType      Type of the image
+        * @return      void
+        */
+       protected final function setImageType ($imageType) {
+               $this->imageType = (string) $imageType;
+       }
+
+       /**
+        * Getter for image name
+        *
+        * @return      $imageType      Type of the image
+        */
+       public final function getImageType () {
+               return $this->imageType;
+       }
+
+       /**
+        * Setter for base image
+        *
+        * @param       $baseImage      A base image template
+        * @return      void
+        */
+       public final function setBaseImage ($baseImage) {
+               $this->baseImage = (string) $baseImage;
+       }
+
+       /**
+        * Getter for base image
+        *
+        * @return      $baseImage      A base image template
+        */
+       public final function getBaseImage () {
+               return $this->baseImage;
+       }
+
+       /**
+        * Setter for image name
+        *
+        * @param       $imageName      Name of the image
+        * @return      void
+        */
+       public final function setImageName ($imageName) {
+               $this->imageName = (string) $imageName;
+       }
+
+       /**
+        * Getter for image name
+        *
+        * @return      $imageName      Name of the image
+        */
+       protected final function getImageName () {
+               return $this->imageName;
+       }
+
+       /**
+        * Setter for image width
+        *
+        * @param       $width  Width of the image
+        * @return      void
+        */
+       public final function setWidth ($width) {
+               $this->width = (int) $width;
+       }
+
+       /**
+        * Getter for image width
+        *
+        * @return      $width  Width of the image
+        */
+       public final function getWidth () {
+               return $this->width;
+       }
+
+       /**
+        * Setter for image height
+        *
+        * @param       $height         Height of the image
+        * @return      void
+        */
+       public final function setHeight ($height) {
+               $this->height = (int) $height;
+       }
+
+       /**
+        * Getter for image height
+        *
+        * @return      $height         Height of the image
+        */
+       public final function getHeight () {
+               return $this->height;
+       }
+
+       /**
+        * Setter for RGB of background color
+        *
+        * @param       $red    Color value for red
+        * @param       $green  Color value for green
+        * @param       $blue   Color value for blue
+        * @return      void
+        */
+       public final function setBackgroundColorRedGreenBlue ($red, $green, $blue) {
+               // Random numbers?
+               if ($red === 'rand') {
+                       $red = $this->getRngInstance()->randomNumber(0, 255);
+               } // END - if
+               if ($green === 'rand') {
+                       $green = $this->getRngInstance()->randomNumber(0, 255);
+               } // END - if
+               if ($blue === 'rand') {
+                       $blue = $this->getRngInstance()->randomNumber(0, 255);
+               } // END - if
+
+               $this->backgroundColor['red']   = (int) $red;
+               $this->backgroundColor['green'] = (int) $green;
+               $this->backgroundColor['blue']  = (int) $blue;
+       }
+
+       /**
+        * Setter for RGB of foreground color
+        *
+        * @param       $red    Color value for red
+        * @param       $green  Color value for green
+        * @param       $blue   Color value for blue
+        * @return      void
+        */
+       public final function setForegroundColorRedGreenBlue ($red, $green, $blue) {
+               // Random numbers?
+               if ($red === 'rand') {
+                       $red = $this->getRngInstance()->randomNumber(0, 255);
+               } // END - if
+               if ($green === 'rand') {
+                       $green = $this->getRngInstance()->randomNumber(0, 255);
+               } // END - if
+               if ($blue === 'rand') {
+                       $blue = $this->getRngInstance()->randomNumber(0, 255);
+               } // END - if
+
+               $this->foregroundColor['red']   = (int) $red;
+               $this->foregroundColor['green'] = (int) $green;
+               $this->foregroundColor['blue']  = (int) $blue;
+       }
+
+       /**
+        * Adds an image string to the buffer by the given string name
+        *
+        * @param       $stringName             String name (identifier)
+        */
+       public function addTextLine ($stringName) {
+               // Create the image string
+               $this->imageStrings[$stringName] = array(
+                       'x'      => '',
+                       'y'      => '',
+                       'size'   => '',
+                       'string' => ''
+               );
+
+               // Set current string name
+               $this->currString = $stringName;
+       }
+
+       /**
+        * Setter for image message string
+        *
+        * @param       $imageString    A message to display in image
+        * @return      void
+        */
+       public final function setImageString ($imageString) {
+               $this->imageStrings[$this->currString]['string'] = (string) $imageString;
+       }
+
+       /**
+        * Getter for image message string
+        *
+        * @return      $imageString    A message to display in image
+        */
+       public final function getImageString () {
+               return $this->imageStrings[$this->currString]['string'];
+       }
+
+       /**
+        * Setter for X/Y coordinates for strings
+        *
+        * @param       $x      X coordinate
+        * @param       $y      Y coordinate
+        * @return      void
+        */
+       public final function setCoord ($x, $y) {
+               $this->imageStrings[$this->currString]['x'] = (int) $x;
+               $this->imageStrings[$this->currString]['y'] = (int) $y;
+       }
+
+       /**
+        * Getter for X coordinate
+        *
+        * @return      $x      X coordinate
+        */
+       public final function getX () {
+               return $this->imageStrings[$this->currString]['x'];
+       }
+
+       /**
+        * Getter for Y coordinate
+        *
+        * @return      $y      Y coordinate
+        */
+       public final function getY () {
+               return $this->imageStrings[$this->currString]['y'];
+       }
+
+       /**
+        * Setter for font size
+        *
+        * @param       $fontSize       Font size for strings
+        * @return      void
+        */
+       public final function setFontSize ($fontSize) {
+               // Random font size?
+               if ($fontSize === 'rand') {
+                       $fontSize = $this->getRngInstance()->randomNumber(4, 9);
+               } // END - if
+
+               $this->imageStrings[$this->currString]['size'] = (int) $fontSize;
+       }
+
+       /**
+        * Getter for font size
+        *
+        * @return      $fontSize       Font size for strings
+        */
+       public final function getFontSize () {
+               return $this->imageStrings[$this->currString]['size'];
+       }
+
+       /**
+        * Flushs the content out
+        *
+        * @return      void
+        */
+       public function flushContent () {
+               // Get a template instance
+               $templateInstance = $this->getTemplateInstance();
+
+               // Get the base image
+               $templateInstance->loadImageTemplate($this->getBaseImage());
+
+               // Assign all the image values with the template
+               $templateInstance->assignVariable('image_name'    , $this->getImageName());
+               $templateInstance->assignVariable('image_type'    , $this->getImageType());
+               $templateInstance->assignVariable('image_width'   , $this->getWidth());
+               $templateInstance->assignVariable('image_height'  , $this->getHeight());
+               $templateInstance->assignVariable('image_bg_red'  , $this->backgroundColor['red']);
+               $templateInstance->assignVariable('image_bg_green', $this->backgroundColor['green']);
+               $templateInstance->assignVariable('image_bg_blue' , $this->backgroundColor['blue']);
+               $templateInstance->assignVariable('image_fg_red'  , $this->foregroundColor['red']);
+               $templateInstance->assignVariable('image_fg_green', $this->foregroundColor['green']);
+               $templateInstance->assignVariable('image_fg_blue' , $this->foregroundColor['blue']);
+
+               // Add all strings
+               foreach ($this->imageStrings as $id => $imageString) {
+                       // Set current string id to keep this helper in sync with template engine
+                       $this->currString = $id;
+
+                       // Set variable group
+                       $templateInstance->setVariableGroup($id);
+
+                       // Add group variables
+                       $templateInstance->addGroupVariable('image_x'     , $this->getX());
+                       $templateInstance->addGroupVariable('image_y'     , $this->getY());
+                       $templateInstance->addGroupVariable('image_size'  , $this->getFontSize());
+                       $templateInstance->addGroupVariable('image_string', $this->getImageString());
+               } // END - foreach
+
+               // Get the raw content
+               $imageContent = $templateInstance->getRawTemplateData();
+
+               // Transfer all to the template engine
+               $templateInstance->renderXmlContent($imageContent);
+       }
+
+}