3 namespace CoreFramework\Helper;
5 // Import framework stuff
6 use CoreFramework\Template\CompileableTemplate;
9 * A helper for creating images
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class ImageHelper extends BaseCaptcha implements HelpableTemplate {
34 private $imageType = 'png';
39 private $imageName = '';
42 * Width of the image in pixel
47 * Height of the image in pixel
52 * Array for background color values
54 private $backgroundColor = array(
61 * Array for foreground color values
63 private $foregroundColor = array(
72 private $imageStrings = array();
77 private $currString = '';
82 private $baseImage = '';
85 * Protected constructor
89 protected function __construct () {
90 // Call parent constructor
91 parent::__construct(__CLASS__);
95 * Creates the helper class
97 * @param $templateInstance An instance of a template engine
98 * @param $imageType Type of the image
99 * @return $helperInstance A preparedf instance of this helper
101 public static final function createImageHelper (CompileableTemplate $templateInstance, $imageType) {
103 $helperInstance = new ImageHelper();
105 // Set template instance
106 $helperInstance->setTemplateInstance($templateInstance);
108 // Set image type (blindly)
109 $helperInstance->setImageType($imageType);
112 $helperInstance->initializeRandomNumberGenerator($templateInstance);
114 // Return the prepared instance
115 return $helperInstance;
119 * Setter for image type
121 * @param $imageType Type of the image
124 protected final function setImageType ($imageType) {
125 $this->imageType = (string) $imageType;
129 * Getter for image name
131 * @return $imageType Type of the image
133 public final function getImageType () {
134 return $this->imageType;
138 * Setter for base image
140 * @param $baseImage A base image template
143 public final function setBaseImage ($baseImage) {
144 $this->baseImage = (string) $baseImage;
148 * Getter for base image
150 * @return $baseImage A base image template
152 public final function getBaseImage () {
153 return $this->baseImage;
157 * Setter for image name
159 * @param $imageName Name of the image
162 public final function setImageName ($imageName) {
163 $this->imageName = (string) $imageName;
167 * Getter for image name
169 * @return $imageName Name of the image
171 protected final function getImageName () {
172 return $this->imageName;
176 * Setter for image width
178 * @param $width Width of the image
181 public final function setWidth ($width) {
182 $this->width = (int) $width;
186 * Getter for image width
188 * @return $width Width of the image
190 public final function getWidth () {
195 * Setter for image height
197 * @param $height Height of the image
200 public final function setHeight ($height) {
201 $this->height = (int) $height;
205 * Getter for image height
207 * @return $height Height of the image
209 public final function getHeight () {
210 return $this->height;
214 * Setter for RGB of background color
216 * @param $red Color value for red
217 * @param $green Color value for green
218 * @param $blue Color value for blue
221 public final function setBackgroundColorRedGreenBlue ($red, $green, $blue) {
223 if ($red === 'rand') {
224 $red = $this->getRngInstance()->randomNumber(0, 255);
226 if ($green === 'rand') {
227 $green = $this->getRngInstance()->randomNumber(0, 255);
229 if ($blue === 'rand') {
230 $blue = $this->getRngInstance()->randomNumber(0, 255);
233 $this->backgroundColor['red'] = (int) $red;
234 $this->backgroundColor['green'] = (int) $green;
235 $this->backgroundColor['blue'] = (int) $blue;
239 * Setter for RGB of foreground color
241 * @param $red Color value for red
242 * @param $green Color value for green
243 * @param $blue Color value for blue
246 public final function setForegroundColorRedGreenBlue ($red, $green, $blue) {
248 if ($red === 'rand') {
249 $red = $this->getRngInstance()->randomNumber(0, 255);
251 if ($green === 'rand') {
252 $green = $this->getRngInstance()->randomNumber(0, 255);
254 if ($blue === 'rand') {
255 $blue = $this->getRngInstance()->randomNumber(0, 255);
258 $this->foregroundColor['red'] = (int) $red;
259 $this->foregroundColor['green'] = (int) $green;
260 $this->foregroundColor['blue'] = (int) $blue;
264 * Adds an image string to the buffer by the given string name
266 * @param $stringName String name (identifier)
268 public function addTextLine ($stringName) {
269 // Create the image string
270 $this->imageStrings[$stringName] = array(
277 // Set current string name
278 $this->currString = $stringName;
282 * Setter for image message string
284 * @param $imageString A message to display in image
287 public final function setImageString ($imageString) {
288 $this->imageStrings[$this->currString]['string'] = (string) $imageString;
292 * Getter for image message string
294 * @return $imageString A message to display in image
296 public final function getImageString () {
297 return $this->imageStrings[$this->currString]['string'];
301 * Setter for X/Y coordinates for strings
303 * @param $x X coordinate
304 * @param $y Y coordinate
307 public final function setCoord ($x, $y) {
308 $this->imageStrings[$this->currString]['x'] = (int) $x;
309 $this->imageStrings[$this->currString]['y'] = (int) $y;
313 * Getter for X coordinate
315 * @return $x X coordinate
317 public final function getX () {
318 return $this->imageStrings[$this->currString]['x'];
322 * Getter for Y coordinate
324 * @return $y Y coordinate
326 public final function getY () {
327 return $this->imageStrings[$this->currString]['y'];
331 * Setter for font size
333 * @param $fontSize Font size for strings
336 public final function setFontSize ($fontSize) {
338 if ($fontSize === 'rand') {
339 $fontSize = $this->getRngInstance()->randomNumber(4, 9);
342 $this->imageStrings[$this->currString]['size'] = (int) $fontSize;
346 * Getter for font size
348 * @return $fontSize Font size for strings
350 public final function getFontSize () {
351 return $this->imageStrings[$this->currString]['size'];
355 * Flushs the content out
359 public function flushContent () {
360 // Get a template instance
361 $templateInstance = $this->getTemplateInstance();
363 // Get the base image
364 $templateInstance->loadImageTemplate($this->getBaseImage());
366 // Assign all the image values with the template
367 $templateInstance->assignVariable('image_name' , $this->getImageName());
368 $templateInstance->assignVariable('image_type' , $this->getImageType());
369 $templateInstance->assignVariable('image_width' , $this->getWidth());
370 $templateInstance->assignVariable('image_height' , $this->getHeight());
371 $templateInstance->assignVariable('image_bg_red' , $this->backgroundColor['red']);
372 $templateInstance->assignVariable('image_bg_green', $this->backgroundColor['green']);
373 $templateInstance->assignVariable('image_bg_blue' , $this->backgroundColor['blue']);
374 $templateInstance->assignVariable('image_fg_red' , $this->foregroundColor['red']);
375 $templateInstance->assignVariable('image_fg_green', $this->foregroundColor['green']);
376 $templateInstance->assignVariable('image_fg_blue' , $this->foregroundColor['blue']);
379 foreach ($this->imageStrings as $id => $imageString) {
380 // Set current string id to keep this helper in sync with template engine
381 $this->currString = $id;
383 // Set variable group
384 $templateInstance->setVariableGroup($id);
386 // Add group variables
387 $templateInstance->addGroupVariable('image_x' , $this->getX());
388 $templateInstance->addGroupVariable('image_y' , $this->getY());
389 $templateInstance->addGroupVariable('image_size' , $this->getFontSize());
390 $templateInstance->addGroupVariable('image_string', $this->getImageString());
393 // Get the raw content
394 $imageContent = $templateInstance->getRawTemplateData();
396 // Transfer all to the template engine
397 $templateInstance->renderXmlContent($imageContent);