Check for config variables first, than general to avoid a bug: If the same
[core.git] / inc / classes / main / images / class_BaseImage.php
1 <?php
2 /**
3  * A general image class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class BaseImage extends BaseFrameworkSystem implements Registerable {
25         /**
26          * Image type
27          */
28         private $imageType = '';
29
30         /**
31          * Width of the image
32          */
33         private $width = '';
34
35         /**
36          * Height of the image
37          */
38         private $height = '';
39
40         /**
41          * Background color in RGB
42          */
43         private $backgroundColor = array(
44                 'red'   => '',
45                 'green' => '',
46                 'blue'  => ''
47         );
48
49         /**
50          * Foreground color in RGB
51          */
52         private $foregroundColor = array(
53                 'red'   => '',
54                 'green' => '',
55                 'blue'  => ''
56         );
57
58         /**
59          * Current choosen color array
60          */
61         private $colorMode = '';
62
63         /**
64          * Image resource
65          */
66         private $imageResource = NULL;
67
68         /**
69          * Image name
70          */
71         private $imageName = '';
72
73         /**
74          * String name
75          */
76         private $stringName = '';
77
78         /**
79          * Groupable image strings?
80          */
81         private $groupable = 'single';
82
83         /**
84          * Protected constructor
85          *
86          * @param       $className      Name of the class
87          * @return      void
88          */
89         protected function __construct ($className) {
90                 // Call parent constructor
91                 parent::__construct($className);
92         }
93
94         /**
95          * Private setter for all colors
96          *
97          * @param       $colorMode              Whether background or foreground color
98          * @param       $colorChannel   Red, green or blue channel?
99          * @param       $colorValue             Value to set
100          */
101         private final function setColor ($colorMode, $colorChannel, $colorValue) {
102                 // Construct the eval() command
103                 $eval = sprintf("\$this->%s['%s'] = \"%s\";",
104                         $colorMode,
105                         $colorChannel,
106                         $colorValue
107                 );
108
109                 // Run the command
110                 //* DEBUG: */ echo "mode={$colorMode}, channel={$colorChannel}, value={$colorValue}<br />\n";
111                 eval($eval);
112         }
113
114         /**
115          * Setter for image width
116          *
117          * @param       $width  Width of the image
118          * @return      void
119          */
120         public final function setWidth ($width) {
121                 $this->width = $width;
122         }
123
124         /**
125          * Getter for image width
126          *
127          * @return      $width  Width of the image
128          */
129         public final function getWidth () {
130                 return $this->width;
131         }
132
133         /**
134          * Setter for image height
135          *
136          * @param       $height Height of the image
137          * @return      void
138          */
139         public final function setHeight ($height) {
140                 $this->height = $height;
141         }
142
143         /**
144          * Getter for image height
145          *
146          * @return      $height Height of the image
147          */
148         public final function getHeight () {
149                 return $this->height;
150         }
151
152         /**
153          * Finish the type handling (unused at the moment)
154          *
155          * @return      void
156          * @todo        Find something usefull for this method.
157          */
158         public function finishType () {
159                 // Empty at the momemt
160         }
161
162         /**
163          * Prepares the class for resolution (unused at the moment)
164          *
165          * @return      void
166          * @todo        Find something usefull for this method.
167          */
168         public function initResolution () {
169                 // Empty at the momemt
170         }
171
172         /**
173          * Finish resolution handling (unused at the moment)
174          *
175          * @return      void
176          * @todo        Find something usefull for this method.
177          */
178         public function finishResolution () {
179                 // Empty at the momemt
180         }
181
182         /**
183          * Prepares the class for base (unused at the moment)
184          *
185          * @return      void
186          * @todo        Find something usefull for this method.
187          */
188         public function initBase () {
189                 // Empty at the momemt
190         }
191
192         /**
193          * Finish base handling (unused at the moment)
194          *
195          * @return      void
196          * @todo        Find something usefull for this method.
197          */
198         public function finishBase () {
199                 // Empty at the momemt
200         }
201
202         /**
203          * Prepares the class for background color
204          *
205          * @return      void
206          */
207         public function initBackgroundColor () {
208                 $this->colorMode = 'backgroundColor';
209         }
210
211         /**
212          * Finish background color handling
213          *
214          * @return      void
215          * @todo        Find something usefull for this method.
216          */
217         public function finishBackgroundColor () {
218                 // Empty at the moment
219         }
220
221         /**
222          * Prepares the class for foreground color
223          *
224          * @return      void
225          */
226         public function initForegroundColor () {
227                 $this->colorMode = 'foregroundColor';
228         }
229
230         /**
231          * Finish foreground color handling
232          *
233          * @return      void
234          * @todo        Find something usefull for this method.
235          */
236         public function finishForegroundColor () {
237                 // Empty at the moment
238         }
239
240         /**
241          * Prepares the class for string (unused at the moment)
242          *
243          * @param       $groupable      Whether this image string is groupable or single
244          * @return      void
245          * @todo        Find something usefull for this method.
246          */
247         public function initImageString ($groupable = 'single') {
248                 $this->groupable = $groupable;
249         }
250
251         /**
252          * Finish string handling (unused at the moment)
253          *
254          * @return      void
255          * @todo        Find something usefull for this method.
256          */
257         public function finishImageString () {
258                 // Empty at the momemt
259         }
260
261         /**
262          * Setter for red color
263          *
264          * @param       $red    Red color value
265          * @return      void
266          */
267         public final function setRed ($red) {
268                 // Get array name
269                 $arrayName = $this->colorMode;
270
271                 // Set image color
272                 $this->setColor($arrayName, 'red', $red);
273         }
274
275         /**
276          * Setter for green color
277          *
278          * @param       $green  Green color value
279          * @return      void
280          */
281         public final function setGreen ($green) {
282                 // Get array name
283                 $arrayName = $this->colorMode;
284
285                 // Set image color
286                 $this->setColor($arrayName, 'green', $green);
287         }
288
289         /**
290          * Setter for blue color
291          *
292          * @param       $blue   Blue color value
293          * @return      void
294          */
295         public final function setBlue ($blue) {
296                 // Get array name
297                 $arrayName = $this->colorMode;
298
299                 // Set image color
300                 $this->setColor($arrayName, 'blue', $blue);
301         }
302
303         /**
304          * Setter for image string
305          *
306          * @param       $string         String to set in image
307          * @return      void
308          */
309         public final function setString ($string) {
310                 $this->imageString = (string) $string;
311         }
312
313         /**
314          * Getter for image string
315          *
316          * @return      $string         String to set in image
317          */
318         public final function getString () {
319                 return $this->imageString;
320         }
321
322         /**
323          * Setter for image type
324          *
325          * @param       $imageType              Type to set in image
326          * @return      void
327          */
328         protected final function setImageType ($imageType) {
329                 $this->imageType = (string) $imageType;
330         }
331
332         /**
333          * Getter for image type
334          *
335          * @return      $imageType              Type to set in image
336          */
337         public final function getImageType () {
338                 return $this->imageType;
339         }
340
341         /**
342          * Setter for image name
343          *
344          * @param       $name   Name of the image
345          * @return      void
346          */
347         public final function setImageName ($name) {
348                 $this->imageName = (string) $name;
349         }
350
351         /**
352          * Getter for image name
353          *
354          * @return      $name   Name of the image
355          */
356         public final function getImageName () {
357                 return $this->imageName;
358         }
359
360         /**
361          * Getter for image resource
362          *
363          * @return      $imageResource  An image resource from imagecreatetruecolor() function
364          */
365         public final function getImageResource() {
366                 return $this->imageResource;
367         }
368
369         /**
370          * Setter for X coordinate
371          *
372          * @param       $x      X coordinate
373          * @return      void
374          */
375         public final function setX ($x) {
376                 $this->x = $x;
377         }
378
379         /**
380          * Getter for X coordinate
381          *
382          * @return      $x      X coordinate
383          */
384         public final function getX () {
385                 return $this->x;
386         }
387
388         /**
389          * Setter for Y coordinate
390          *
391          * @param       $y      Y coordinate
392          * @return      void
393          */
394         public final function setY ($y) {
395                 $this->y = $y;
396         }
397
398         /**
399          * Getter for Y coordinate
400          *
401          * @return      $y      Y coordinate
402          */
403         public final function getY () {
404                 return $this->y;
405         }
406
407         /**
408          * Setter for font size
409          *
410          * @param       $fontSize       Font size for strings
411          * @return      void
412          */
413         public final function setFontSize ($fontSize) {
414                 $this->fontSize = $fontSize;
415         }
416
417         /**
418          * Getter for font size
419          *
420          * @return      $fontSize       Font size for strings
421          */
422         public final function getFontSize () {
423                 return $this->fontSize;
424         }
425
426         /**
427          * Setter for string name
428          *
429          * @param       $stringName             String name to set
430          * @return      void
431          */
432         public final function setStringName($stringName) {
433                 $this->stringName = $stringName;
434         }
435
436         /**
437          * Finish this image by producing it
438          *
439          * @return      void
440          */
441         public function finishImage () {
442                 // Get template instance
443                 $templateInstance = $this->getTemplateInstance();
444
445                 // Compile width and height
446                 $width = $templateInstance->compileRawCode($this->getWidth());
447                 $height = $templateInstance->compileRawCode($this->getHeight());
448
449                 // Set both again
450                 $this->setWidth($width);
451                 $this->setHeight($height);
452
453                 // Get a image resource
454                 $this->imageResource = imagecreatetruecolor($width, $height);
455
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']);
460
461                 // Set all back
462                 $this->initBackgroundColor();
463                 $this->setRed($red);
464                 $this->setGreen($green);
465                 $this->setBlue($blue);
466
467                 // Get a pointer for background color
468                 $backColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
469
470                 // Fill the image
471                 imagefill($this->getImageResource(), 0, 0, $backColor);
472
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']);
477
478                 // Set all fore
479                 $this->initForegroundColor();
480                 $this->setRed($red);
481                 $this->setGreen($green);
482                 $this->setBlue($blue);
483
484                 // Get a pointer for foreground color
485                 $foreColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
486
487                 switch ($this->groupable) {
488                         case 'single': // Single image string
489                                 // Compile image string
490                                 $imageString = $templateInstance->compileRawCode($this->getString());
491
492                                 // Set it back
493                                 $this->setString($imageString);
494
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());
499
500                                 // Set the image string
501                                 imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
502                                 break;
503
504                         case 'groupable': // More than one string allowed
505                                 // Walk through all groups
506                                 foreach ($templateInstance->getVariableGroups() as $group => $set) {
507                                         // Set the group
508                                         $templateInstance->setVariableGroup($group, false);
509
510                                         // Compile image string
511                                         $imageString = $templateInstance->compileRawCode($this->getString());
512
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());
517
518                                         // Set the image string
519                                         //* DEBUG: */ print __METHOD__.": size={$size}, x={$x}, y={$y}, string={$imageString}<br />\n";
520                                         imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
521                                 } // END - foreach
522                                 break;
523                 }
524
525                 // You need finishing in your image class!
526         }
527
528         /**
529          * Getter for full created image content
530          *
531          * @return      $imageContent   The raw image content
532          */
533         public function getContent () {
534                 // Get cache file name
535                 $cacheFile = $this->getTemplateInstance()->getImageCacheFqfn();
536
537                 // Load the content
538                 $imageContent = file_get_contents($cacheFile);
539
540                 // And return it
541                 return $imageContent;
542         }
543 }
544 // [EOF]
545 ?>