3 namespace Org\Mxchange\CoreFramework\Helper\Image;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Helper\Captcha\BaseCaptcha;
7 use Org\Mxchange\CoreFramework\Helper\Template\HelpableTemplate;
8 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
11 * A helper for creating images
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 <<<<<<< HEAD:framework/main/classes/helper/captcha/images/class_ImageHelper.php
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
19 >>>>>>> Some updates::inc/main/classes/helper/captcha/images/class_ImageHelper.php
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class ImageHelper extends BaseCaptcha implements HelpableTemplate {
40 private $imageType = 'png';
45 private $imageName = '';
48 * Width of the image in pixel
53 * Height of the image in pixel
58 * Array for background color values
60 private $backgroundColor = array(
67 * Array for foreground color values
69 private $foregroundColor = array(
78 private $imageStrings = array();
83 private $currString = '';
88 private $baseImage = '';
91 * Protected constructor
95 protected function __construct () {
96 // Call parent constructor
97 parent::__construct(__CLASS__);
101 * Creates the helper class
103 * @param $templateInstance An instance of a template engine
104 * @param $imageType Type of the image
105 * @return $helperInstance A preparedf instance of this helper
107 public static final function createImageHelper (CompileableTemplate $templateInstance, $imageType) {
109 $helperInstance = new ImageHelper();
111 // Set template instance
112 $helperInstance->setTemplateInstance($templateInstance);
114 // Set image type (blindly)
115 $helperInstance->setImageType($imageType);
118 $helperInstance->initializeRandomNumberGenerator($templateInstance);
120 // Return the prepared instance
121 return $helperInstance;
125 * Setter for image type
127 * @param $imageType Type of the image
130 protected final function setImageType ($imageType) {
131 $this->imageType = (string) $imageType;
135 * Getter for image name
137 * @return $imageType Type of the image
139 public final function getImageType () {
140 return $this->imageType;
144 * Setter for base image
146 * @param $baseImage A base image template
149 public final function setBaseImage ($baseImage) {
150 $this->baseImage = (string) $baseImage;
154 * Getter for base image
156 * @return $baseImage A base image template
158 public final function getBaseImage () {
159 return $this->baseImage;
163 * Setter for image name
165 * @param $imageName Name of the image
168 public final function setImageName ($imageName) {
169 $this->imageName = (string) $imageName;
173 * Getter for image name
175 * @return $imageName Name of the image
177 protected final function getImageName () {
178 return $this->imageName;
182 * Setter for image width
184 * @param $width Width of the image
187 public final function setWidth ($width) {
188 $this->width = (int) $width;
192 * Getter for image width
194 * @return $width Width of the image
196 public final function getWidth () {
201 * Setter for image height
203 * @param $height Height of the image
206 public final function setHeight ($height) {
207 $this->height = (int) $height;
211 * Getter for image height
213 * @return $height Height of the image
215 public final function getHeight () {
216 return $this->height;
220 * Setter for RGB of background color
222 * @param $red Color value for red
223 * @param $green Color value for green
224 * @param $blue Color value for blue
227 public final function setBackgroundColorRedGreenBlue ($red, $green, $blue) {
229 if ($red === 'rand') {
230 $red = $this->getRngInstance()->randomNumber(0, 255);
232 if ($green === 'rand') {
233 $green = $this->getRngInstance()->randomNumber(0, 255);
235 if ($blue === 'rand') {
236 $blue = $this->getRngInstance()->randomNumber(0, 255);
239 $this->backgroundColor['red'] = (int) $red;
240 $this->backgroundColor['green'] = (int) $green;
241 $this->backgroundColor['blue'] = (int) $blue;
245 * Setter for RGB of foreground color
247 * @param $red Color value for red
248 * @param $green Color value for green
249 * @param $blue Color value for blue
252 public final function setForegroundColorRedGreenBlue ($red, $green, $blue) {
254 if ($red === 'rand') {
255 $red = $this->getRngInstance()->randomNumber(0, 255);
257 if ($green === 'rand') {
258 $green = $this->getRngInstance()->randomNumber(0, 255);
260 if ($blue === 'rand') {
261 $blue = $this->getRngInstance()->randomNumber(0, 255);
264 $this->foregroundColor['red'] = (int) $red;
265 $this->foregroundColor['green'] = (int) $green;
266 $this->foregroundColor['blue'] = (int) $blue;
270 * Adds an image string to the buffer by the given string name
272 * @param $stringName String name (identifier)
274 public function addTextLine ($stringName) {
275 // Create the image string
276 $this->imageStrings[$stringName] = array(
283 // Set current string name
284 $this->currString = $stringName;
288 * Setter for image message string
290 * @param $imageString A message to display in image
293 public final function setImageString ($imageString) {
294 $this->imageStrings[$this->currString]['string'] = (string) $imageString;
298 * Getter for image message string
300 * @return $imageString A message to display in image
302 public final function getImageString () {
303 return $this->imageStrings[$this->currString]['string'];
307 * Setter for X/Y coordinates for strings
309 * @param $x X coordinate
310 * @param $y Y coordinate
313 public final function setCoord ($x, $y) {
314 $this->imageStrings[$this->currString]['x'] = (int) $x;
315 $this->imageStrings[$this->currString]['y'] = (int) $y;
319 * Getter for X coordinate
321 * @return $x X coordinate
323 public final function getX () {
324 return $this->imageStrings[$this->currString]['x'];
328 * Getter for Y coordinate
330 * @return $y Y coordinate
332 public final function getY () {
333 return $this->imageStrings[$this->currString]['y'];
337 * Setter for font size
339 * @param $fontSize Font size for strings
342 public final function setFontSize ($fontSize) {
344 if ($fontSize === 'rand') {
345 $fontSize = $this->getRngInstance()->randomNumber(4, 9);
348 $this->imageStrings[$this->currString]['size'] = (int) $fontSize;
352 * Getter for font size
354 * @return $fontSize Font size for strings
356 public final function getFontSize () {
357 return $this->imageStrings[$this->currString]['size'];
361 * Flushs the content out
365 public function flushContent () {
366 // Get a template instance
367 $templateInstance = $this->getTemplateInstance();
369 // Get the base image
370 $templateInstance->loadImageTemplate($this->getBaseImage());
372 // Assign all the image values with the template
373 $templateInstance->assignVariable('image_name' , $this->getImageName());
374 $templateInstance->assignVariable('image_type' , $this->getImageType());
375 $templateInstance->assignVariable('image_width' , $this->getWidth());
376 $templateInstance->assignVariable('image_height' , $this->getHeight());
377 $templateInstance->assignVariable('image_bg_red' , $this->backgroundColor['red']);
378 $templateInstance->assignVariable('image_bg_green', $this->backgroundColor['green']);
379 $templateInstance->assignVariable('image_bg_blue' , $this->backgroundColor['blue']);
380 $templateInstance->assignVariable('image_fg_red' , $this->foregroundColor['red']);
381 $templateInstance->assignVariable('image_fg_green', $this->foregroundColor['green']);
382 $templateInstance->assignVariable('image_fg_blue' , $this->foregroundColor['blue']);
385 foreach ($this->imageStrings as $id => $imageString) {
386 // Set current string id to keep this helper in sync with template engine
387 $this->currString = $id;
389 // Set variable group
390 $templateInstance->setVariableGroup($id);
392 // Add group variables
393 $templateInstance->addGroupVariable('image_x' , $this->getX());
394 $templateInstance->addGroupVariable('image_y' , $this->getY());
395 $templateInstance->addGroupVariable('image_size' , $this->getFontSize());
396 $templateInstance->addGroupVariable('image_string', $this->getImageString());
399 // Get the raw content
400 $imageContent = $templateInstance->getRawTemplateData();
402 // Transfer all to the template engine
403 $templateInstance->renderXmlContent($imageContent);