* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2019 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 . */ abstract class BaseImage extends BaseFrameworkSystem implements Registerable { /** * Image type */ private $imageType = ''; /** * Width of the image */ private $width = ''; /** * Height of the image */ private $height = ''; /** * X/Y */ private $x = ''; private $y = ''; /** * Font size */ private $fontSize = ''; /** * Image string */ private $imageString = ''; /** * Background color in RGB */ private $backgroundColor = array( 'red' => '', 'green' => '', 'blue' => '' ); /** * Foreground color in RGB */ private $foregroundColor = array( 'red' => '', 'green' => '', 'blue' => '' ); /** * Current choosen color array */ private $colorMode = ''; /** * Image resource */ private $imageResource = NULL; /** * Image name */ private $imageName = ''; /** * String name */ private $stringName = ''; /** * Groupable image strings? */ private $groupable = 'single'; /** * Protected constructor * * @param $className Name of the class * @return void */ protected function __construct ($className) { // Call parent constructor parent::__construct($className); } /** * Private setter for all colors * * @param $colorMode Whether background or foreground color * @param $colorChannel Red, green or blue channel? * @param $colorValue Value to set */ private final function setColor ($colorMode, $colorChannel, $colorValue) { // Construct the eval() command $eval = sprintf("\$this->%s['%s'] = \"%s\";", $colorMode, $colorChannel, $colorValue ); // Run the command //* DEBUG: */ echo "mode={$colorMode}, channel={$colorChannel}, value={$colorValue}
\n"; eval($eval); } /** * Setter for image width * * @param $width Width of the image * @return void */ public final function setWidth ($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 ($height) { $this->height = $height; } /** * Getter for image height * * @return $height Height of the image */ public final function getHeight () { return $this->height; } /** * Finish the type handling (unused at the moment) * * @return void * @todo Find something usefull for this method. */ public function finishType () { // Empty at the momemt } /** * Prepares the class for resolution (unused at the moment) * * @return void * @todo Find something usefull for this method. */ public function initResolution () { // Empty at the momemt } /** * Finish resolution handling (unused at the moment) * * @return void * @todo Find something usefull for this method. */ public function finishResolution () { // Empty at the momemt } /** * Prepares the class for base (unused at the moment) * * @return void * @todo Find something usefull for this method. */ public function initBase () { // Empty at the momemt } /** * Finish base handling (unused at the moment) * * @return void * @todo Find something usefull for this method. */ public function finishBase () { // Empty at the momemt } /** * Prepares the class for background color * * @return void */ public function initBackgroundColor () { $this->colorMode = 'backgroundColor'; } /** * Finish background color handling * * @return void * @todo Find something usefull for this method. */ public function finishBackgroundColor () { // Empty at the moment } /** * Prepares the class for foreground color * * @return void */ public function initForegroundColor () { $this->colorMode = 'foregroundColor'; } /** * Finish foreground color handling * * @return void * @todo Find something usefull for this method. */ public function finishForegroundColor () { // Empty at the moment } /** * Prepares the class for string (unused at the moment) * * @param $groupable Whether this image string is groupable or single * @return void * @todo Find something usefull for this method. */ public function initImageString ($groupable = 'single') { $this->groupable = $groupable; } /** * Finish string handling (unused at the moment) * * @return void * @todo Find something usefull for this method. */ public function finishImageString () { // Empty at the momemt } /** * Setter for red color * * @param $red Red color value * @return void */ public final function setRed ($red) { // Get array name $arrayName = $this->colorMode; // Set image color $this->setColor($arrayName, 'red', $red); } /** * Setter for green color * * @param $green Green color value * @return void */ public final function setGreen ($green) { // Get array name $arrayName = $this->colorMode; // Set image color $this->setColor($arrayName, 'green', $green); } /** * Setter for blue color * * @param $blue Blue color value * @return void */ public final function setBlue ($blue) { // Get array name $arrayName = $this->colorMode; // Set image color $this->setColor($arrayName, 'blue', $blue); } /** * Setter for image string * * @param $string String to set in image * @return void */ public final function setString ($string) { $this->imageString = (string) $string; } /** * Getter for image string * * @return $string String to set in image */ public final function getString () { return $this->imageString; } /** * Setter for image type * * @param $imageType Type to set in image * @return void */ protected final function setImageType ($imageType) { $this->imageType = (string) $imageType; } /** * Getter for image type * * @return $imageType Type to set in image */ public final function getImageType () { return $this->imageType; } /** * Setter for image name * * @param $name Name of the image * @return void */ public final function setImageName ($name) { $this->imageName = (string) $name; } /** * Getter for image name * * @return $name Name of the image */ public final function getImageName () { return $this->imageName; } /** * Getter for image resource * * @return $imageResource An image resource from imagecreatetruecolor() function */ public final function getImageResource() { return $this->imageResource; } /** * Setter for X coordinate * * @param $x X coordinate * @return void */ public final function setX ($x) { $this->x = $x; } /** * Getter for X coordinate * * @return $x X coordinate */ public final function getX () { return $this->x; } /** * Setter for Y coordinate * * @param $y Y coordinate * @return void */ public final function setY ($y) { $this->y = $y; } /** * Getter for Y coordinate * * @return $y Y coordinate */ public final function getY () { return $this->y; } /** * Setter for font size * * @param $fontSize Font size for strings * @return void */ public final function setFontSize ($fontSize) { $this->fontSize = $fontSize; } /** * Getter for font size * * @return $fontSize Font size for strings */ public final function getFontSize () { return $this->fontSize; } /** * Setter for string name * * @param $stringName String name to set * @return void */ public final function setStringName($stringName) { $this->stringName = $stringName; } /** * Finish this image by producing it * * @return void */ public function finishImage () { // Get template instance $templateInstance = $this->getTemplateInstance(); // Compile width and height $width = $templateInstance->compileRawCode($this->getWidth()); $height = $templateInstance->compileRawCode($this->getHeight()); // Set both again $this->setWidth($width); $this->setHeight($height); // Get a image resource $this->imageResource = imagecreatetruecolor($width, $height); // Compile background colors $red = $templateInstance->compileRawCode($this->backgroundColor['red']); $green = $templateInstance->compileRawCode($this->backgroundColor['green']); $blue = $templateInstance->compileRawCode($this->backgroundColor['blue']); // Set all back $this->initBackgroundColor(); $this->setRed($red); $this->setGreen($green); $this->setBlue($blue); // Get a pointer for background color $backColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue); // Fill the image imagefill($this->getImageResource(), 0, 0, $backColor); // Compile foreground colors $red = $templateInstance->compileRawCode($this->foregroundColor['red']); $green = $templateInstance->compileRawCode($this->foregroundColor['green']); $blue = $templateInstance->compileRawCode($this->foregroundColor['blue']); // Set all fore $this->initForegroundColor(); $this->setRed($red); $this->setGreen($green); $this->setBlue($blue); // Get a pointer for foreground color $foreColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue); switch ($this->groupable) { case 'single': // Single image string // Compile image string $imageString = $templateInstance->compileRawCode($this->getString()); // Set it back $this->setString($imageString); // Compile X/Y coordinates and font size $x = $templateInstance->compileRawCode($this->getX()); $y = $templateInstance->compileRawCode($this->getY()); $size = $templateInstance->compileRawCode($this->getFontSize()); // Set the image string imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor); break; case 'groupable': // More than one string allowed // Walk through all groups foreach ($templateInstance->getVariableGroups() as $group => $set) { // Set the group $templateInstance->setVariableGroup($group, false); // Compile image string $imageString = $templateInstance->compileRawCode($this->getString()); // Compile X/Y coordinates and font size $x = $templateInstance->compileRawCode($this->getX()); $y = $templateInstance->compileRawCode($this->getY()); $size = $templateInstance->compileRawCode($this->getFontSize()); // Set the image string //* DEBUG: */ print __METHOD__.": size={$size}, x={$x}, y={$y}, string={$imageString}
\n"; imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor); } // END - foreach break; } // You need finishing in your image class! } /** * Getter for full created image content * * @return $imageContent The raw image content */ public function getContent () { // Get cache file name $cacheFile = $this->getTemplateInstance()->getImageCacheFile(); // Open it for reading $fileObject = $cacheFile->openFile('r'); // Rewind to beginning $fileObject->rewind(); // Load the content $imageContent = $fileObject->fread($cacheFile->getSize()); // And return it return $imageContent; } }