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