3 * A general image class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.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 = '';
41 * Background color in RGB
43 private $backgroundColor = array(
50 * Foreground color in RGB
52 private $foregroundColor = array(
59 * Current choosen color array
61 private $colorMode = '';
66 private $imageResource = null;
71 private $imageName = '';
76 private $stringName = '';
79 * Groupable image strings?
81 private $groupable = 'single';
84 * Protected constructor
86 * @param $className Name of the class
89 protected function __construct ($className) {
90 // Call parent constructor
91 parent::__construct($className);
95 * Private setter for all colors
97 * @param $colorMode Wether background or foreground color
98 * @param $colorChannel Red, green or blue channel?
99 * @param $colorValue Value to set
101 private final function setColor ($colorMode, $colorChannel, $colorValue) {
102 // Construct the eval() command
103 $eval = sprintf("\$this->%s['%s'] = \"%s\";",
110 //* DEBUG: */ echo "mode={$colorMode}, channel={$colorChannel}, value={$colorValue}<br />\n";
115 * Setter for image width
117 * @param $width Width of the image
120 public final function setWidth ($width) {
121 $this->width = $width;
125 * Getter for image width
127 * @return $width Width of the image
129 public final function getWidth () {
134 * Setter for image height
136 * @param $height Height of the image
139 public final function setHeight ($height) {
140 $this->height = $height;
144 * Getter for image height
146 * @return $height Height of the image
148 public final function getHeight () {
149 return $this->height;
153 * Finish the type handling (unused at the moment)
156 * @todo Find something usefull for this method.
158 public function finishType () {
159 // Empty at the momemt
163 * Prepares the class for resolution (unused at the moment)
166 * @todo Find something usefull for this method.
168 public function initResolution () {
169 // Empty at the momemt
173 * Finish resolution handling (unused at the moment)
176 * @todo Find something usefull for this method.
178 public function finishResolution () {
179 // Empty at the momemt
183 * Prepares the class for base (unused at the moment)
186 * @todo Find something usefull for this method.
188 public function initBase () {
189 // Empty at the momemt
193 * Finish base handling (unused at the moment)
196 * @todo Find something usefull for this method.
198 public function finishBase () {
199 // Empty at the momemt
203 * Prepares the class for background color
207 public function initBackgroundColor () {
208 $this->colorMode = 'backgroundColor';
212 * Finish background color handling
215 * @todo Find something usefull for this method.
217 public function finishBackgroundColor () {
218 // Empty at the moment
222 * Prepares the class for foreground color
226 public function initForegroundColor () {
227 $this->colorMode = 'foregroundColor';
231 * Finish foreground color handling
234 * @todo Find something usefull for this method.
236 public function finishForegroundColor () {
237 // Empty at the moment
241 * Prepares the class for string (unused at the moment)
243 * @param $groupable Wether this image string is groupable or single
245 * @todo Find something usefull for this method.
247 public function initImageString ($groupable = 'single') {
248 $this->groupable = $groupable;
252 * Finish string handling (unused at the moment)
255 * @todo Find something usefull for this method.
257 public function finishImageString () {
258 // Empty at the momemt
262 * Setter for red color
264 * @param $red Red color value
267 public final function setRed ($red) {
269 $arrayName = $this->colorMode;
272 $this->setColor($arrayName, 'red', $red);
276 * Setter for green color
278 * @param $green Green color value
281 public final function setGreen ($green) {
283 $arrayName = $this->colorMode;
286 $this->setColor($arrayName, 'green', $green);
290 * Setter for blue color
292 * @param $blue Blue color value
295 public final function setBlue ($blue) {
297 $arrayName = $this->colorMode;
300 $this->setColor($arrayName, 'blue', $blue);
304 * Setter for image string
306 * @param $string String to set in image
309 public final function setString ($string) {
310 $this->imageString = (string) $string;
314 * Getter for image string
316 * @return $string String to set in image
318 public final function getString () {
319 return $this->imageString;
323 * Setter for image type
325 * @param $imageType Type to set in image
328 protected final function setImageType ($imageType) {
329 $this->imageType = (string) $imageType;
333 * Getter for image type
335 * @return $imageType Type to set in image
337 public final function getImageType () {
338 return $this->imageType;
342 * Setter for image name
344 * @param $name Name of the image
347 public final function setImageName ($name) {
348 $this->imageName = (string) $name;
352 * Getter for image name
354 * @return $name Name of the image
356 public final function getImageName () {
357 return $this->imageName;
361 * Getter for image resource
363 * @return $imageResource An image resource from imagecreatetruecolor() function
365 public final function getImageResource() {
366 return $this->imageResource;
370 * Setter for X coordinate
372 * @param $x X coordinate
375 public final function setX ($x) {
380 * Getter for X coordinate
382 * @return $x X coordinate
384 public final function getX () {
389 * Setter for Y coordinate
391 * @param $y Y coordinate
394 public final function setY ($y) {
399 * Getter for Y coordinate
401 * @return $y Y coordinate
403 public final function getY () {
408 * Setter for font size
410 * @param $fontSize Font size for strings
413 public final function setFontSize ($fontSize) {
414 $this->fontSize = $fontSize;
418 * Getter for font size
420 * @return $fontSize Font size for strings
422 public final function getFontSize () {
423 return $this->fontSize;
427 * Setter for string name
429 * @param $stringName String name to set
432 public final function setStringName($stringName) {
433 $this->stringName = $stringName;
437 * Finish this image by producing it
441 public function finishImage () {
442 // Get template instance
443 $templateInstance = $this->getTemplateInstance();
445 // Compile width and height
446 $width = $templateInstance->compileRawCode($this->getWidth());
447 $height = $templateInstance->compileRawCode($this->getHeight());
450 $this->setWidth($width);
451 $this->setHeight($height);
453 // Get a image resource
454 $this->imageResource = imagecreatetruecolor($width, $height);
456 // Compile background colors
457 $red = $templateInstance->compileRawCode($this->backgroundColor['red']);
458 $green = $templateInstance->compileRawCode($this->backgroundColor['green']);
459 $blue = $templateInstance->compileRawCode($this->backgroundColor['blue']);
462 $this->initBackgroundColor();
464 $this->setGreen($green);
465 $this->setBlue($blue);
467 // Get a pointer for background color
468 $backColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
471 imagefill($this->getImageResource(), 0, 0, $backColor);
473 // Compile foreground colors
474 $red = $templateInstance->compileRawCode($this->foregroundColor['red']);
475 $green = $templateInstance->compileRawCode($this->foregroundColor['green']);
476 $blue = $templateInstance->compileRawCode($this->foregroundColor['blue']);
479 $this->initForegroundColor();
481 $this->setGreen($green);
482 $this->setBlue($blue);
484 // Get a pointer for foreground color
485 $foreColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
487 switch ($this->groupable) {
488 case 'single': // Single image string
489 // Compile image string
490 $imageString = $templateInstance->compileRawCode($this->getString());
493 $this->setString($imageString);
495 // Compile X/Y coordinates and font size
496 $x = $templateInstance->compileRawCode($this->getX());
497 $y = $templateInstance->compileRawCode($this->getY());
498 $size = $templateInstance->compileRawCode($this->getFontSize());
500 // Set the image string
501 imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
504 case 'groupable': // More than one string allowed
505 // Walk through all groups
506 foreach ($templateInstance->getVariableGroups() as $group => $set) {
508 $templateInstance->setVariableGroup($group, false);
510 // Compile image string
511 $imageString = $templateInstance->compileRawCode($this->getString());
513 // Compile X/Y coordinates and font size
514 $x = $templateInstance->compileRawCode($this->getX());
515 $y = $templateInstance->compileRawCode($this->getY());
516 $size = $templateInstance->compileRawCode($this->getFontSize());
518 // Set the image string
519 //* DEBUG: */ echo __METHOD__.": size={$size}, x={$x}, y={$y}, string={$imageString}<br />\n";
520 imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
525 // You need finishing in your image class!
529 * Getter for full created image content
531 * @return $imageContent The raw image content
533 public function getContent () {
534 // Get cache file name
535 $cacheFile = $this->getTemplateInstance()->getImageCacheFqfn();
538 $imageContent = file_get_contents($cacheFile);
541 return $imageContent;