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 <<<<<<< HEAD:framework/main/classes/images/class_BaseImage.php
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
18 >>>>>>> Some updates::inc/main/classes/images/class_BaseImage.php
19 * @license GNU GPL 3.0 or any newer version
20 * @link http://www.shipsimu.org
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 abstract class BaseImage extends BaseFrameworkSystem implements Registerable {
39 private $imageType = '';
60 private $fontSize = '';
65 private $imageString = '';
68 * Background color in RGB
70 private $backgroundColor = array(
77 * Foreground color in RGB
79 private $foregroundColor = array(
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 ($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 ($colorMode, $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 ($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 ($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 ($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 $arrayName = $this->colorMode;
299 $this->setColor($arrayName, 'red', $red);
303 * Setter for green color
305 * @param $green Green color value
308 public final function setGreen ($green) {
310 $arrayName = $this->colorMode;
313 $this->setColor($arrayName, 'green', $green);
317 * Setter for blue color
319 * @param $blue Blue color value
322 public final function setBlue ($blue) {
324 $arrayName = $this->colorMode;
327 $this->setColor($arrayName, 'blue', $blue);
331 * Setter for image string
333 * @param $string String to set in image
336 public final function setString ($string) {
337 $this->imageString = (string) $string;
341 * Getter for image string
343 * @return $string String to set in image
345 public final function getString () {
346 return $this->imageString;
350 * Setter for image type
352 * @param $imageType Type to set in image
355 protected final function setImageType ($imageType) {
356 $this->imageType = (string) $imageType;
360 * Getter for image type
362 * @return $imageType Type to set in image
364 public final function getImageType () {
365 return $this->imageType;
369 * Setter for image name
371 * @param $name Name of the image
374 public final function setImageName ($name) {
375 $this->imageName = (string) $name;
379 * Getter for image name
381 * @return $name Name of the image
383 public final function getImageName () {
384 return $this->imageName;
388 * Getter for image resource
390 * @return $imageResource An image resource from imagecreatetruecolor() function
392 public final function getImageResource() {
393 return $this->imageResource;
397 * Setter for X coordinate
399 * @param $x X coordinate
402 public final function setX ($x) {
407 * Getter for X coordinate
409 * @return $x X coordinate
411 public final function getX () {
416 * Setter for Y coordinate
418 * @param $y Y coordinate
421 public final function setY ($y) {
426 * Getter for Y coordinate
428 * @return $y Y coordinate
430 public final function getY () {
435 * Setter for font size
437 * @param $fontSize Font size for strings
440 public final function setFontSize ($fontSize) {
441 $this->fontSize = $fontSize;
445 * Getter for font size
447 * @return $fontSize Font size for strings
449 public final function getFontSize () {
450 return $this->fontSize;
454 * Setter for string name
456 * @param $stringName String name to set
459 public final function setStringName($stringName) {
460 $this->stringName = $stringName;
464 * Finish this image by producing it
468 public function finishImage () {
469 // Get template instance
470 $templateInstance = $this->getTemplateInstance();
472 // Compile width and height
473 $width = $templateInstance->compileRawCode($this->getWidth());
474 $height = $templateInstance->compileRawCode($this->getHeight());
477 $this->setWidth($width);
478 $this->setHeight($height);
480 // Get a image resource
481 $this->imageResource = imagecreatetruecolor($width, $height);
483 // Compile background colors
484 $red = $templateInstance->compileRawCode($this->backgroundColor['red']);
485 $green = $templateInstance->compileRawCode($this->backgroundColor['green']);
486 $blue = $templateInstance->compileRawCode($this->backgroundColor['blue']);
489 $this->initBackgroundColor();
491 $this->setGreen($green);
492 $this->setBlue($blue);
494 // Get a pointer for background color
495 $backColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
498 imagefill($this->getImageResource(), 0, 0, $backColor);
500 // Compile foreground colors
501 $red = $templateInstance->compileRawCode($this->foregroundColor['red']);
502 $green = $templateInstance->compileRawCode($this->foregroundColor['green']);
503 $blue = $templateInstance->compileRawCode($this->foregroundColor['blue']);
506 $this->initForegroundColor();
508 $this->setGreen($green);
509 $this->setBlue($blue);
511 // Get a pointer for foreground color
512 $foreColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
514 switch ($this->groupable) {
515 case 'single': // Single image string
516 // Compile image string
517 $imageString = $templateInstance->compileRawCode($this->getString());
520 $this->setString($imageString);
522 // Compile X/Y coordinates and font size
523 $x = $templateInstance->compileRawCode($this->getX());
524 $y = $templateInstance->compileRawCode($this->getY());
525 $size = $templateInstance->compileRawCode($this->getFontSize());
527 // Set the image string
528 imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
531 case 'groupable': // More than one string allowed
532 // Walk through all groups
533 foreach ($templateInstance->getVariableGroups() as $group => $set) {
535 $templateInstance->setVariableGroup($group, false);
537 // Compile image string
538 $imageString = $templateInstance->compileRawCode($this->getString());
540 // Compile X/Y coordinates and font size
541 $x = $templateInstance->compileRawCode($this->getX());
542 $y = $templateInstance->compileRawCode($this->getY());
543 $size = $templateInstance->compileRawCode($this->getFontSize());
545 // Set the image string
546 //* DEBUG: */ print __METHOD__.": size={$size}, x={$x}, y={$y}, string={$imageString}<br />\n";
547 imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
552 // You need finishing in your image class!
556 * Getter for full created image content
558 * @return $imageContent The raw image content
560 public function getContent () {
561 // Get cache file name
562 $cacheFile = $this->getTemplateInstance()->getImageCacheFile();
564 // Open it for reading
565 $fileObject = $cacheFile->openFile('r');
567 // Rewind to beginning
568 $fileObject->rewind();
571 $imageContent = $fileObject->fread($cacheFile->getSize());
574 return $imageContent;