* @version 0.0.0
* @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 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 {
// Load traits
use CompileableTemplateTrait;
/**
* Image type
*/
private $imageType = '';
/**
* Width of the image
*/
private $width = 0;
/**
* Height of the image
*/
private $height = 0;
/**
* X/Y
*/
private $x = 0;
private $y = 0;
/**
* Font size
*/
private $fontSize = 0;
/**
* Image string
*/
private $imageString = '';
/**
* Background color in RGB
*/
private $backgroundColor = [
'red' => '',
'green' => '',
'blue' => ''
];
/**
* Foreground color in RGB
*/
private $foregroundColor = [
'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 (string $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 (string $colorMode, string $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 (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;
}
/**
* 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 (string $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) {
// Set image color
$this->setColor($this->colorMode, 'red', $red);
}
/**
* Setter for green color
*
* @param $green Green color value
* @return void
*/
public final function setGreen ($green) {
// Set image color
$this->setColor($this->colorMode, 'green', $green);
}
/**
* Setter for blue color
*
* @param $blue Blue color value
* @return void
*/
public final function setBlue ($blue) {
// Set image color
$this->setColor($this->colorMode, 'blue', $blue);
}
/**
* Setter for image string
*
* @param $string String to set in image
* @return void
*/
public final function setString (string $string) {
$this->imageString = $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 (string $imageType) {
$this->imageType = $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 (string $name) {
$this->imageName = $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 (int $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 (int $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 (int $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(string $stringName) {
$this->stringName = $stringName;
}
/**
* Finish this image by producing it
*
* @return void
*/
public function finishImage () {
// Compile width and height
$width = $this->getTemplateInstance()->compileRawCode($this->getWidth());
$height = $this->getTemplateInstance()->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 = $this->getTemplateInstance()->compileRawCode($this->backgroundColor['red']);
$green = $this->getTemplateInstance()->compileRawCode($this->backgroundColor['green']);
$blue = $this->getTemplateInstance()->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 = $this->getTemplateInstance()->compileRawCode($this->foregroundColor['red']);
$green = $this->getTemplateInstance()->compileRawCode($this->foregroundColor['green']);
$blue = $this->getTemplateInstance()->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 = $this->getTemplateInstance()->compileRawCode($this->getString());
// Set it back
$this->setString($imageString);
// Compile X/Y coordinates and font size
$x = $this->getTemplateInstance()->compileRawCode($this->getX());
$y = $this->getTemplateInstance()->compileRawCode($this->getY());
$size = $this->getTemplateInstance()->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 ($this->getTemplateInstance()->getVariableGroups() as $group => $set) {
// Set the group
$this->getTemplateInstance()->setVariableGroup($group, false);
// Compile image string
$imageString = $this->getTemplateInstance()->compileRawCode($this->getString());
// Compile X/Y coordinates and font size
$x = $this->getTemplateInstance()->compileRawCode($this->getX());
$y = $this->getTemplateInstance()->compileRawCode($this->getY());
$size = $this->getTemplateInstance()->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;
}
}