3 * A helper for creating images
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
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.
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.
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/>.
24 class ImageHelper extends BaseCaptcha implements HelpableTemplate {
28 private $imageType = 'png';
33 private $imageName = '';
36 * Width of the image in pixel
41 * Height of the image in pixel
46 * Array for background color values
48 private $backgroundColor = array(
55 * Array for foreground color values
57 private $foregroundColor = array(
66 private $imageStrings = array();
71 private $currString = '';
76 private $baseImage = '';
79 * Protected constructor
83 protected function __construct () {
84 // Call parent constructor
85 parent::__construct(__CLASS__);
89 * Creates the helper class
91 * @param $templateInstance An instance of a template engine
92 * @param $imageType Type of the image
93 * @return $helperInstance A preparedf instance of this helper
95 public static final function createImageHelper (CompileableTemplate $templateInstance, $imageType) {
97 $helperInstance = new ImageHelper();
99 // Set template instance
100 $helperInstance->setTemplateInstance($templateInstance);
102 // Set image type (blindly)
103 $helperInstance->setImageType($imageType);
106 $helperInstance->initializeRandomNumberGenerator($templateInstance);
108 // Return the prepared instance
109 return $helperInstance;
113 * Setter for image type
115 * @param $imageType Type of the image
118 protected final function setImageType ($imageType) {
119 $this->imageType = (string) $imageType;
123 * Getter for image name
125 * @return $imageType Type of the image
127 public final function getImageType () {
128 return $this->imageType;
132 * Setter for base image
134 * @param $baseImage A base image template
137 public final function setBaseImage ($baseImage) {
138 $this->baseImage = (string) $baseImage;
142 * Getter for base image
144 * @return $baseImage A base image template
146 public final function getBaseImage () {
147 return $this->baseImage;
151 * Setter for image name
153 * @param $imageName Name of the image
156 public final function setImageName ($imageName) {
157 $this->imageName = (string) $imageName;
161 * Getter for image name
163 * @return $imageName Name of the image
165 protected final function getImageName () {
166 return $this->imageName;
170 * Setter for image width
172 * @param $width Width of the image
175 public final function setWidth ($width) {
176 $this->width = (int) $width;
180 * Getter for image width
182 * @return $width Width of the image
184 public final function getWidth () {
189 * Setter for image height
191 * @param $height Height of the image
194 public final function setHeight ($height) {
195 $this->height = (int) $height;
199 * Getter for image height
201 * @return $height Height of the image
203 public final function getHeight () {
204 return $this->height;
208 * Setter for RGB of background color
210 * @param $red Color value for red
211 * @param $green Color value for green
212 * @param $blue Color value for blue
215 public final function setBackgroundColorRedGreenBlue ($red, $green, $blue) {
217 if ($red === 'rand') {
218 $red = $this->getRngInstance()->randomNumber(0, 255);
220 if ($green === 'rand') {
221 $green = $this->getRngInstance()->randomNumber(0, 255);
223 if ($blue === 'rand') {
224 $blue = $this->getRngInstance()->randomNumber(0, 255);
227 $this->backgroundColor['red'] = (int) $red;
228 $this->backgroundColor['green'] = (int) $green;
229 $this->backgroundColor['blue'] = (int) $blue;
233 * Setter for RGB of foreground color
235 * @param $red Color value for red
236 * @param $green Color value for green
237 * @param $blue Color value for blue
240 public final function setForegroundColorRedGreenBlue ($red, $green, $blue) {
242 if ($red === 'rand') {
243 $red = $this->getRngInstance()->randomNumber(0, 255);
245 if ($green === 'rand') {
246 $green = $this->getRngInstance()->randomNumber(0, 255);
248 if ($blue === 'rand') {
249 $blue = $this->getRngInstance()->randomNumber(0, 255);
252 $this->foregroundColor['red'] = (int) $red;
253 $this->foregroundColor['green'] = (int) $green;
254 $this->foregroundColor['blue'] = (int) $blue;
258 * Adds an image string to the buffer by the given string name
260 * @param $stringName String name (identifier)
262 public function addTextLine ($stringName) {
263 // Create the image string
264 $this->imageStrings[$stringName] = array(
271 // Set current string name
272 $this->currString = $stringName;
276 * Setter for image message string
278 * @param $imageString A message to display in image
281 public final function setImageString ($imageString) {
282 $this->imageStrings[$this->currString]['string'] = (string) $imageString;
286 * Getter for image message string
288 * @return $imageString A message to display in image
290 public final function getImageString () {
291 return $this->imageStrings[$this->currString]['string'];
295 * Setter for X/Y coordinates for strings
297 * @param $x X coordinate
298 * @param $y Y coordinate
301 public final function setCoord ($x, $y) {
302 $this->imageStrings[$this->currString]['x'] = (int) $x;
303 $this->imageStrings[$this->currString]['y'] = (int) $y;
307 * Getter for X coordinate
309 * @return $x X coordinate
311 public final function getX () {
312 return $this->imageStrings[$this->currString]['x'];
316 * Getter for Y coordinate
318 * @return $y Y coordinate
320 public final function getY () {
321 return $this->imageStrings[$this->currString]['y'];
325 * Setter for font size
327 * @param $fontSize Font size for strings
330 public final function setFontSize ($fontSize) {
332 if ($fontSize === 'rand') {
333 $fontSize = $this->getRngInstance()->randomNumber(4, 9);
336 $this->imageStrings[$this->currString]['size'] = (int) $fontSize;
340 * Getter for font size
342 * @return $fontSize Font size for strings
344 public final function getFontSize () {
345 return $this->imageStrings[$this->currString]['size'];
349 * Flushs the content out
353 public function flushContent () {
354 // Get a template instance
355 $templateInstance = $this->getTemplateInstance();
357 // Get the base image
358 $templateInstance->loadImageTemplate($this->getBaseImage());
360 // Assign all the image values with the template
361 $templateInstance->assignVariable('image_name' , $this->getImageName());
362 $templateInstance->assignVariable('image_type' , $this->getImageType());
363 $templateInstance->assignVariable('image_width' , $this->getWidth());
364 $templateInstance->assignVariable('image_height' , $this->getHeight());
365 $templateInstance->assignVariable('image_bg_red' , $this->backgroundColor['red']);
366 $templateInstance->assignVariable('image_bg_green', $this->backgroundColor['green']);
367 $templateInstance->assignVariable('image_bg_blue' , $this->backgroundColor['blue']);
368 $templateInstance->assignVariable('image_fg_red' , $this->foregroundColor['red']);
369 $templateInstance->assignVariable('image_fg_green', $this->foregroundColor['green']);
370 $templateInstance->assignVariable('image_fg_blue' , $this->foregroundColor['blue']);
373 foreach ($this->imageStrings as $id => $imageString) {
374 // Set current string id to keep this helper in sync with template engine
375 $this->currString = $id;
377 // Set variable group
378 $templateInstance->setVariableGroup($id);
380 // Add group variables
381 $templateInstance->addGroupVariable('image_x' , $this->getX());
382 $templateInstance->addGroupVariable('image_y' , $this->getY());
383 $templateInstance->addGroupVariable('image_size' , $this->getFontSize());
384 $templateInstance->addGroupVariable('image_string', $this->getImageString());
387 // Get the raw content
388 $imageContent = $templateInstance->getRawTemplateData();
390 // Transfer all to the template engine
391 $templateInstance->renderXmlContent($imageContent);