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