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