3 namespace Org\Mxchange\CoreFramework\Image;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
7 use Org\Mxchange\CoreFramework\Registry\Registerable;
8 use Org\Mxchange\CoreFramework\Traits\Template\CompileableTemplateTrait;
11 * A general image class
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 abstract class BaseImage extends BaseFrameworkSystem implements Registerable {
34 use CompileableTemplateTrait;
39 private $imageType = '';
60 private $fontSize = 0;
65 private $imageString = '';
68 * Background color in RGB
70 private $backgroundColor = [
77 * Foreground color in RGB
79 private $foregroundColor = [
86 * Current choosen color array
88 private $colorMode = '';
93 private $imageResource = NULL;
98 private $imageName = '';
103 private $stringName = '';
106 * Groupable image strings?
108 private $groupable = 'single';
111 * Protected constructor
113 * @param $className Name of the class
116 protected function __construct (string $className) {
117 // Call parent constructor
118 parent::__construct($className);
122 * Private setter for all colors
124 * @param $colorMode Whether background or foreground color
125 * @param $colorChannel Red, green or blue channel?
126 * @param $colorValue Value to set
128 private final function setColor (string $colorMode, string $colorChannel, $colorValue) {
129 // Construct the eval() command
130 $eval = sprintf("\$this->%s['%s'] = \"%s\";",
137 //* DEBUG: */ echo "mode={$colorMode}, channel={$colorChannel}, value={$colorValue}<br />\n";
142 * Setter for image width
144 * @param $width Width of the image
147 public final function setWidth (int $width) {
148 $this->width = $width;
152 * Getter for image width
154 * @return $width Width of the image
156 public final function getWidth () {
161 * Setter for image height
163 * @param $height Height of the image
166 public final function setHeight (int $height) {
167 $this->height = $height;
171 * Getter for image height
173 * @return $height Height of the image
175 public final function getHeight () {
176 return $this->height;
180 * Finish the type handling (unused at the moment)
183 * @todo Find something usefull for this method.
185 public function finishType () {
186 // Empty at the momemt
190 * Prepares the class for resolution (unused at the moment)
193 * @todo Find something usefull for this method.
195 public function initResolution () {
196 // Empty at the momemt
200 * Finish resolution handling (unused at the moment)
203 * @todo Find something usefull for this method.
205 public function finishResolution () {
206 // Empty at the momemt
210 * Prepares the class for base (unused at the moment)
213 * @todo Find something usefull for this method.
215 public function initBase () {
216 // Empty at the momemt
220 * Finish base handling (unused at the moment)
223 * @todo Find something usefull for this method.
225 public function finishBase () {
226 // Empty at the momemt
230 * Prepares the class for background color
234 public function initBackgroundColor () {
235 $this->colorMode = 'backgroundColor';
239 * Finish background color handling
242 * @todo Find something usefull for this method.
244 public function finishBackgroundColor () {
245 // Empty at the moment
249 * Prepares the class for foreground color
253 public function initForegroundColor () {
254 $this->colorMode = 'foregroundColor';
258 * Finish foreground color handling
261 * @todo Find something usefull for this method.
263 public function finishForegroundColor () {
264 // Empty at the moment
268 * Prepares the class for string (unused at the moment)
270 * @param $groupable Whether this image string is groupable or single
272 * @todo Find something usefull for this method.
274 public function initImageString (string $groupable = 'single') {
275 $this->groupable = $groupable;
279 * Finish string handling (unused at the moment)
282 * @todo Find something usefull for this method.
284 public function finishImageString () {
285 // Empty at the momemt
289 * Setter for red color
291 * @param $red Red color value
294 public final function setRed ($red) {
296 $this->setColor($this->colorMode, 'red', $red);
300 * Setter for green color
302 * @param $green Green color value
305 public final function setGreen ($green) {
307 $this->setColor($this->colorMode, 'green', $green);
311 * Setter for blue color
313 * @param $blue Blue color value
316 public final function setBlue ($blue) {
318 $this->setColor($this->colorMode, 'blue', $blue);
322 * Setter for image string
324 * @param $string String to set in image
327 public final function setString ($string) {
328 $this->imageString = (string) $string;
332 * Getter for image string
334 * @return $string String to set in image
336 public final function getString () {
337 return $this->imageString;
341 * Setter for image type
343 * @param $imageType Type to set in image
346 protected final function setImageType (string $imageType) {
347 $this->imageType = $imageType;
351 * Getter for image type
353 * @return $imageType Type to set in image
355 public final function getImageType () {
356 return $this->imageType;
360 * Setter for image name
362 * @param $name Name of the image
365 public final function setImageName ($name) {
366 $this->imageName = (string) $name;
370 * Getter for image name
372 * @return $name Name of the image
374 public final function getImageName () {
375 return $this->imageName;
379 * Getter for image resource
381 * @return $imageResource An image resource from imagecreatetruecolor() function
383 public final function getImageResource() {
384 return $this->imageResource;
388 * Setter for X coordinate
390 * @param $x X coordinate
393 public final function setX (int $x) {
398 * Getter for X coordinate
400 * @return $x X coordinate
402 public final function getX () {
407 * Setter for Y coordinate
409 * @param $y Y coordinate
412 public final function setY (int $y) {
417 * Getter for Y coordinate
419 * @return $y Y coordinate
421 public final function getY () {
426 * Setter for font size
428 * @param $fontSize Font size for strings
431 public final function setFontSize (int $fontSize) {
432 $this->fontSize = $fontSize;
436 * Getter for font size
438 * @return $fontSize Font size for strings
440 public final function getFontSize () {
441 return $this->fontSize;
445 * Setter for string name
447 * @param $stringName String name to set
450 public final function setStringName(string $stringName) {
451 $this->stringName = $stringName;
455 * Finish this image by producing it
459 public function finishImage () {
460 // Compile width and height
461 $width = $this->getTemplateInstance()->compileRawCode($this->getWidth());
462 $height = $this->getTemplateInstance()->compileRawCode($this->getHeight());
465 $this->setWidth($width);
466 $this->setHeight($height);
468 // Get a image resource
469 $this->imageResource = imagecreatetruecolor($width, $height);
471 // Compile background colors
472 $red = $this->getTemplateInstance()->compileRawCode($this->backgroundColor['red']);
473 $green = $this->getTemplateInstance()->compileRawCode($this->backgroundColor['green']);
474 $blue = $this->getTemplateInstance()->compileRawCode($this->backgroundColor['blue']);
477 $this->initBackgroundColor();
479 $this->setGreen($green);
480 $this->setBlue($blue);
482 // Get a pointer for background color
483 $backColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
486 imagefill($this->getImageResource(), 0, 0, $backColor);
488 // Compile foreground colors
489 $red = $this->getTemplateInstance()->compileRawCode($this->foregroundColor['red']);
490 $green = $this->getTemplateInstance()->compileRawCode($this->foregroundColor['green']);
491 $blue = $this->getTemplateInstance()->compileRawCode($this->foregroundColor['blue']);
494 $this->initForegroundColor();
496 $this->setGreen($green);
497 $this->setBlue($blue);
499 // Get a pointer for foreground color
500 $foreColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
502 switch ($this->groupable) {
503 case 'single': // Single image string
504 // Compile image string
505 $imageString = $this->getTemplateInstance()->compileRawCode($this->getString());
508 $this->setString($imageString);
510 // Compile X/Y coordinates and font size
511 $x = $this->getTemplateInstance()->compileRawCode($this->getX());
512 $y = $this->getTemplateInstance()->compileRawCode($this->getY());
513 $size = $this->getTemplateInstance()->compileRawCode($this->getFontSize());
515 // Set the image string
516 imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
519 case 'groupable': // More than one string allowed
520 // Walk through all groups
521 foreach ($this->getTemplateInstance()->getVariableGroups() as $group => $set) {
523 $this->getTemplateInstance()->setVariableGroup($group, false);
525 // Compile image string
526 $imageString = $this->getTemplateInstance()->compileRawCode($this->getString());
528 // Compile X/Y coordinates and font size
529 $x = $this->getTemplateInstance()->compileRawCode($this->getX());
530 $y = $this->getTemplateInstance()->compileRawCode($this->getY());
531 $size = $this->getTemplateInstance()->compileRawCode($this->getFontSize());
533 // Set the image string
534 //* DEBUG: */ print __METHOD__.": size={$size}, x={$x}, y={$y}, string={$imageString}<br />\n";
535 imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
540 // You need finishing in your image class!
544 * Getter for full created image content
546 * @return $imageContent The raw image content
548 public function getContent () {
549 // Get cache file name
550 $cacheFile = $this->getTemplateInstance()->getImageCacheFile();
552 // Open it for reading
553 $fileObject = $cacheFile->openFile('r');
555 // Rewind to beginning
556 $fileObject->rewind();
559 $imageContent = $fileObject->fread($cacheFile->getSize());
562 return $imageContent;