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