3 * A general image class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software
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);
94 $this->removeNumberFormaters();
95 $this->removeSystemArray();
99 * Private setter for all colors
101 * @param $colorMode Wether background or foreground color
102 * @param $colorChannel Red, green or blue channel?
103 * @param $colorValue Value to set
105 private final function setColor ($colorMode, $colorChannel, $colorValue) {
106 // Construct the eval() command
107 $eval = sprintf("\$this->%s['%s'] = \"%s\";",
114 //* DEBUG: */ echo "mode={$colorMode}, channel={$colorChannel}, value={$colorValue}<br />\n";
119 * Setter for image width
121 * @param $width Width of the image
124 public final function setWidth ($width) {
125 $this->width = $width;
129 * Getter for image width
131 * @return $width Width of the image
133 public final function getWidth () {
138 * Setter for image height
140 * @param $height Height of the image
143 public final function setHeight ($height) {
144 $this->height = $height;
148 * Getter for image height
150 * @return $height Height of the image
152 public final function getHeight () {
153 return $this->height;
157 * Finish the type handling (unused at the moment)
160 * @todo Find something usefull for this method.
162 public function finishType () {
163 // Empty at the momemt
167 * Prepares the class for resolution (unused at the moment)
170 * @todo Find something usefull for this method.
172 public function initResolution () {
173 // Empty at the momemt
177 * Finish resolution handling (unused at the moment)
180 * @todo Find something usefull for this method.
182 public function finishResolution () {
183 // Empty at the momemt
187 * Prepares the class for base (unused at the moment)
190 * @todo Find something usefull for this method.
192 public function initBase () {
193 // Empty at the momemt
197 * Finish base handling (unused at the moment)
200 * @todo Find something usefull for this method.
202 public function finishBase () {
203 // Empty at the momemt
207 * Prepares the class for background color
211 public function initBackgroundColor () {
212 $this->colorMode = "backgroundColor";
216 * Finish background color handling
219 * @todo Find something usefull for this method.
221 public function finishBackgroundColor () {
222 // Empty at the moment
226 * Prepares the class for foreground color
230 public function initForegroundColor () {
231 $this->colorMode = "foregroundColor";
235 * Finish foreground color handling
238 * @todo Find something usefull for this method.
240 public function finishForegroundColor () {
241 // Empty at the moment
245 * Prepares the class for string (unused at the moment)
247 * @param $groupable Wether this image string is groupable or single
249 * @todo Find something usefull for this method.
251 public function initImageString ($groupable = "single") {
252 $this->groupable = $groupable;
256 * Finish string handling (unused at the moment)
259 * @todo Find something usefull for this method.
261 public function finishImageString () {
262 // Empty at the momemt
266 * Setter for red color
268 * @param $red Red color value
271 public final function setRed ($red) {
273 $arrayName = $this->colorMode;
276 $this->setColor($arrayName, 'red', $red);
280 * Setter for green color
282 * @param $green Green color value
285 public final function setGreen ($green) {
287 $arrayName = $this->colorMode;
290 $this->setColor($arrayName, 'green', $green);
294 * Setter for blue color
296 * @param $blue Blue color value
299 public final function setBlue ($blue) {
301 $arrayName = $this->colorMode;
304 $this->setColor($arrayName, 'blue', $blue);
308 * Setter for image string
310 * @param $string String to set in image
313 public final function setString ($string) {
314 $this->imageString = (string) $string;
318 * Getter for image string
320 * @return $string String to set in image
322 public final function getString () {
323 return $this->imageString;
327 * Setter for image type
329 * @param $imageType Type to set in image
332 protected final function setImageType ($imageType) {
333 $this->imageType = (string) $imageType;
337 * Getter for image type
339 * @return $imageType Type to set in image
341 public final function getImageType () {
342 return $this->imageType;
346 * Setter for image name
348 * @param $name Name of the image
351 public final function setImageName ($name) {
352 $this->imageName = (string) $name;
356 * Getter for image name
358 * @return $name Name of the image
360 public final function getImageName () {
361 return $this->imageName;
365 * Getter for image resource
367 * @return $imageResource An image resource from imagecreatetruecolor() function
369 public final function getImageResource() {
370 return $this->imageResource;
374 * Setter for X coordinate
376 * @param $x X coordinate
379 public final function setX ($x) {
384 * Getter for X coordinate
386 * @return $x X coordinate
388 public final function getX () {
393 * Setter for Y coordinate
395 * @param $y Y coordinate
398 public final function setY ($y) {
403 * Getter for Y coordinate
405 * @return $y Y coordinate
407 public final function getY () {
412 * Setter for font size
414 * @param $fontSize Font size for strings
417 public final function setFontSize ($fontSize) {
418 $this->fontSize = $fontSize;
422 * Getter for font size
424 * @return $fontSize Font size for strings
426 public final function getFontSize () {
427 return $this->fontSize;
431 * Setter for string name
433 * @param $stringName String name to set
436 public final function setStringName($stringName) {
437 $this->stringName = $stringName;
441 * Finish this image by producing it
445 public function finishImage () {
446 // Get template instance
447 $templateInstance = $this->getTemplateInstance();
449 // Compile width and height
450 $width = $templateInstance->compileRawCode($this->getWidth());
451 $height = $templateInstance->compileRawCode($this->getHeight());
454 $this->setWidth($width);
455 $this->setHeight($height);
457 // Get a image resource
458 $this->imageResource = imagecreatetruecolor($width, $height);
460 // Compile background colors
461 $red = $templateInstance->compileRawCode($this->backgroundColor['red']);
462 $green = $templateInstance->compileRawCode($this->backgroundColor['green']);
463 $blue = $templateInstance->compileRawCode($this->backgroundColor['blue']);
466 $this->initBackgroundColor();
468 $this->setGreen($green);
469 $this->setBlue($blue);
471 // Get a pointer for background color
472 $backColor = imagecolorallocate($this->imageResource, $red, $green, $blue);
475 imagefill($this->imageResource, 0, 0, $backColor);
477 // Compile foreground colors
478 $red = $templateInstance->compileRawCode($this->foregroundColor['red']);
479 $green = $templateInstance->compileRawCode($this->foregroundColor['green']);
480 $blue = $templateInstance->compileRawCode($this->foregroundColor['blue']);
483 $this->initForegroundColor();
485 $this->setGreen($green);
486 $this->setBlue($blue);
488 // Get a pointer for foreground color
489 $foreColor = imagecolorallocate($this->imageResource, $red, $green, $blue);
491 switch ($this->groupable) {
492 case "single": // Single image string
493 // Compile image string
494 $imageString = $templateInstance->compileRawCode($this->getString());
497 $this->setString($imageString);
499 // Compile X/Y coordinates and font size
500 $x = $templateInstance->compileRawCode($this->getX());
501 $y = $templateInstance->compileRawCode($this->getY());
502 $size = $templateInstance->compileRawCode($this->getFontSize());
504 // Set the image string
505 imagestring($this->imageResource, $size, $x, $y, $imageString, $foreColor);
508 case "groupable": // More than one string allowed
509 // Walk through all groups
510 foreach ($templateInstance->getVariableGroups() as $group => $set) {
512 $templateInstance->setVariableGroup($group, false);
514 // Compile image string
515 $imageString = $templateInstance->compileRawCode($this->getString());
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 //* DEBUG: */ echo __METHOD__.": size={$size}, x={$x}, y={$y}, string={$imageString}<br />\n";
524 imagestring($this->imageResource, $size, $x, $y, $imageString, $foreColor);
529 // You need finishing in your image class!
533 * Getter for full created image content
535 * @return $imageContent The raw image content
537 public function getContent () {
538 // Get cache file name
539 $cacheFile = $this->getTemplateInstance()->getImageCacheFqfn();
542 $imageContent = file_get_contents($cacheFile);
545 return $imageContent;