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