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