* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 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 . */ 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 = [ 'red' => 0, 'green' => 0, 'blue' => 0 ]; /** * Array for foreground color values */ private $foregroundColor = [ 'red' => 0, 'green' => 0, 'blue' => 0 ]; /** * All image strings */ private $imageStrings = []; /** * Current string name */ private $currString = ''; /** * Base image */ private $baseImage = ''; /** * Protected constructor * * @return void */ private 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, string $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 (string $imageType) { $this->imageType = $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 (string $baseImage) { $this->baseImage = $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 (string $imageName) { $this->imageName = $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 (int $width) { $this->width = $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 (int $height) { $this->height = $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); } if ($green === 'rand') { $green = $this->getRngInstance()->randomNumber(0, 255); } if ($blue === 'rand') { $blue = $this->getRngInstance()->randomNumber(0, 255); } $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); } if ($green === 'rand') { $green = $this->getRngInstance()->randomNumber(0, 255); } if ($blue === 'rand') { $blue = $this->getRngInstance()->randomNumber(0, 255); } $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 (string $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 (string $imageString) { $this->imageStrings[$this->currString]['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 (int $x, int $y) { $this->imageStrings[$this->currString]['x'] = $x; $this->imageStrings[$this->currString]['y'] = $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); } $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()); } // Get the raw content $imageContent = $templateInstance->getRawTemplateData(); // Transfer all to the template engine $templateInstance->renderXmlContent($imageContent); } }