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