3 * A general image class
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class BaseImage extends BaseFrameworkSystem implements Registerable {
28 private $imageType = '';
49 private $fontSize = '';
54 private $imageString = '';
57 * Background color in RGB
59 private $backgroundColor = array(
66 * Foreground color in RGB
68 private $foregroundColor = array(
75 * Current choosen color array
77 private $colorMode = '';
82 private $imageResource = NULL;
87 private $imageName = '';
92 private $stringName = '';
95 * Groupable image strings?
97 private $groupable = 'single';
100 * Protected constructor
102 * @param $className Name of the class
105 protected function __construct ($className) {
106 // Call parent constructor
107 parent::__construct($className);
111 * Private setter for all colors
113 * @param $colorMode Whether background or foreground color
114 * @param $colorChannel Red, green or blue channel?
115 * @param $colorValue Value to set
117 private final function setColor ($colorMode, $colorChannel, $colorValue) {
118 // Construct the eval() command
119 $eval = sprintf("\$this->%s['%s'] = \"%s\";",
126 //* DEBUG: */ echo "mode={$colorMode}, channel={$colorChannel}, value={$colorValue}<br />\n";
131 * Setter for image width
133 * @param $width Width of the image
136 public final function setWidth ($width) {
137 $this->width = $width;
141 * Getter for image width
143 * @return $width Width of the image
145 public final function getWidth () {
150 * Setter for image height
152 * @param $height Height of the image
155 public final function setHeight ($height) {
156 $this->height = $height;
160 * Getter for image height
162 * @return $height Height of the image
164 public final function getHeight () {
165 return $this->height;
169 * Finish the type handling (unused at the moment)
172 * @todo Find something usefull for this method.
174 public function finishType () {
175 // Empty at the momemt
179 * Prepares the class for resolution (unused at the moment)
182 * @todo Find something usefull for this method.
184 public function initResolution () {
185 // Empty at the momemt
189 * Finish resolution handling (unused at the moment)
192 * @todo Find something usefull for this method.
194 public function finishResolution () {
195 // Empty at the momemt
199 * Prepares the class for base (unused at the moment)
202 * @todo Find something usefull for this method.
204 public function initBase () {
205 // Empty at the momemt
209 * Finish base handling (unused at the moment)
212 * @todo Find something usefull for this method.
214 public function finishBase () {
215 // Empty at the momemt
219 * Prepares the class for background color
223 public function initBackgroundColor () {
224 $this->colorMode = 'backgroundColor';
228 * Finish background color handling
231 * @todo Find something usefull for this method.
233 public function finishBackgroundColor () {
234 // Empty at the moment
238 * Prepares the class for foreground color
242 public function initForegroundColor () {
243 $this->colorMode = 'foregroundColor';
247 * Finish foreground color handling
250 * @todo Find something usefull for this method.
252 public function finishForegroundColor () {
253 // Empty at the moment
257 * Prepares the class for string (unused at the moment)
259 * @param $groupable Whether this image string is groupable or single
261 * @todo Find something usefull for this method.
263 public function initImageString ($groupable = 'single') {
264 $this->groupable = $groupable;
268 * Finish string handling (unused at the moment)
271 * @todo Find something usefull for this method.
273 public function finishImageString () {
274 // Empty at the momemt
278 * Setter for red color
280 * @param $red Red color value
283 public final function setRed ($red) {
285 $arrayName = $this->colorMode;
288 $this->setColor($arrayName, 'red', $red);
292 * Setter for green color
294 * @param $green Green color value
297 public final function setGreen ($green) {
299 $arrayName = $this->colorMode;
302 $this->setColor($arrayName, 'green', $green);
306 * Setter for blue color
308 * @param $blue Blue color value
311 public final function setBlue ($blue) {
313 $arrayName = $this->colorMode;
316 $this->setColor($arrayName, 'blue', $blue);
320 * Setter for image string
322 * @param $string String to set in image
325 public final function setString ($string) {
326 $this->imageString = (string) $string;
330 * Getter for image string
332 * @return $string String to set in image
334 public final function getString () {
335 return $this->imageString;
339 * Setter for image type
341 * @param $imageType Type to set in image
344 protected final function setImageType ($imageType) {
345 $this->imageType = (string) $imageType;
349 * Getter for image type
351 * @return $imageType Type to set in image
353 public final function getImageType () {
354 return $this->imageType;
358 * Setter for image name
360 * @param $name Name of the image
363 public final function setImageName ($name) {
364 $this->imageName = (string) $name;
368 * Getter for image name
370 * @return $name Name of the image
372 public final function getImageName () {
373 return $this->imageName;
377 * Getter for image resource
379 * @return $imageResource An image resource from imagecreatetruecolor() function
381 public final function getImageResource() {
382 return $this->imageResource;
386 * Setter for X coordinate
388 * @param $x X coordinate
391 public final function setX ($x) {
396 * Getter for X coordinate
398 * @return $x X coordinate
400 public final function getX () {
405 * Setter for Y coordinate
407 * @param $y Y coordinate
410 public final function setY ($y) {
415 * Getter for Y coordinate
417 * @return $y Y coordinate
419 public final function getY () {
424 * Setter for font size
426 * @param $fontSize Font size for strings
429 public final function setFontSize ($fontSize) {
430 $this->fontSize = $fontSize;
434 * Getter for font size
436 * @return $fontSize Font size for strings
438 public final function getFontSize () {
439 return $this->fontSize;
443 * Setter for string name
445 * @param $stringName String name to set
448 public final function setStringName($stringName) {
449 $this->stringName = $stringName;
453 * Finish this image by producing it
457 public function finishImage () {
458 // Get template instance
459 $templateInstance = $this->getTemplateInstance();
461 // Compile width and height
462 $width = $templateInstance->compileRawCode($this->getWidth());
463 $height = $templateInstance->compileRawCode($this->getHeight());
466 $this->setWidth($width);
467 $this->setHeight($height);
469 // Get a image resource
470 $this->imageResource = imagecreatetruecolor($width, $height);
472 // Compile background colors
473 $red = $templateInstance->compileRawCode($this->backgroundColor['red']);
474 $green = $templateInstance->compileRawCode($this->backgroundColor['green']);
475 $blue = $templateInstance->compileRawCode($this->backgroundColor['blue']);
478 $this->initBackgroundColor();
480 $this->setGreen($green);
481 $this->setBlue($blue);
483 // Get a pointer for background color
484 $backColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
487 imagefill($this->getImageResource(), 0, 0, $backColor);
489 // Compile foreground colors
490 $red = $templateInstance->compileRawCode($this->foregroundColor['red']);
491 $green = $templateInstance->compileRawCode($this->foregroundColor['green']);
492 $blue = $templateInstance->compileRawCode($this->foregroundColor['blue']);
495 $this->initForegroundColor();
497 $this->setGreen($green);
498 $this->setBlue($blue);
500 // Get a pointer for foreground color
501 $foreColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
503 switch ($this->groupable) {
504 case 'single': // Single image string
505 // Compile image string
506 $imageString = $templateInstance->compileRawCode($this->getString());
509 $this->setString($imageString);
511 // Compile X/Y coordinates and font size
512 $x = $templateInstance->compileRawCode($this->getX());
513 $y = $templateInstance->compileRawCode($this->getY());
514 $size = $templateInstance->compileRawCode($this->getFontSize());
516 // Set the image string
517 imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
520 case 'groupable': // More than one string allowed
521 // Walk through all groups
522 foreach ($templateInstance->getVariableGroups() as $group => $set) {
524 $templateInstance->setVariableGroup($group, FALSE);
526 // Compile image string
527 $imageString = $templateInstance->compileRawCode($this->getString());
529 // Compile X/Y coordinates and font size
530 $x = $templateInstance->compileRawCode($this->getX());
531 $y = $templateInstance->compileRawCode($this->getY());
532 $size = $templateInstance->compileRawCode($this->getFontSize());
534 // Set the image string
535 //* DEBUG: */ print __METHOD__.": size={$size}, x={$x}, y={$y}, string={$imageString}<br />\n";
536 imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
541 // You need finishing in your image class!
545 * Getter for full created image content
547 * @return $imageContent The raw image content
549 public function getContent () {
550 // Get cache file name
551 $cacheFile = $this->getTemplateInstance()->getImageCacheFqfn();
554 $imageContent = file_get_contents($cacheFile);
557 return $imageContent;