3 namespace CoreFramework\Image;
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
7 use CoreFramework\Registry\Registerable;
10 * A general image class
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 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 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 ($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 // Get template instance
466 $templateInstance = $this->getTemplateInstance();
468 // Compile width and height
469 $width = $templateInstance->compileRawCode($this->getWidth());
470 $height = $templateInstance->compileRawCode($this->getHeight());
473 $this->setWidth($width);
474 $this->setHeight($height);
476 // Get a image resource
477 $this->imageResource = imagecreatetruecolor($width, $height);
479 // Compile background colors
480 $red = $templateInstance->compileRawCode($this->backgroundColor['red']);
481 $green = $templateInstance->compileRawCode($this->backgroundColor['green']);
482 $blue = $templateInstance->compileRawCode($this->backgroundColor['blue']);
485 $this->initBackgroundColor();
487 $this->setGreen($green);
488 $this->setBlue($blue);
490 // Get a pointer for background color
491 $backColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
494 imagefill($this->getImageResource(), 0, 0, $backColor);
496 // Compile foreground colors
497 $red = $templateInstance->compileRawCode($this->foregroundColor['red']);
498 $green = $templateInstance->compileRawCode($this->foregroundColor['green']);
499 $blue = $templateInstance->compileRawCode($this->foregroundColor['blue']);
502 $this->initForegroundColor();
504 $this->setGreen($green);
505 $this->setBlue($blue);
507 // Get a pointer for foreground color
508 $foreColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
510 switch ($this->groupable) {
511 case 'single': // Single image string
512 // Compile image string
513 $imageString = $templateInstance->compileRawCode($this->getString());
516 $this->setString($imageString);
518 // Compile X/Y coordinates and font size
519 $x = $templateInstance->compileRawCode($this->getX());
520 $y = $templateInstance->compileRawCode($this->getY());
521 $size = $templateInstance->compileRawCode($this->getFontSize());
523 // Set the image string
524 imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
527 case 'groupable': // More than one string allowed
528 // Walk through all groups
529 foreach ($templateInstance->getVariableGroups() as $group => $set) {
531 $templateInstance->setVariableGroup($group, false);
533 // Compile image string
534 $imageString = $templateInstance->compileRawCode($this->getString());
536 // Compile X/Y coordinates and font size
537 $x = $templateInstance->compileRawCode($this->getX());
538 $y = $templateInstance->compileRawCode($this->getY());
539 $size = $templateInstance->compileRawCode($this->getFontSize());
541 // Set the image string
542 //* DEBUG: */ print __METHOD__.": size={$size}, x={$x}, y={$y}, string={$imageString}<br />\n";
543 imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
548 // You need finishing in your image class!
552 * Getter for full created image content
554 * @return $imageContent The raw image content
556 public function getContent () {
557 // Get cache file name
558 $cacheFile = $this->getTemplateInstance()->getImageCacheFqfn();
561 $imageContent = file_get_contents($cacheFile);
564 return $imageContent;