Continued:
[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 use Org\Mxchange\CoreFramework\Traits\Template\CompileableTemplateTrait;
9
10 /**
11  * A general image class
12  *
13  * @author              Roland Haeder <webmaster@shipsimu.org>
14  * @version             0.0.0
15  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
16  * @license             GNU GPL 3.0 or any newer version
17  * @link                http://www.shipsimu.org
18  *
19  * This program is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  */
32 abstract class BaseImage extends BaseFrameworkSystem implements Registerable {
33         // Load traits
34         use CompileableTemplateTrait;
35
36         /**
37          * Image type
38          */
39         private $imageType = '';
40
41         /**
42          * Width of the image
43          */
44         private $width = 0;
45
46         /**
47          * Height of the image
48          */
49         private $height = 0;
50
51         /**
52          * X/Y
53          */
54         private $x = 0;
55         private $y = 0;
56
57         /**
58          * Font size
59          */
60         private $fontSize = 0;
61
62         /**
63          * Image string
64          */
65         private $imageString = '';
66
67         /**
68          * Background color in RGB
69          */
70         private $backgroundColor = [
71                 'red'   => '',
72                 'green' => '',
73                 'blue'  => ''
74         ];
75
76         /**
77          * Foreground color in RGB
78          */
79         private $foregroundColor = [
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 (string $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 (string $colorMode, string $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 (int $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 (int $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 (string $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                 // Set image color
296                 $this->setColor($this->colorMode, 'red', $red);
297         }
298
299         /**
300          * Setter for green color
301          *
302          * @param       $green  Green color value
303          * @return      void
304          */
305         public final function setGreen ($green) {
306                 // Set image color
307                 $this->setColor($this->colorMode, 'green', $green);
308         }
309
310         /**
311          * Setter for blue color
312          *
313          * @param       $blue   Blue color value
314          * @return      void
315          */
316         public final function setBlue ($blue) {
317                 // Set image color
318                 $this->setColor($this->colorMode, 'blue', $blue);
319         }
320
321         /**
322          * Setter for image string
323          *
324          * @param       $string         String to set in image
325          * @return      void
326          */
327         public final function setString (string $string) {
328                 $this->imageString = $string;
329         }
330
331         /**
332          * Getter for image string
333          *
334          * @return      $string         String to set in image
335          */
336         public final function getString () {
337                 return $this->imageString;
338         }
339
340         /**
341          * Setter for image type
342          *
343          * @param       $imageType              Type to set in image
344          * @return      void
345          */
346         protected final function setImageType (string $imageType) {
347                 $this->imageType = $imageType;
348         }
349
350         /**
351          * Getter for image type
352          *
353          * @return      $imageType              Type to set in image
354          */
355         public final function getImageType () {
356                 return $this->imageType;
357         }
358
359         /**
360          * Setter for image name
361          *
362          * @param       $name   Name of the image
363          * @return      void
364          */
365         public final function setImageName (string $name) {
366                 $this->imageName = $name;
367         }
368
369         /**
370          * Getter for image name
371          *
372          * @return      $name   Name of the image
373          */
374         public final function getImageName () {
375                 return $this->imageName;
376         }
377
378         /**
379          * Getter for image resource
380          *
381          * @return      $imageResource  An image resource from imagecreatetruecolor() function
382          */
383         public final function getImageResource() {
384                 return $this->imageResource;
385         }
386
387         /**
388          * Setter for X coordinate
389          *
390          * @param       $x      X coordinate
391          * @return      void
392          */
393         public final function setX (int $x) {
394                 $this->x = $x;
395         }
396
397         /**
398          * Getter for X coordinate
399          *
400          * @return      $x      X coordinate
401          */
402         public final function getX () {
403                 return $this->x;
404         }
405
406         /**
407          * Setter for Y coordinate
408          *
409          * @param       $y      Y coordinate
410          * @return      void
411          */
412         public final function setY (int $y) {
413                 $this->y = $y;
414         }
415
416         /**
417          * Getter for Y coordinate
418          *
419          * @return      $y      Y coordinate
420          */
421         public final function getY () {
422                 return $this->y;
423         }
424
425         /**
426          * Setter for font size
427          *
428          * @param       $fontSize       Font size for strings
429          * @return      void
430          */
431         public final function setFontSize (int $fontSize) {
432                 $this->fontSize = $fontSize;
433         }
434
435         /**
436          * Getter for font size
437          *
438          * @return      $fontSize       Font size for strings
439          */
440         public final function getFontSize () {
441                 return $this->fontSize;
442         }
443
444         /**
445          * Setter for string name
446          *
447          * @param       $stringName             String name to set
448          * @return      void
449          */
450         public final function setStringName(string $stringName) {
451                 $this->stringName = $stringName;
452         }
453
454         /**
455          * Finish this image by producing it
456          *
457          * @return      void
458          */
459         public function finishImage () {
460                 // Compile width and height
461                 $width = $this->getTemplateInstance()->compileRawCode($this->getWidth());
462                 $height = $this->getTemplateInstance()->compileRawCode($this->getHeight());
463
464                 // Set both again
465                 $this->setWidth($width);
466                 $this->setHeight($height);
467
468                 // Get a image resource
469                 $this->imageResource = imagecreatetruecolor($width, $height);
470
471                 // Compile background colors
472                 $red   = $this->getTemplateInstance()->compileRawCode($this->backgroundColor['red']);
473                 $green = $this->getTemplateInstance()->compileRawCode($this->backgroundColor['green']);
474                 $blue  = $this->getTemplateInstance()->compileRawCode($this->backgroundColor['blue']);
475
476                 // Set all back
477                 $this->initBackgroundColor();
478                 $this->setRed($red);
479                 $this->setGreen($green);
480                 $this->setBlue($blue);
481
482                 // Get a pointer for background color
483                 $backColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
484
485                 // Fill the image
486                 imagefill($this->getImageResource(), 0, 0, $backColor);
487
488                 // Compile foreground colors
489                 $red   = $this->getTemplateInstance()->compileRawCode($this->foregroundColor['red']);
490                 $green = $this->getTemplateInstance()->compileRawCode($this->foregroundColor['green']);
491                 $blue  = $this->getTemplateInstance()->compileRawCode($this->foregroundColor['blue']);
492
493                 // Set all fore
494                 $this->initForegroundColor();
495                 $this->setRed($red);
496                 $this->setGreen($green);
497                 $this->setBlue($blue);
498
499                 // Get a pointer for foreground color
500                 $foreColor = imagecolorallocate($this->getImageResource(), $red, $green, $blue);
501
502                 switch ($this->groupable) {
503                         case 'single': // Single image string
504                                 // Compile image string
505                                 $imageString = $this->getTemplateInstance()->compileRawCode($this->getString());
506
507                                 // Set it back
508                                 $this->setString($imageString);
509
510                                 // Compile X/Y coordinates and font size
511                                 $x    = $this->getTemplateInstance()->compileRawCode($this->getX());
512                                 $y    = $this->getTemplateInstance()->compileRawCode($this->getY());
513                                 $size = $this->getTemplateInstance()->compileRawCode($this->getFontSize());
514
515                                 // Set the image string
516                                 imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
517                                 break;
518
519                         case 'groupable': // More than one string allowed
520                                 // Walk through all groups
521                                 foreach ($this->getTemplateInstance()->getVariableGroups() as $group => $set) {
522                                         // Set the group
523                                         $this->getTemplateInstance()->setVariableGroup($group, false);
524
525                                         // Compile image string
526                                         $imageString = $this->getTemplateInstance()->compileRawCode($this->getString());
527
528                                         // Compile X/Y coordinates and font size
529                                         $x    = $this->getTemplateInstance()->compileRawCode($this->getX());
530                                         $y    = $this->getTemplateInstance()->compileRawCode($this->getY());
531                                         $size = $this->getTemplateInstance()->compileRawCode($this->getFontSize());
532
533                                         // Set the image string
534                                         //* DEBUG: */ print __METHOD__.": size={$size}, x={$x}, y={$y}, string={$imageString}<br />\n";
535                                         imagestring($this->getImageResource(), $size, $x, $y, $imageString, $foreColor);
536                                 }
537                                 break;
538                 }
539
540                 // You need finishing in your image class!
541         }
542
543         /**
544          * Getter for full created image content
545          *
546          * @return      $imageContent   The raw image content
547          */
548         public function getContent () {
549                 // Get cache file name
550                 $cacheFile = $this->getTemplateInstance()->getImageCacheFile();
551
552                 // Open it for reading
553                 $fileObject = $cacheFile->openFile('r');
554
555                 // Rewind to beginning
556                 $fileObject->rewind();
557
558                 // Load the content
559                 $imageContent = $fileObject->fread($cacheFile->getSize());
560
561                 // And return it
562                 return $imageContent;
563         }
564
565 }