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 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class ImageHelper extends BaseCaptcha implements HelpableTemplate {
36 private $imageType = 'png';
41 private $imageName = '';
44 * Width of the image in pixel
49 * Height of the image in pixel
54 * Array for background color values
56 private $backgroundColor = array(
63 * Array for foreground color values
65 private $foregroundColor = array(
74 private $imageStrings = array();
79 private $currString = '';
84 private $baseImage = '';
87 * Protected constructor
91 protected function __construct () {
92 // Call parent constructor
93 parent::__construct(__CLASS__);
97 * Creates the helper class
99 * @param $templateInstance An instance of a template engine
100 * @param $imageType Type of the image
101 * @return $helperInstance A preparedf instance of this helper
103 public static final function createImageHelper (CompileableTemplate $templateInstance, $imageType) {
105 $helperInstance = new ImageHelper();
107 // Set template instance
108 $helperInstance->setTemplateInstance($templateInstance);
110 // Set image type (blindly)
111 $helperInstance->setImageType($imageType);
114 $helperInstance->initializeRandomNumberGenerator($templateInstance);
116 // Return the prepared instance
117 return $helperInstance;
121 * Setter for image type
123 * @param $imageType Type of the image
126 protected final function setImageType ($imageType) {
127 $this->imageType = (string) $imageType;
131 * Getter for image name
133 * @return $imageType Type of the image
135 public final function getImageType () {
136 return $this->imageType;
140 * Setter for base image
142 * @param $baseImage A base image template
145 public final function setBaseImage ($baseImage) {
146 $this->baseImage = (string) $baseImage;
150 * Getter for base image
152 * @return $baseImage A base image template
154 public final function getBaseImage () {
155 return $this->baseImage;
159 * Setter for image name
161 * @param $imageName Name of the image
164 public final function setImageName ($imageName) {
165 $this->imageName = (string) $imageName;
169 * Getter for image name
171 * @return $imageName Name of the image
173 protected final function getImageName () {
174 return $this->imageName;
178 * Setter for image width
180 * @param $width Width of the image
183 public final function setWidth ($width) {
184 $this->width = (int) $width;
188 * Getter for image width
190 * @return $width Width of the image
192 public final function getWidth () {
197 * Setter for image height
199 * @param $height Height of the image
202 public final function setHeight ($height) {
203 $this->height = (int) $height;
207 * Getter for image height
209 * @return $height Height of the image
211 public final function getHeight () {
212 return $this->height;
216 * Setter for RGB of background color
218 * @param $red Color value for red
219 * @param $green Color value for green
220 * @param $blue Color value for blue
223 public final function setBackgroundColorRedGreenBlue ($red, $green, $blue) {
225 if ($red === 'rand') {
226 $red = $this->getRngInstance()->randomNumber(0, 255);
228 if ($green === 'rand') {
229 $green = $this->getRngInstance()->randomNumber(0, 255);
231 if ($blue === 'rand') {
232 $blue = $this->getRngInstance()->randomNumber(0, 255);
235 $this->backgroundColor['red'] = (int) $red;
236 $this->backgroundColor['green'] = (int) $green;
237 $this->backgroundColor['blue'] = (int) $blue;
241 * Setter for RGB of foreground color
243 * @param $red Color value for red
244 * @param $green Color value for green
245 * @param $blue Color value for blue
248 public final function setForegroundColorRedGreenBlue ($red, $green, $blue) {
250 if ($red === 'rand') {
251 $red = $this->getRngInstance()->randomNumber(0, 255);
253 if ($green === 'rand') {
254 $green = $this->getRngInstance()->randomNumber(0, 255);
256 if ($blue === 'rand') {
257 $blue = $this->getRngInstance()->randomNumber(0, 255);
260 $this->foregroundColor['red'] = (int) $red;
261 $this->foregroundColor['green'] = (int) $green;
262 $this->foregroundColor['blue'] = (int) $blue;
266 * Adds an image string to the buffer by the given string name
268 * @param $stringName String name (identifier)
270 public function addTextLine ($stringName) {
271 // Create the image string
272 $this->imageStrings[$stringName] = array(
279 // Set current string name
280 $this->currString = $stringName;
284 * Setter for image message string
286 * @param $imageString A message to display in image
289 public final function setImageString ($imageString) {
290 $this->imageStrings[$this->currString]['string'] = (string) $imageString;
294 * Getter for image message string
296 * @return $imageString A message to display in image
298 public final function getImageString () {
299 return $this->imageStrings[$this->currString]['string'];
303 * Setter for X/Y coordinates for strings
305 * @param $x X coordinate
306 * @param $y Y coordinate
309 public final function setCoord ($x, $y) {
310 $this->imageStrings[$this->currString]['x'] = (int) $x;
311 $this->imageStrings[$this->currString]['y'] = (int) $y;
315 * Getter for X coordinate
317 * @return $x X coordinate
319 public final function getX () {
320 return $this->imageStrings[$this->currString]['x'];
324 * Getter for Y coordinate
326 * @return $y Y coordinate
328 public final function getY () {
329 return $this->imageStrings[$this->currString]['y'];
333 * Setter for font size
335 * @param $fontSize Font size for strings
338 public final function setFontSize ($fontSize) {
340 if ($fontSize === 'rand') {
341 $fontSize = $this->getRngInstance()->randomNumber(4, 9);
344 $this->imageStrings[$this->currString]['size'] = (int) $fontSize;
348 * Getter for font size
350 * @return $fontSize Font size for strings
352 public final function getFontSize () {
353 return $this->imageStrings[$this->currString]['size'];
357 * Flushs the content out
361 public function flushContent () {
362 // Get a template instance
363 $templateInstance = $this->getTemplateInstance();
365 // Get the base image
366 $templateInstance->loadImageTemplate($this->getBaseImage());
368 // Assign all the image values with the template
369 $templateInstance->assignVariable('image_name' , $this->getImageName());
370 $templateInstance->assignVariable('image_type' , $this->getImageType());
371 $templateInstance->assignVariable('image_width' , $this->getWidth());
372 $templateInstance->assignVariable('image_height' , $this->getHeight());
373 $templateInstance->assignVariable('image_bg_red' , $this->backgroundColor['red']);
374 $templateInstance->assignVariable('image_bg_green', $this->backgroundColor['green']);
375 $templateInstance->assignVariable('image_bg_blue' , $this->backgroundColor['blue']);
376 $templateInstance->assignVariable('image_fg_red' , $this->foregroundColor['red']);
377 $templateInstance->assignVariable('image_fg_green', $this->foregroundColor['green']);
378 $templateInstance->assignVariable('image_fg_blue' , $this->foregroundColor['blue']);
381 foreach ($this->imageStrings as $id => $imageString) {
382 // Set current string id to keep this helper in sync with template engine
383 $this->currString = $id;
385 // Set variable group
386 $templateInstance->setVariableGroup($id);
388 // Add group variables
389 $templateInstance->addGroupVariable('image_x' , $this->getX());
390 $templateInstance->addGroupVariable('image_y' , $this->getY());
391 $templateInstance->addGroupVariable('image_size' , $this->getFontSize());
392 $templateInstance->addGroupVariable('image_string', $this->getImageString());
395 // Get the raw content
396 $imageContent = $templateInstance->getRawTemplateData();
398 // Transfer all to the template engine
399 $templateInstance->renderXmlContent($imageContent);