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