]> git.mxchange.org Git - core.git/blob - framework/main/classes/helper/captcha/images/class_ImageHelper.php
Some updates:
[core.git] / framework / main / classes / helper / captcha / images / class_ImageHelper.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Helper\Image;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Helper\Captcha\BaseCaptcha;
7 use Org\Mxchange\CoreFramework\Helper\Template\HelpableTemplate;
8 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
9
10 /**
11  * A helper for creating images
12  *
13  * @author              Roland Haeder <webmaster@shipsimu.org>
14  * @version             0.0.0
15 <<<<<<< HEAD:framework/main/classes/helper/captcha/images/class_ImageHelper.php
16  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 =======
18  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
19 >>>>>>> Some updates::inc/main/classes/helper/captcha/images/class_ImageHelper.php
20  * @license             GNU GPL 3.0 or any newer version
21  * @link                http://www.shipsimu.org
22  *
23  * This program is free software: you can redistribute it and/or modify
24  * it under the terms of the GNU General Public License as published by
25  * the Free Software Foundation, either version 3 of the License, or
26  * (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  * GNU General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program. If not, see <http://www.gnu.org/licenses/>.
35  */
36 class ImageHelper extends BaseCaptcha implements HelpableTemplate {
37         /**
38          * The image type
39          */
40         private $imageType = 'png';
41
42         /**
43          * The image name
44          */
45         private $imageName = '';
46
47         /**
48          * Width of the image in pixel
49          */
50         private $width = 0;
51
52         /**
53          * Height of the image in pixel
54          */
55         private $height = 0;
56
57         /**
58          * Array for background color values
59          */
60         private $backgroundColor = array(
61                 'red'   => 0,
62                 'green' => 0,
63                 'blue'  => 0
64         );
65
66         /**
67          * Array for foreground color values
68          */
69         private $foregroundColor = array(
70                 'red'   => 0,
71                 'green' => 0,
72                 'blue'  => 0
73         );
74
75         /**
76          * All image strings
77          */
78         private $imageStrings = array();
79
80         /**
81          * Current string name
82          */
83         private $currString = '';
84
85         /**
86          * Base image
87          */
88         private $baseImage = '';
89
90         /**
91          * Protected constructor
92          *
93          * @return      void
94          */
95         protected function __construct () {
96                 // Call parent constructor
97                 parent::__construct(__CLASS__);
98         }
99
100         /**
101          * Creates the helper class
102          *
103          * @param       $templateInstance       An instance of a template engine
104          * @param       $imageType                      Type of the image
105          * @return      $helperInstance         A preparedf instance of this helper
106          */
107         public static final function createImageHelper (CompileableTemplate $templateInstance, $imageType) {
108                 // Get new instance
109                 $helperInstance = new ImageHelper();
110
111                 // Set template instance
112                 $helperInstance->setTemplateInstance($templateInstance);
113
114                 // Set image type (blindly)
115                 $helperInstance->setImageType($imageType);
116
117                 // Initialize RNG
118                 $helperInstance->initializeRandomNumberGenerator($templateInstance);
119
120                 // Return the prepared instance
121                 return $helperInstance;
122         }
123
124         /**
125          * Setter for image type
126          *
127          * @param       $imageType      Type of the image
128          * @return      void
129          */
130         protected final function setImageType ($imageType) {
131                 $this->imageType = (string) $imageType;
132         }
133
134         /**
135          * Getter for image name
136          *
137          * @return      $imageType      Type of the image
138          */
139         public final function getImageType () {
140                 return $this->imageType;
141         }
142
143         /**
144          * Setter for base image
145          *
146          * @param       $baseImage      A base image template
147          * @return      void
148          */
149         public final function setBaseImage ($baseImage) {
150                 $this->baseImage = (string) $baseImage;
151         }
152
153         /**
154          * Getter for base image
155          *
156          * @return      $baseImage      A base image template
157          */
158         public final function getBaseImage () {
159                 return $this->baseImage;
160         }
161
162         /**
163          * Setter for image name
164          *
165          * @param       $imageName      Name of the image
166          * @return      void
167          */
168         public final function setImageName ($imageName) {
169                 $this->imageName = (string) $imageName;
170         }
171
172         /**
173          * Getter for image name
174          *
175          * @return      $imageName      Name of the image
176          */
177         protected final function getImageName () {
178                 return $this->imageName;
179         }
180
181         /**
182          * Setter for image width
183          *
184          * @param       $width  Width of the image
185          * @return      void
186          */
187         public final function setWidth ($width) {
188                 $this->width = (int) $width;
189         }
190
191         /**
192          * Getter for image width
193          *
194          * @return      $width  Width of the image
195          */
196         public final function getWidth () {
197                 return $this->width;
198         }
199
200         /**
201          * Setter for image height
202          *
203          * @param       $height         Height of the image
204          * @return      void
205          */
206         public final function setHeight ($height) {
207                 $this->height = (int) $height;
208         }
209
210         /**
211          * Getter for image height
212          *
213          * @return      $height         Height of the image
214          */
215         public final function getHeight () {
216                 return $this->height;
217         }
218
219         /**
220          * Setter for RGB of background color
221          *
222          * @param       $red    Color value for red
223          * @param       $green  Color value for green
224          * @param       $blue   Color value for blue
225          * @return      void
226          */
227         public final function setBackgroundColorRedGreenBlue ($red, $green, $blue) {
228                 // Random numbers?
229                 if ($red === 'rand') {
230                         $red = $this->getRngInstance()->randomNumber(0, 255);
231                 } // END - if
232                 if ($green === 'rand') {
233                         $green = $this->getRngInstance()->randomNumber(0, 255);
234                 } // END - if
235                 if ($blue === 'rand') {
236                         $blue = $this->getRngInstance()->randomNumber(0, 255);
237                 } // END - if
238
239                 $this->backgroundColor['red']   = (int) $red;
240                 $this->backgroundColor['green'] = (int) $green;
241                 $this->backgroundColor['blue']  = (int) $blue;
242         }
243
244         /**
245          * Setter for RGB of foreground color
246          *
247          * @param       $red    Color value for red
248          * @param       $green  Color value for green
249          * @param       $blue   Color value for blue
250          * @return      void
251          */
252         public final function setForegroundColorRedGreenBlue ($red, $green, $blue) {
253                 // Random numbers?
254                 if ($red === 'rand') {
255                         $red = $this->getRngInstance()->randomNumber(0, 255);
256                 } // END - if
257                 if ($green === 'rand') {
258                         $green = $this->getRngInstance()->randomNumber(0, 255);
259                 } // END - if
260                 if ($blue === 'rand') {
261                         $blue = $this->getRngInstance()->randomNumber(0, 255);
262                 } // END - if
263
264                 $this->foregroundColor['red']   = (int) $red;
265                 $this->foregroundColor['green'] = (int) $green;
266                 $this->foregroundColor['blue']  = (int) $blue;
267         }
268
269         /**
270          * Adds an image string to the buffer by the given string name
271          *
272          * @param       $stringName             String name (identifier)
273          */
274         public function addTextLine ($stringName) {
275                 // Create the image string
276                 $this->imageStrings[$stringName] = array(
277                         'x'      => '',
278                         'y'      => '',
279                         'size'   => '',
280                         'string' => ''
281                 );
282
283                 // Set current string name
284                 $this->currString = $stringName;
285         }
286
287         /**
288          * Setter for image message string
289          *
290          * @param       $imageString    A message to display in image
291          * @return      void
292          */
293         public final function setImageString ($imageString) {
294                 $this->imageStrings[$this->currString]['string'] = (string) $imageString;
295         }
296
297         /**
298          * Getter for image message string
299          *
300          * @return      $imageString    A message to display in image
301          */
302         public final function getImageString () {
303                 return $this->imageStrings[$this->currString]['string'];
304         }
305
306         /**
307          * Setter for X/Y coordinates for strings
308          *
309          * @param       $x      X coordinate
310          * @param       $y      Y coordinate
311          * @return      void
312          */
313         public final function setCoord ($x, $y) {
314                 $this->imageStrings[$this->currString]['x'] = (int) $x;
315                 $this->imageStrings[$this->currString]['y'] = (int) $y;
316         }
317
318         /**
319          * Getter for X coordinate
320          *
321          * @return      $x      X coordinate
322          */
323         public final function getX () {
324                 return $this->imageStrings[$this->currString]['x'];
325         }
326
327         /**
328          * Getter for Y coordinate
329          *
330          * @return      $y      Y coordinate
331          */
332         public final function getY () {
333                 return $this->imageStrings[$this->currString]['y'];
334         }
335
336         /**
337          * Setter for font size
338          *
339          * @param       $fontSize       Font size for strings
340          * @return      void
341          */
342         public final function setFontSize ($fontSize) {
343                 // Random font size?
344                 if ($fontSize === 'rand') {
345                         $fontSize = $this->getRngInstance()->randomNumber(4, 9);
346                 } // END - if
347
348                 $this->imageStrings[$this->currString]['size'] = (int) $fontSize;
349         }
350
351         /**
352          * Getter for font size
353          *
354          * @return      $fontSize       Font size for strings
355          */
356         public final function getFontSize () {
357                 return $this->imageStrings[$this->currString]['size'];
358         }
359
360         /**
361          * Flushs the content out
362          *
363          * @return      void
364          */
365         public function flushContent () {
366                 // Get a template instance
367                 $templateInstance = $this->getTemplateInstance();
368
369                 // Get the base image
370                 $templateInstance->loadImageTemplate($this->getBaseImage());
371
372                 // Assign all the image values with the template
373                 $templateInstance->assignVariable('image_name'    , $this->getImageName());
374                 $templateInstance->assignVariable('image_type'    , $this->getImageType());
375                 $templateInstance->assignVariable('image_width'   , $this->getWidth());
376                 $templateInstance->assignVariable('image_height'  , $this->getHeight());
377                 $templateInstance->assignVariable('image_bg_red'  , $this->backgroundColor['red']);
378                 $templateInstance->assignVariable('image_bg_green', $this->backgroundColor['green']);
379                 $templateInstance->assignVariable('image_bg_blue' , $this->backgroundColor['blue']);
380                 $templateInstance->assignVariable('image_fg_red'  , $this->foregroundColor['red']);
381                 $templateInstance->assignVariable('image_fg_green', $this->foregroundColor['green']);
382                 $templateInstance->assignVariable('image_fg_blue' , $this->foregroundColor['blue']);
383
384                 // Add all strings
385                 foreach ($this->imageStrings as $id => $imageString) {
386                         // Set current string id to keep this helper in sync with template engine
387                         $this->currString = $id;
388
389                         // Set variable group
390                         $templateInstance->setVariableGroup($id);
391
392                         // Add group variables
393                         $templateInstance->addGroupVariable('image_x'     , $this->getX());
394                         $templateInstance->addGroupVariable('image_y'     , $this->getY());
395                         $templateInstance->addGroupVariable('image_size'  , $this->getFontSize());
396                         $templateInstance->addGroupVariable('image_string', $this->getImageString());
397                 } // END - foreach
398
399                 // Get the raw content
400                 $imageContent = $templateInstance->getRawTemplateData();
401
402                 // Transfer all to the template engine
403                 $templateInstance->renderXmlContent($imageContent);
404         }
405
406 }