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\Template\CompileableTemplate;
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 {
36 private $imageType = '';
57 private $fontSize = '';
62 private $imageString = '';
65 * Background color in RGB
67 private $backgroundColor = [
74 * Foreground color in RGB
76 private $foregroundColor = [
83 * Current choosen color array
85 private $colorMode = '';
90 private $imageResource = NULL;
95 private $imageName = '';
100 private $stringName = '';
103 * Groupable image strings?
105 private $groupable = 'single';
108 * Template engine instance
110 private $templateInstance = NULL;
113 * Protected constructor
115 * @param $className Name of the class
118 protected function __construct (string $className) {
119 // Call parent constructor
120 parent::__construct($className);
124 * Private setter for all colors
126 * @param $colorMode Whether background or foreground color
127 * @param $colorChannel Red, green or blue channel?
128 * @param $colorValue Value to set
130 private final function setColor (string $colorMode, string $colorChannel, $colorValue) {
131 // Construct the eval() command
132 $eval = sprintf("\$this->%s['%s'] = \"%s\";",
139 //* DEBUG: */ echo "mode={$colorMode}, channel={$colorChannel}, value={$colorValue}<br />\n";
144 * Setter for image width
146 * @param $width Width of the image
149 public final function setWidth (int $width) {
150 $this->width = $width;
154 * Getter for image width
156 * @return $width Width of the image
158 public final function getWidth () {
163 * Setter for image height
165 * @param $height Height of the image
168 public final function setHeight (int $height) {
169 $this->height = $height;
173 * Getter for image height
175 * @return $height Height of the image
177 public final function getHeight () {
178 return $this->height;
182 * Finish the type handling (unused at the moment)
185 * @todo Find something usefull for this method.
187 public function finishType () {
188 // Empty at the momemt
192 * Prepares the class for resolution (unused at the moment)
195 * @todo Find something usefull for this method.
197 public function initResolution () {
198 // Empty at the momemt
202 * Finish resolution handling (unused at the moment)
205 * @todo Find something usefull for this method.
207 public function finishResolution () {
208 // Empty at the momemt
212 * Prepares the class for base (unused at the moment)
215 * @todo Find something usefull for this method.
217 public function initBase () {
218 // Empty at the momemt
222 * Finish base handling (unused at the moment)
225 * @todo Find something usefull for this method.
227 public function finishBase () {
228 // Empty at the momemt
232 * Prepares the class for background color
236 public function initBackgroundColor () {
237 $this->colorMode = 'backgroundColor';
241 * Finish background color handling
244 * @todo Find something usefull for this method.
246 public function finishBackgroundColor () {
247 // Empty at the moment
251 * Prepares the class for foreground color
255 public function initForegroundColor () {
256 $this->colorMode = 'foregroundColor';
260 * Finish foreground color handling
263 * @todo Find something usefull for this method.
265 public function finishForegroundColor () {
266 // Empty at the moment
270 * Prepares the class for string (unused at the moment)
272 * @param $groupable Whether this image string is groupable or single
274 * @todo Find something usefull for this method.
276 public function initImageString (string $groupable = 'single') {
277 $this->groupable = $groupable;
281 * Finish string handling (unused at the moment)
284 * @todo Find something usefull for this method.
286 public function finishImageString () {
287 // Empty at the momemt
291 * Setter for red color
293 * @param $red Red color value
296 public final function setRed ($red) {
298 $this->setColor($this->colorMode, 'red', $red);
302 * Setter for green color
304 * @param $green Green color value
307 public final function setGreen ($green) {
309 $this->setColor($this->colorMode, 'green', $green);
313 * Setter for blue color
315 * @param $blue Blue color value
318 public final function setBlue ($blue) {
320 $this->setColor($this->colorMode, 'blue', $blue);
324 * Setter for image string
326 * @param $string String to set in image
329 public final function setString ($string) {
330 $this->imageString = (string) $string;
334 * Getter for image string
336 * @return $string String to set in image
338 public final function getString () {
339 return $this->imageString;
343 * Setter for image type
345 * @param $imageType Type to set in image
348 protected final function setImageType (string $imageType) {
349 $this->imageType = $imageType;
353 * Getter for image type
355 * @return $imageType Type to set in image
357 public final function getImageType () {
358 return $this->imageType;
362 * Setter for image name
364 * @param $name Name of the image
367 public final function setImageName ($name) {
368 $this->imageName = (string) $name;
372 * Getter for image name
374 * @return $name Name of the image
376 public final function getImageName () {
377 return $this->imageName;
381 * Getter for image resource
383 * @return $imageResource An image resource from imagecreatetruecolor() function
385 public final function getImageResource() {
386 return $this->imageResource;
390 * Setter for X coordinate
392 * @param $x X coordinate
395 public final function setX (int $x) {
400 * Getter for X coordinate
402 * @return $x X coordinate
404 public final function getX () {
409 * Setter for Y coordinate
411 * @param $y Y coordinate
414 public final function setY (int $y) {
419 * Getter for Y coordinate
421 * @return $y Y coordinate
423 public final function getY () {
428 * Setter for font size
430 * @param $fontSize Font size for strings
433 public final function setFontSize (int $fontSize) {
434 $this->fontSize = $fontSize;
438 * Getter for font size
440 * @return $fontSize Font size for strings
442 public final function getFontSize () {
443 return $this->fontSize;
447 * Setter for string name
449 * @param $stringName String name to set
452 public final function setStringName(string $stringName) {
453 $this->stringName = $stringName;
457 * Setter for template engine instances
459 * @param $templateInstance An instance of a CompileableTemplate class
462 protected final function setTemplateInstance (CompileableTemplate $templateInstance) {
463 $this->templateInstance = $templateInstance;
467 * Getter for template engine instances
469 * @return $templateInstance An instance of a CompileableTemplate class
471 public final function getTemplateInstance () {
472 return $this->templateInstance;
476 * Finish this image by producing it
480 public function finishImage () {
481 // Compile width and height
482 $width = $this->getTemplateInstance()->compileRawCode($this->getWidth());
483 $height = $this->getTemplateInstance()->compileRawCode($this->getHeight());
486 $this->setWidth($width);
487 $this->setHeight($height);
489 // Get a image resource
490 $this->imageResource = imagecreatetruecolor($width, $height);
492 // Compile background colors
493 $red = $this->getTemplateInstance()->compileRawCode($this->backgroundColor['red']);
494 $green = $this->getTemplateInstance()->compileRawCode($this->backgroundColor['green']);
495 $blue = $this->getTemplateInstance()->compileRawCode($this->backgroundColor['blue']);
498 $this->initBackgroundColor();
500 $this->setGreen($green);
501 $this->setBlue($blue);
503 // Get a pointer for background color
504 $backColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
507 imagefill($this->getImageResource(), 0, 0, $backColor);
509 // Compile foreground colors
510 $red = $this->getTemplateInstance()->compileRawCode($this->foregroundColor['red']);
511 $green = $this->getTemplateInstance()->compileRawCode($this->foregroundColor['green']);
512 $blue = $this->getTemplateInstance()->compileRawCode($this->foregroundColor['blue']);
515 $this->initForegroundColor();
517 $this->setGreen($green);
518 $this->setBlue($blue);
520 // Get a pointer for foreground color
521 $foreColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
523 switch ($this->groupable) {
524 case 'single': // Single image string
525 // Compile image string
526 $imageString = $this->getTemplateInstance()->compileRawCode($this->getString());
529 $this->setString($imageString);
531 // Compile X/Y coordinates and font size
532 $x = $this->getTemplateInstance()->compileRawCode($this->getX());
533 $y = $this->getTemplateInstance()->compileRawCode($this->getY());
534 $size = $this->getTemplateInstance()->compileRawCode($this->getFontSize());
536 // Set the image string
537 imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
540 case 'groupable': // More than one string allowed
541 // Walk through all groups
542 foreach ($this->getTemplateInstance()->getVariableGroups() as $group => $set) {
544 $this->getTemplateInstance()->setVariableGroup($group, false);
546 // Compile image string
547 $imageString = $this->getTemplateInstance()->compileRawCode($this->getString());
549 // Compile X/Y coordinates and font size
550 $x = $this->getTemplateInstance()->compileRawCode($this->getX());
551 $y = $this->getTemplateInstance()->compileRawCode($this->getY());
552 $size = $this->getTemplateInstance()->compileRawCode($this->getFontSize());
554 // Set the image string
555 //* DEBUG: */ print __METHOD__.": size={$size}, x={$x}, y={$y}, string={$imageString}<br />\n";
556 imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
561 // You need finishing in your image class!
565 * Getter for full created image content
567 * @return $imageContent The raw image content
569 public function getContent () {
570 // Get cache file name
571 $cacheFile = $this->getTemplateInstance()->getImageCacheFile();
573 // Open it for reading
574 $fileObject = $cacheFile->openFile('r');
576 // Rewind to beginning
577 $fileObject->rewind();
580 $imageContent = $fileObject->fread($cacheFile->getSize());
583 return $imageContent;