Comment header cosmetics applied
[shipsimu.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, this is free software
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                 // Clean up a little
94                 $this->removeNumberFormaters();
95                 $this->removeSystemArray();
96         }
97
98         /**
99          * Private setter for all colors
100          *
101          * @param       $colorMode              Wether background or foreground color
102          * @param       $colorChannel   Red, green or blue channel?
103          * @param       $colorValue             Value to set
104          */
105         private final function setColor ($colorMode, $colorChannel, $colorValue) {
106                 // Construct the eval() command
107                 $eval = sprintf("\$this->%s['%s'] = \"%s\";",
108                         $colorMode,
109                         $colorChannel,
110                         $colorValue
111                 );
112
113                 // Run the command
114                 //* DEBUG: */ echo "mode={$colorMode}, channel={$colorChannel}, value={$colorValue}<br />\n";
115                 eval($eval);
116         }
117
118         /**
119          * Setter for image width
120          *
121          * @param       $width  Width of the image
122          * @return      void
123          */
124         public final function setWidth ($width) {
125                 $this->width = $width;
126         }
127
128         /**
129          * Getter for image width
130          *
131          * @return      $width  Width of the image
132          */
133         public final function getWidth () {
134                 return $this->width;
135         }
136
137         /**
138          * Setter for image height
139          *
140          * @param       $height Height of the image
141          * @return      void
142          */
143         public final function setHeight ($height) {
144                 $this->height = $height;
145         }
146
147         /**
148          * Getter for image height
149          *
150          * @return      $height Height of the image
151          */
152         public final function getHeight () {
153                 return $this->height;
154         }
155
156         /**
157          * Finish the type handling (unused at the moment)
158          *
159          * @return      void
160          * @todo        Find something usefull for this method.
161          */
162         public function finishType () {
163                 // Empty at the momemt
164         }
165
166         /**
167          * Prepares the class for resolution (unused at the moment)
168          *
169          * @return      void
170          * @todo        Find something usefull for this method.
171          */
172         public function initResolution () {
173                 // Empty at the momemt
174         }
175
176         /**
177          * Finish resolution handling (unused at the moment)
178          *
179          * @return      void
180          * @todo        Find something usefull for this method.
181          */
182         public function finishResolution () {
183                 // Empty at the momemt
184         }
185
186         /**
187          * Prepares the class for base (unused at the moment)
188          *
189          * @return      void
190          * @todo        Find something usefull for this method.
191          */
192         public function initBase () {
193                 // Empty at the momemt
194         }
195
196         /**
197          * Finish base handling (unused at the moment)
198          *
199          * @return      void
200          * @todo        Find something usefull for this method.
201          */
202         public function finishBase () {
203                 // Empty at the momemt
204         }
205
206         /**
207          * Prepares the class for background color
208          *
209          * @return      void
210          */
211         public function initBackgroundColor () {
212                 $this->colorMode = "backgroundColor";
213         }
214
215         /**
216          * Finish background color handling
217          *
218          * @return      void
219          * @todo        Find something usefull for this method.
220          */
221         public function finishBackgroundColor () {
222                 // Empty at the moment
223         }
224
225         /**
226          * Prepares the class for foreground color
227          *
228          * @return      void
229          */
230         public function initForegroundColor () {
231                 $this->colorMode = "foregroundColor";
232         }
233
234         /**
235          * Finish foreground color handling
236          *
237          * @return      void
238          * @todo        Find something usefull for this method.
239          */
240         public function finishForegroundColor () {
241                 // Empty at the moment
242         }
243
244         /**
245          * Prepares the class for string (unused at the moment)
246          *
247          * @param       $groupable      Wether this image string is groupable or single
248          * @return      void
249          * @todo        Find something usefull for this method.
250          */
251         public function initImageString ($groupable = "single") {
252                 $this->groupable = $groupable;
253         }
254
255         /**
256          * Finish string handling (unused at the moment)
257          *
258          * @return      void
259          * @todo        Find something usefull for this method.
260          */
261         public function finishImageString () {
262                 // Empty at the momemt
263         }
264
265         /**
266          * Setter for red color
267          *
268          * @param       $red    Red color value
269          * @return      void
270          */
271         public final function setRed ($red) {
272                 // Get array name
273                 $arrayName = $this->colorMode;
274
275                 // Set image color
276                 $this->setColor($arrayName, 'red', $red);
277         }
278
279         /**
280          * Setter for green color
281          *
282          * @param       $green  Green color value
283          * @return      void
284          */
285         public final function setGreen ($green) {
286                 // Get array name
287                 $arrayName = $this->colorMode;
288
289                 // Set image color
290                 $this->setColor($arrayName, 'green', $green);
291         }
292
293         /**
294          * Setter for blue color
295          *
296          * @param       $blue   Blue color value
297          * @return      void
298          */
299         public final function setBlue ($blue) {
300                 // Get array name
301                 $arrayName = $this->colorMode;
302
303                 // Set image color
304                 $this->setColor($arrayName, 'blue', $blue);
305         }
306
307         /**
308          * Setter for image string
309          *
310          * @param       $string         String to set in image
311          * @return      void
312          */
313         public final function setString ($string) {
314                 $this->imageString = (string) $string;
315         }
316
317         /**
318          * Getter for image string
319          *
320          * @return      $string         String to set in image
321          */
322         public final function getString () {
323                 return $this->imageString;
324         }
325
326         /**
327          * Setter for image type
328          *
329          * @param       $imageType              Type to set in image
330          * @return      void
331          */
332         protected final function setImageType ($imageType) {
333                 $this->imageType = (string) $imageType;
334         }
335
336         /**
337          * Getter for image type
338          *
339          * @return      $imageType              Type to set in image
340          */
341         public final function getImageType () {
342                 return $this->imageType;
343         }
344
345         /**
346          * Setter for image name
347          *
348          * @param       $name   Name of the image
349          * @return      void
350          */
351         public final function setImageName ($name) {
352                 $this->imageName = (string) $name;
353         }
354
355         /**
356          * Getter for image name
357          *
358          * @return      $name   Name of the image
359          */
360         public final function getImageName () {
361                 return $this->imageName;
362         }
363
364         /**
365          * Getter for image resource
366          *
367          * @return      $imageResource  An image resource from imagecreatetruecolor() function
368          */
369         public final function getImageResource() {
370                 return $this->imageResource;
371         }
372
373         /**
374          * Setter for X coordinate
375          *
376          * @param       $x      X coordinate
377          * @return      void
378          */
379         public final function setX ($x) {
380                 $this->x = $x;
381         }
382
383         /**
384          * Getter for X coordinate
385          *
386          * @return      $x      X coordinate
387          */
388         public final function getX () {
389                 return $this->x;
390         }
391
392         /**
393          * Setter for Y coordinate
394          *
395          * @param       $y      Y coordinate
396          * @return      void
397          */
398         public final function setY ($y) {
399                 $this->y = $y;
400         }
401
402         /**
403          * Getter for Y coordinate
404          *
405          * @return      $y      Y coordinate
406          */
407         public final function getY () {
408                 return $this->y;
409         }
410
411         /**
412          * Setter for font size
413          *
414          * @param       $fontSize       Font size for strings
415          * @return      void
416          */
417         public final function setFontSize ($fontSize) {
418                 $this->fontSize = $fontSize;
419         }
420
421         /**
422          * Getter for font size
423          *
424          * @return      $fontSize       Font size for strings
425          */
426         public final function getFontSize () {
427                 return $this->fontSize;
428         }
429
430         /**
431          * Setter for string name
432          *
433          * @param       $stringName             String name to set
434          * @return      void
435          */
436         public final function setStringName($stringName) {
437                 $this->stringName = $stringName;
438         }
439
440         /**
441          * Finish this image by producing it
442          *
443          * @return      void
444          */
445         public function finishImage () {
446                 // Get template instance
447                 $templateInstance = $this->getTemplateInstance();
448
449                 // Compile width and height
450                 $width = $templateInstance->compileRawCode($this->getWidth());
451                 $height = $templateInstance->compileRawCode($this->getHeight());
452
453                 // Set both again
454                 $this->setWidth($width);
455                 $this->setHeight($height);
456
457                 // Get a image resource
458                 $this->imageResource = imagecreatetruecolor($width, $height);
459
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']);
464
465                 // Set all back
466                 $this->initBackgroundColor();
467                 $this->setRed($red);
468                 $this->setGreen($green);
469                 $this->setBlue($blue);
470
471                 // Get a pointer for background color
472                 $backColor = imagecolorallocate($this->imageResource, $red, $green, $blue);
473
474                 // Fill the image
475                 imagefill($this->imageResource, 0, 0, $backColor);
476
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']);
481
482                 // Set all fore
483                 $this->initForegroundColor();
484                 $this->setRed($red);
485                 $this->setGreen($green);
486                 $this->setBlue($blue);
487
488                 // Get a pointer for foreground color
489                 $foreColor = imagecolorallocate($this->imageResource, $red, $green, $blue);
490
491                 switch ($this->groupable) {
492                         case "single": // Single image string
493                                 // Compile image string
494                                 $imageString = $templateInstance->compileRawCode($this->getString());
495
496                                 // Set it back
497                                 $this->setString($imageString);
498
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());
503
504                                 // Set the image string
505                                 imagestring($this->imageResource, $size, $x, $y, $imageString, $foreColor);
506                                 break;
507
508                         case "groupable": // More than one string allowed
509                                 // Walk through all groups
510                                 foreach ($templateInstance->getVariableGroups() as $group => $set) {
511                                         // Set the group
512                                         $templateInstance->setVariableGroup($group, false);
513
514                                         // Compile image string
515                                         $imageString = $templateInstance->compileRawCode($this->getString());
516
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());
521
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);
525                                 } // END - foreach
526                                 break;
527                 }
528
529                 // You need finishing in your image class!
530         }
531
532         /**
533          * Getter for full created image content
534          *
535          * @return      $imageContent   The raw image content
536          */
537         public function getContent () {
538                 // Get cache file name
539                 $cacheFile = $this->getTemplateInstance()->getImageCacheFqfn();
540
541                 // Load the content
542                 $imageContent = file_get_contents($cacheFile);
543
544                 // And return it
545                 return $imageContent;
546         }
547 }
548 // [EOF]
549 ?>