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