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