3 namespace CoreFramework\Helper;
5 // Import framework stuff
6 use CoreFramework\Helper\Template\HelpableTemplate;
7 use CoreFramework\Template\CompileableTemplate;
10 * A helper for creating images
12 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
31 class ImageHelper extends BaseCaptcha implements HelpableTemplate {
35 private $imageType = 'png';
40 private $imageName = '';
43 * Width of the image in pixel
48 * Height of the image in pixel
53 * Array for background color values
55 private $backgroundColor = array(
62 * Array for foreground color values
64 private $foregroundColor = array(
73 private $imageStrings = array();
78 private $currString = '';
83 private $baseImage = '';
86 * Protected constructor
90 protected function __construct () {
91 // Call parent constructor
92 parent::__construct(__CLASS__);
96 * Creates the helper class
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
102 public static final function createImageHelper (CompileableTemplate $templateInstance, $imageType) {
104 $helperInstance = new ImageHelper();
106 // Set template instance
107 $helperInstance->setTemplateInstance($templateInstance);
109 // Set image type (blindly)
110 $helperInstance->setImageType($imageType);
113 $helperInstance->initializeRandomNumberGenerator($templateInstance);
115 // Return the prepared instance
116 return $helperInstance;
120 * Setter for image type
122 * @param $imageType Type of the image
125 protected final function setImageType ($imageType) {
126 $this->imageType = (string) $imageType;
130 * Getter for image name
132 * @return $imageType Type of the image
134 public final function getImageType () {
135 return $this->imageType;
139 * Setter for base image
141 * @param $baseImage A base image template
144 public final function setBaseImage ($baseImage) {
145 $this->baseImage = (string) $baseImage;
149 * Getter for base image
151 * @return $baseImage A base image template
153 public final function getBaseImage () {
154 return $this->baseImage;
158 * Setter for image name
160 * @param $imageName Name of the image
163 public final function setImageName ($imageName) {
164 $this->imageName = (string) $imageName;
168 * Getter for image name
170 * @return $imageName Name of the image
172 protected final function getImageName () {
173 return $this->imageName;
177 * Setter for image width
179 * @param $width Width of the image
182 public final function setWidth ($width) {
183 $this->width = (int) $width;
187 * Getter for image width
189 * @return $width Width of the image
191 public final function getWidth () {
196 * Setter for image height
198 * @param $height Height of the image
201 public final function setHeight ($height) {
202 $this->height = (int) $height;
206 * Getter for image height
208 * @return $height Height of the image
210 public final function getHeight () {
211 return $this->height;
215 * Setter for RGB of background color
217 * @param $red Color value for red
218 * @param $green Color value for green
219 * @param $blue Color value for blue
222 public final function setBackgroundColorRedGreenBlue ($red, $green, $blue) {
224 if ($red === 'rand') {
225 $red = $this->getRngInstance()->randomNumber(0, 255);
227 if ($green === 'rand') {
228 $green = $this->getRngInstance()->randomNumber(0, 255);
230 if ($blue === 'rand') {
231 $blue = $this->getRngInstance()->randomNumber(0, 255);
234 $this->backgroundColor['red'] = (int) $red;
235 $this->backgroundColor['green'] = (int) $green;
236 $this->backgroundColor['blue'] = (int) $blue;
240 * Setter for RGB of foreground color
242 * @param $red Color value for red
243 * @param $green Color value for green
244 * @param $blue Color value for blue
247 public final function setForegroundColorRedGreenBlue ($red, $green, $blue) {
249 if ($red === 'rand') {
250 $red = $this->getRngInstance()->randomNumber(0, 255);
252 if ($green === 'rand') {
253 $green = $this->getRngInstance()->randomNumber(0, 255);
255 if ($blue === 'rand') {
256 $blue = $this->getRngInstance()->randomNumber(0, 255);
259 $this->foregroundColor['red'] = (int) $red;
260 $this->foregroundColor['green'] = (int) $green;
261 $this->foregroundColor['blue'] = (int) $blue;
265 * Adds an image string to the buffer by the given string name
267 * @param $stringName String name (identifier)
269 public function addTextLine ($stringName) {
270 // Create the image string
271 $this->imageStrings[$stringName] = array(
278 // Set current string name
279 $this->currString = $stringName;
283 * Setter for image message string
285 * @param $imageString A message to display in image
288 public final function setImageString ($imageString) {
289 $this->imageStrings[$this->currString]['string'] = (string) $imageString;
293 * Getter for image message string
295 * @return $imageString A message to display in image
297 public final function getImageString () {
298 return $this->imageStrings[$this->currString]['string'];
302 * Setter for X/Y coordinates for strings
304 * @param $x X coordinate
305 * @param $y Y coordinate
308 public final function setCoord ($x, $y) {
309 $this->imageStrings[$this->currString]['x'] = (int) $x;
310 $this->imageStrings[$this->currString]['y'] = (int) $y;
314 * Getter for X coordinate
316 * @return $x X coordinate
318 public final function getX () {
319 return $this->imageStrings[$this->currString]['x'];
323 * Getter for Y coordinate
325 * @return $y Y coordinate
327 public final function getY () {
328 return $this->imageStrings[$this->currString]['y'];
332 * Setter for font size
334 * @param $fontSize Font size for strings
337 public final function setFontSize ($fontSize) {
339 if ($fontSize === 'rand') {
340 $fontSize = $this->getRngInstance()->randomNumber(4, 9);
343 $this->imageStrings[$this->currString]['size'] = (int) $fontSize;
347 * Getter for font size
349 * @return $fontSize Font size for strings
351 public final function getFontSize () {
352 return $this->imageStrings[$this->currString]['size'];
356 * Flushs the content out
360 public function flushContent () {
361 // Get a template instance
362 $templateInstance = $this->getTemplateInstance();
364 // Get the base image
365 $templateInstance->loadImageTemplate($this->getBaseImage());
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']);
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;
384 // Set variable group
385 $templateInstance->setVariableGroup($id);
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());
394 // Get the raw content
395 $imageContent = $templateInstance->getRawTemplateData();
397 // Transfer all to the template engine
398 $templateInstance->renderXmlContent($imageContent);