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