3 namespace Org\Mxchange\CoreFramework\Image;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
7 use Org\Mxchange\CoreFramework\Registry\Registerable;
10 * A general image class
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 abstract class BaseImage extends BaseFrameworkSystem implements Registerable {
35 private $imageType = '';
56 private $fontSize = '';
61 private $imageString = '';
64 * Background color in RGB
66 private $backgroundColor = array(
73 * Foreground color in RGB
75 private $foregroundColor = array(
82 * Current choosen color array
84 private $colorMode = '';
89 private $imageResource = NULL;
94 private $imageName = '';
99 private $stringName = '';
102 * Groupable image strings?
104 private $groupable = 'single';
107 * Protected constructor
109 * @param $className Name of the class
112 protected function __construct (string $className) {
113 // Call parent constructor
114 parent::__construct($className);
118 * Private setter for all colors
120 * @param $colorMode Whether background or foreground color
121 * @param $colorChannel Red, green or blue channel?
122 * @param $colorValue Value to set
124 private final function setColor ($colorMode, $colorChannel, $colorValue) {
125 // Construct the eval() command
126 $eval = sprintf("\$this->%s['%s'] = \"%s\";",
133 //* DEBUG: */ echo "mode={$colorMode}, channel={$colorChannel}, value={$colorValue}<br />\n";
138 * Setter for image width
140 * @param $width Width of the image
143 public final function setWidth ($width) {
144 $this->width = $width;
148 * Getter for image width
150 * @return $width Width of the image
152 public final function getWidth () {
157 * Setter for image height
159 * @param $height Height of the image
162 public final function setHeight ($height) {
163 $this->height = $height;
167 * Getter for image height
169 * @return $height Height of the image
171 public final function getHeight () {
172 return $this->height;
176 * Finish the type handling (unused at the moment)
179 * @todo Find something usefull for this method.
181 public function finishType () {
182 // Empty at the momemt
186 * Prepares the class for resolution (unused at the moment)
189 * @todo Find something usefull for this method.
191 public function initResolution () {
192 // Empty at the momemt
196 * Finish resolution handling (unused at the moment)
199 * @todo Find something usefull for this method.
201 public function finishResolution () {
202 // Empty at the momemt
206 * Prepares the class for base (unused at the moment)
209 * @todo Find something usefull for this method.
211 public function initBase () {
212 // Empty at the momemt
216 * Finish base handling (unused at the moment)
219 * @todo Find something usefull for this method.
221 public function finishBase () {
222 // Empty at the momemt
226 * Prepares the class for background color
230 public function initBackgroundColor () {
231 $this->colorMode = 'backgroundColor';
235 * Finish background color handling
238 * @todo Find something usefull for this method.
240 public function finishBackgroundColor () {
241 // Empty at the moment
245 * Prepares the class for foreground color
249 public function initForegroundColor () {
250 $this->colorMode = 'foregroundColor';
254 * Finish foreground color handling
257 * @todo Find something usefull for this method.
259 public function finishForegroundColor () {
260 // Empty at the moment
264 * Prepares the class for string (unused at the moment)
266 * @param $groupable Whether this image string is groupable or single
268 * @todo Find something usefull for this method.
270 public function initImageString ($groupable = 'single') {
271 $this->groupable = $groupable;
275 * Finish string handling (unused at the moment)
278 * @todo Find something usefull for this method.
280 public function finishImageString () {
281 // Empty at the momemt
285 * Setter for red color
287 * @param $red Red color value
290 public final function setRed ($red) {
292 $arrayName = $this->colorMode;
295 $this->setColor($arrayName, 'red', $red);
299 * Setter for green color
301 * @param $green Green color value
304 public final function setGreen ($green) {
306 $arrayName = $this->colorMode;
309 $this->setColor($arrayName, 'green', $green);
313 * Setter for blue color
315 * @param $blue Blue color value
318 public final function setBlue ($blue) {
320 $arrayName = $this->colorMode;
323 $this->setColor($arrayName, 'blue', $blue);
327 * Setter for image string
329 * @param $string String to set in image
332 public final function setString ($string) {
333 $this->imageString = (string) $string;
337 * Getter for image string
339 * @return $string String to set in image
341 public final function getString () {
342 return $this->imageString;
346 * Setter for image type
348 * @param $imageType Type to set in image
351 protected final function setImageType ($imageType) {
352 $this->imageType = (string) $imageType;
356 * Getter for image type
358 * @return $imageType Type to set in image
360 public final function getImageType () {
361 return $this->imageType;
365 * Setter for image name
367 * @param $name Name of the image
370 public final function setImageName ($name) {
371 $this->imageName = (string) $name;
375 * Getter for image name
377 * @return $name Name of the image
379 public final function getImageName () {
380 return $this->imageName;
384 * Getter for image resource
386 * @return $imageResource An image resource from imagecreatetruecolor() function
388 public final function getImageResource() {
389 return $this->imageResource;
393 * Setter for X coordinate
395 * @param $x X coordinate
398 public final function setX ($x) {
403 * Getter for X coordinate
405 * @return $x X coordinate
407 public final function getX () {
412 * Setter for Y coordinate
414 * @param $y Y coordinate
417 public final function setY ($y) {
422 * Getter for Y coordinate
424 * @return $y Y coordinate
426 public final function getY () {
431 * Setter for font size
433 * @param $fontSize Font size for strings
436 public final function setFontSize ($fontSize) {
437 $this->fontSize = $fontSize;
441 * Getter for font size
443 * @return $fontSize Font size for strings
445 public final function getFontSize () {
446 return $this->fontSize;
450 * Setter for string name
452 * @param $stringName String name to set
455 public final function setStringName($stringName) {
456 $this->stringName = $stringName;
460 * Finish this image by producing it
464 public function finishImage () {
465 // Compile width and height
466 $width = $this->getTemplateInstance()->compileRawCode($this->getWidth());
467 $height = $this->getTemplateInstance()->compileRawCode($this->getHeight());
470 $this->setWidth($width);
471 $this->setHeight($height);
473 // Get a image resource
474 $this->imageResource = imagecreatetruecolor($width, $height);
476 // Compile background colors
477 $red = $this->getTemplateInstance()->compileRawCode($this->backgroundColor['red']);
478 $green = $this->getTemplateInstance()->compileRawCode($this->backgroundColor['green']);
479 $blue = $this->getTemplateInstance()->compileRawCode($this->backgroundColor['blue']);
482 $this->initBackgroundColor();
484 $this->setGreen($green);
485 $this->setBlue($blue);
487 // Get a pointer for background color
488 $backColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
491 imagefill($this->getImageResource(), 0, 0, $backColor);
493 // Compile foreground colors
494 $red = $this->getTemplateInstance()->compileRawCode($this->foregroundColor['red']);
495 $green = $this->getTemplateInstance()->compileRawCode($this->foregroundColor['green']);
496 $blue = $this->getTemplateInstance()->compileRawCode($this->foregroundColor['blue']);
499 $this->initForegroundColor();
501 $this->setGreen($green);
502 $this->setBlue($blue);
504 // Get a pointer for foreground color
505 $foreColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
507 switch ($this->groupable) {
508 case 'single': // Single image string
509 // Compile image string
510 $imageString = $this->getTemplateInstance()->compileRawCode($this->getString());
513 $this->setString($imageString);
515 // Compile X/Y coordinates and font size
516 $x = $this->getTemplateInstance()->compileRawCode($this->getX());
517 $y = $this->getTemplateInstance()->compileRawCode($this->getY());
518 $size = $this->getTemplateInstance()->compileRawCode($this->getFontSize());
520 // Set the image string
521 imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
524 case 'groupable': // More than one string allowed
525 // Walk through all groups
526 foreach ($this->getTemplateInstance()->getVariableGroups() as $group => $set) {
528 $this->getTemplateInstance()->setVariableGroup($group, false);
530 // Compile image string
531 $imageString = $this->getTemplateInstance()->compileRawCode($this->getString());
533 // Compile X/Y coordinates and font size
534 $x = $this->getTemplateInstance()->compileRawCode($this->getX());
535 $y = $this->getTemplateInstance()->compileRawCode($this->getY());
536 $size = $this->getTemplateInstance()->compileRawCode($this->getFontSize());
538 // Set the image string
539 //* DEBUG: */ print __METHOD__.": size={$size}, x={$x}, y={$y}, string={$imageString}<br />\n";
540 imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
545 // You need finishing in your image class!
549 * Getter for full created image content
551 * @return $imageContent The raw image content
553 public function getContent () {
554 // Get cache file name
555 $cacheFile = $this->getTemplateInstance()->getImageCacheFile();
557 // Open it for reading
558 $fileObject = $cacheFile->openFile('r');
560 // Rewind to beginning
561 $fileObject->rewind();
564 $imageContent = $fileObject->fread($cacheFile->getSize());
567 return $imageContent;