Image generator added, first CAPTCHA added with missing controller (partly work)
[shipsimu.git] / inc / classes / main / helper / 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 BaseHelper 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 class
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                 // 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                 $this->backgroundColor['red']   = (int) $red;
212                 $this->backgroundColor['green'] = (int) $green;
213                 $this->backgroundColor['blue']  = (int) $blue;
214         }
215
216         /**
217          * Setter for RGB of foreground color
218          *
219          * @param       $red    Color value for red
220          * @param       $green  Color value for green
221          * @param       $blue   Color value for blue
222          * @return      void
223          */
224         public final function setForegroundColorRedGreenBlue ($red, $green, $blue) {
225                 $this->foregroundColor['red']   = (int) $red;
226                 $this->foregroundColor['green'] = (int) $green;
227                 $this->foregroundColor['blue']  = (int) $blue;
228         }
229
230         /**
231          * Adds an image string to the buffer by the given string name
232          *
233          * @param       $stringName             String name (identifier)
234          */
235         public function addTextLine ($stringName) {
236                 // Create the image string
237                 $this->imageStrings[$stringName] = array(
238                         'x'      => "",
239                         'y'      => "",
240                         'size'   => "",
241                         'string' => ""
242                 );
243
244                 // Set current string name
245                 $this->currString = $stringName;
246         }
247
248         /**
249          * Setter for image message string
250          *
251          * @param       $imageString    A message to display in the image
252          * @return      void
253          */
254         public final function setImageString ($imageString) {
255                 $this->imageStrings[$this->currString]['string'] = (string) $imageString;
256         }
257
258         /**
259          * Getter for image message string
260          *
261          * @return      $imageString    A message to display in the image
262          */
263         public final function getImageString () {
264                 return $this->imageStrings[$this->currString]['string'];
265         }
266
267         /**
268          * Setter for X/Y coordinates for strings
269          *
270          * @param       $x      X coordinate
271          * @param       $y      Y coordinate
272          * @return      void
273          */
274         public final function setCoord ($x, $y) {
275                 $this->imageStrings[$this->currString]['x'] = (int) $x;
276                 $this->imageStrings[$this->currString]['y'] = (int) $y;
277         }
278
279         /**
280          * Getter for X coordinate
281          *
282          * @return      $x      X coordinate
283          */
284         public final function getX () {
285                 return $this->imageStrings[$this->currString]['x'];
286         }
287
288         /**
289          * Getter for Y coordinate
290          *
291          * @return      $y      Y coordinate
292          */
293         public final function getY () {
294                 return $this->imageStrings[$this->currString]['y'];
295         }
296
297         /**
298          * Setter for font size
299          *
300          * @param       $fontSize       Font size for strings
301          * @return      void
302          */
303         public final function setFontSize ($fontSize) {
304                 $this->imageStrings[$this->currString]['size'] = (int) $fontSize;
305         }
306
307         /**
308          * Getter for font size
309          *
310          * @return      $fontSize       Font size for strings
311          */
312         public final function getFontSize () {
313                 return $this->imageStrings[$this->currString]['size'];
314         }
315
316         /**
317          * Flushs the content out
318          *
319          * @return      void
320          */
321         public function flushContent () {
322                 // Get a template instance
323                 $templateInstance = $this->getTemplateInstance();
324
325                 // Get the base image
326                 $templateInstance->loadCodeTemplate($this->getBaseImage());
327
328                 // Assign all the image values with the template
329                 $templateInstance->assignVariable("image_name"    , $this->getImageName());
330                 $templateInstance->assignVariable("image_type"    , $this->getImageType());
331                 $templateInstance->assignVariable("image_width"   , $this->getWidth());
332                 $templateInstance->assignVariable("image_height"  , $this->getHeight());
333                 $templateInstance->assignVariable("image_bg_red"  , $this->backgroundColor['red']);
334                 $templateInstance->assignVariable("image_bg_green", $this->backgroundColor['green']);
335                 $templateInstance->assignVariable("image_bg_blue" , $this->backgroundColor['blue']);
336                 $templateInstance->assignVariable("image_fg_red"  , $this->foregroundColor['red']);
337                 $templateInstance->assignVariable("image_fg_green", $this->foregroundColor['green']);
338                 $templateInstance->assignVariable("image_fg_blue" , $this->foregroundColor['blue']);
339
340                 // Add all strings
341                 foreach ($this->imageStrings as $id=>$imageString) {
342                         // Set current string id
343                         $this->currString = $id;
344
345                         // Set variable group
346                         $templateInstance->setVariableGroup($id);
347
348                         // Add group variables
349                         $templateInstance->addGroupVariable("image_x"     , $this->getX());
350                         $templateInstance->addGroupVariable("image_y"     , $this->getY());
351                         $templateInstance->addGroupVariable("image_size"  , $this->getFontSize());
352                         $templateInstance->addGroupVariable("image_string", $this->getImageString());
353                 } // END - foreach
354
355                 // Get the raw content
356                 $imageContent = $templateInstance->getRawTemplateData();
357
358                 // Transfer all to the template engine
359                 $templateInstance->renderImageContent($imageContent);
360         }
361 }
362
363 // [EOF]
364 ?>