3 namespace CoreFramework\Image;
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
9 * A general image class
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class BaseImage extends BaseFrameworkSystem implements Registerable {
34 private $imageType = '';
55 private $fontSize = '';
60 private $imageString = '';
63 * Background color in RGB
65 private $backgroundColor = array(
72 * Foreground color in RGB
74 private $foregroundColor = array(
81 * Current choosen color array
83 private $colorMode = '';
88 private $imageResource = NULL;
93 private $imageName = '';
98 private $stringName = '';
101 * Groupable image strings?
103 private $groupable = 'single';
106 * Protected constructor
108 * @param $className Name of the class
111 protected function __construct ($className) {
112 // Call parent constructor
113 parent::__construct($className);
117 * Private setter for all colors
119 * @param $colorMode Whether background or foreground color
120 * @param $colorChannel Red, green or blue channel?
121 * @param $colorValue Value to set
123 private final function setColor ($colorMode, $colorChannel, $colorValue) {
124 // Construct the eval() command
125 $eval = sprintf("\$this->%s['%s'] = \"%s\";",
132 //* DEBUG: */ echo "mode={$colorMode}, channel={$colorChannel}, value={$colorValue}<br />\n";
137 * Setter for image width
139 * @param $width Width of the image
142 public final function setWidth ($width) {
143 $this->width = $width;
147 * Getter for image width
149 * @return $width Width of the image
151 public final function getWidth () {
156 * Setter for image height
158 * @param $height Height of the image
161 public final function setHeight ($height) {
162 $this->height = $height;
166 * Getter for image height
168 * @return $height Height of the image
170 public final function getHeight () {
171 return $this->height;
175 * Finish the type handling (unused at the moment)
178 * @todo Find something usefull for this method.
180 public function finishType () {
181 // Empty at the momemt
185 * Prepares the class for resolution (unused at the moment)
188 * @todo Find something usefull for this method.
190 public function initResolution () {
191 // Empty at the momemt
195 * Finish resolution handling (unused at the moment)
198 * @todo Find something usefull for this method.
200 public function finishResolution () {
201 // Empty at the momemt
205 * Prepares the class for base (unused at the moment)
208 * @todo Find something usefull for this method.
210 public function initBase () {
211 // Empty at the momemt
215 * Finish base handling (unused at the moment)
218 * @todo Find something usefull for this method.
220 public function finishBase () {
221 // Empty at the momemt
225 * Prepares the class for background color
229 public function initBackgroundColor () {
230 $this->colorMode = 'backgroundColor';
234 * Finish background color handling
237 * @todo Find something usefull for this method.
239 public function finishBackgroundColor () {
240 // Empty at the moment
244 * Prepares the class for foreground color
248 public function initForegroundColor () {
249 $this->colorMode = 'foregroundColor';
253 * Finish foreground color handling
256 * @todo Find something usefull for this method.
258 public function finishForegroundColor () {
259 // Empty at the moment
263 * Prepares the class for string (unused at the moment)
265 * @param $groupable Whether this image string is groupable or single
267 * @todo Find something usefull for this method.
269 public function initImageString ($groupable = 'single') {
270 $this->groupable = $groupable;
274 * Finish string handling (unused at the moment)
277 * @todo Find something usefull for this method.
279 public function finishImageString () {
280 // Empty at the momemt
284 * Setter for red color
286 * @param $red Red color value
289 public final function setRed ($red) {
291 $arrayName = $this->colorMode;
294 $this->setColor($arrayName, 'red', $red);
298 * Setter for green color
300 * @param $green Green color value
303 public final function setGreen ($green) {
305 $arrayName = $this->colorMode;
308 $this->setColor($arrayName, 'green', $green);
312 * Setter for blue color
314 * @param $blue Blue color value
317 public final function setBlue ($blue) {
319 $arrayName = $this->colorMode;
322 $this->setColor($arrayName, 'blue', $blue);
326 * Setter for image string
328 * @param $string String to set in image
331 public final function setString ($string) {
332 $this->imageString = (string) $string;
336 * Getter for image string
338 * @return $string String to set in image
340 public final function getString () {
341 return $this->imageString;
345 * Setter for image type
347 * @param $imageType Type to set in image
350 protected final function setImageType ($imageType) {
351 $this->imageType = (string) $imageType;
355 * Getter for image type
357 * @return $imageType Type to set in image
359 public final function getImageType () {
360 return $this->imageType;
364 * Setter for image name
366 * @param $name Name of the image
369 public final function setImageName ($name) {
370 $this->imageName = (string) $name;
374 * Getter for image name
376 * @return $name Name of the image
378 public final function getImageName () {
379 return $this->imageName;
383 * Getter for image resource
385 * @return $imageResource An image resource from imagecreatetruecolor() function
387 public final function getImageResource() {
388 return $this->imageResource;
392 * Setter for X coordinate
394 * @param $x X coordinate
397 public final function setX ($x) {
402 * Getter for X coordinate
404 * @return $x X coordinate
406 public final function getX () {
411 * Setter for Y coordinate
413 * @param $y Y coordinate
416 public final function setY ($y) {
421 * Getter for Y coordinate
423 * @return $y Y coordinate
425 public final function getY () {
430 * Setter for font size
432 * @param $fontSize Font size for strings
435 public final function setFontSize ($fontSize) {
436 $this->fontSize = $fontSize;
440 * Getter for font size
442 * @return $fontSize Font size for strings
444 public final function getFontSize () {
445 return $this->fontSize;
449 * Setter for string name
451 * @param $stringName String name to set
454 public final function setStringName($stringName) {
455 $this->stringName = $stringName;
459 * Finish this image by producing it
463 public function finishImage () {
464 // Get template instance
465 $templateInstance = $this->getTemplateInstance();
467 // Compile width and height
468 $width = $templateInstance->compileRawCode($this->getWidth());
469 $height = $templateInstance->compileRawCode($this->getHeight());
472 $this->setWidth($width);
473 $this->setHeight($height);
475 // Get a image resource
476 $this->imageResource = imagecreatetruecolor($width, $height);
478 // Compile background colors
479 $red = $templateInstance->compileRawCode($this->backgroundColor['red']);
480 $green = $templateInstance->compileRawCode($this->backgroundColor['green']);
481 $blue = $templateInstance->compileRawCode($this->backgroundColor['blue']);
484 $this->initBackgroundColor();
486 $this->setGreen($green);
487 $this->setBlue($blue);
489 // Get a pointer for background color
490 $backColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
493 imagefill($this->getImageResource(), 0, 0, $backColor);
495 // Compile foreground colors
496 $red = $templateInstance->compileRawCode($this->foregroundColor['red']);
497 $green = $templateInstance->compileRawCode($this->foregroundColor['green']);
498 $blue = $templateInstance->compileRawCode($this->foregroundColor['blue']);
501 $this->initForegroundColor();
503 $this->setGreen($green);
504 $this->setBlue($blue);
506 // Get a pointer for foreground color
507 $foreColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
509 switch ($this->groupable) {
510 case 'single': // Single image string
511 // Compile image string
512 $imageString = $templateInstance->compileRawCode($this->getString());
515 $this->setString($imageString);
517 // Compile X/Y coordinates and font size
518 $x = $templateInstance->compileRawCode($this->getX());
519 $y = $templateInstance->compileRawCode($this->getY());
520 $size = $templateInstance->compileRawCode($this->getFontSize());
522 // Set the image string
523 imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
526 case 'groupable': // More than one string allowed
527 // Walk through all groups
528 foreach ($templateInstance->getVariableGroups() as $group => $set) {
530 $templateInstance->setVariableGroup($group, FALSE);
532 // Compile image string
533 $imageString = $templateInstance->compileRawCode($this->getString());
535 // Compile X/Y coordinates and font size
536 $x = $templateInstance->compileRawCode($this->getX());
537 $y = $templateInstance->compileRawCode($this->getY());
538 $size = $templateInstance->compileRawCode($this->getFontSize());
540 // Set the image string
541 //* DEBUG: */ print __METHOD__.": size={$size}, x={$x}, y={$y}, string={$imageString}<br />\n";
542 imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
547 // You need finishing in your image class!
551 * Getter for full created image content
553 * @return $imageContent The raw image content
555 public function getContent () {
556 // Get cache file name
557 $cacheFile = $this->getTemplateInstance()->getImageCacheFqfn();
560 $imageContent = file_get_contents($cacheFile);
563 return $imageContent;