3 * A helper for creating images
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.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 = "";
74 * Protected constructor
78 protected function __construct () {
79 // Call parent constructor
80 parent::__construct(__CLASS__);
82 // Set part description
83 $this->setObjectDescription("A image helper class");
87 * Creates the helper class
89 * @param $templateInstance An instance of a template engine
90 * @param $imageType Type of the image
91 * @return $helperInstance A preparedf instance of this class
93 public final static function createImageHelper (CompileableTemplate $templateInstance, $imageType) {
95 $helperInstance = new ImageHelper();
97 // Set template instance
98 $helperInstance->setTemplateInstance($templateInstance);
100 // Set image type (blindly)
101 $helperInstance->setImageType($imageType);
104 $helperInstance->initializeRandomNumberGenerator($templateInstance);
106 // Return the prepared instance
107 return $helperInstance;
111 * Setter for image type
113 * @param $imageType Type of the image
116 protected final function setImageType ($imageType) {
117 $this->imageType = (string) $imageType;
121 * Getter for image name
123 * @return $imageType Type of the image
125 public final function getImageType () {
126 return $this->imageType;
130 * Setter for base image
132 * @param $baseImage A base image template
135 public final function setBaseImage ($baseImage) {
136 $this->baseImage = (string) $baseImage;
140 * Getter for base image
142 * @return $baseImage A base image template
144 public final function getBaseImage () {
145 return $this->baseImage;
149 * Setter for image name
151 * @param $imageName Name of the image
154 public final function setImageName ($imageName) {
155 $this->imageName = (string) $imageName;
159 * Getter for image name
161 * @return $imageName Name of the image
163 protected final function getImageName () {
164 return $this->imageName;
168 * Setter for image width
170 * @param $width Width of the image
173 public final function setWidth ($width) {
174 $this->width = (int) $width;
178 * Getter for image width
180 * @return $width Width of the image
182 public final function getWidth () {
187 * Setter for image height
189 * @param $height Height of the image
192 public final function setHeight ($height) {
193 $this->height = (int) $height;
197 * Getter for image height
199 * @return $height Height of the image
201 public final function getHeight () {
202 return $this->height;
206 * Setter for RGB of background color
208 * @param $red Color value for red
209 * @param $green Color value for green
210 * @param $blue Color value for blue
213 public final function setBackgroundColorRedGreenBlue ($red, $green, $blue) {
215 if ($red === "rand") {
216 $red = $this->getRngInstance()->randomNumber(0, 255);
218 if ($green === "rand") {
219 $green = $this->getRngInstance()->randomNumber(0, 255);
221 if ($blue === "rand") {
222 $blue = $this->getRngInstance()->randomNumber(0, 255);
225 $this->backgroundColor['red'] = (int) $red;
226 $this->backgroundColor['green'] = (int) $green;
227 $this->backgroundColor['blue'] = (int) $blue;
231 * Setter for RGB of foreground color
233 * @param $red Color value for red
234 * @param $green Color value for green
235 * @param $blue Color value for blue
238 public final function setForegroundColorRedGreenBlue ($red, $green, $blue) {
240 if ($red === "rand") {
241 $red = $this->getRngInstance()->randomNumber(0, 255);
243 if ($green === "rand") {
244 $green = $this->getRngInstance()->randomNumber(0, 255);
246 if ($blue === "rand") {
247 $blue = $this->getRngInstance()->randomNumber(0, 255);
250 $this->foregroundColor['red'] = (int) $red;
251 $this->foregroundColor['green'] = (int) $green;
252 $this->foregroundColor['blue'] = (int) $blue;
256 * Adds an image string to the buffer by the given string name
258 * @param $stringName String name (identifier)
260 public function addTextLine ($stringName) {
261 // Create the image string
262 $this->imageStrings[$stringName] = array(
269 // Set current string name
270 $this->currString = $stringName;
274 * Setter for image message string
276 * @param $imageString A message to display in the image
279 public final function setImageString ($imageString) {
280 $this->imageStrings[$this->currString]['string'] = (string) $imageString;
284 * Getter for image message string
286 * @return $imageString A message to display in the image
288 public final function getImageString () {
289 return $this->imageStrings[$this->currString]['string'];
293 * Setter for X/Y coordinates for strings
295 * @param $x X coordinate
296 * @param $y Y coordinate
299 public final function setCoord ($x, $y) {
300 $this->imageStrings[$this->currString]['x'] = (int) $x;
301 $this->imageStrings[$this->currString]['y'] = (int) $y;
305 * Getter for X coordinate
307 * @return $x X coordinate
309 public final function getX () {
310 return $this->imageStrings[$this->currString]['x'];
314 * Getter for Y coordinate
316 * @return $y Y coordinate
318 public final function getY () {
319 return $this->imageStrings[$this->currString]['y'];
323 * Setter for font size
325 * @param $fontSize Font size for strings
328 public final function setFontSize ($fontSize) {
330 if ($fontSize === "rand") {
331 $fontSize = $this->getRngInstance()->randomNumber(3, 9);
334 $this->imageStrings[$this->currString]['size'] = (int) $fontSize;
338 * Getter for font size
340 * @return $fontSize Font size for strings
342 public final function getFontSize () {
343 return $this->imageStrings[$this->currString]['size'];
347 * Flushs the content out
351 public function flushContent () {
352 // Get a template instance
353 $templateInstance = $this->getTemplateInstance();
355 // Get the base image
356 $templateInstance->loadCodeTemplate($this->getBaseImage());
358 // Assign all the image values with the template
359 $templateInstance->assignVariable("image_name" , $this->getImageName());
360 $templateInstance->assignVariable("image_type" , $this->getImageType());
361 $templateInstance->assignVariable("image_width" , $this->getWidth());
362 $templateInstance->assignVariable("image_height" , $this->getHeight());
363 $templateInstance->assignVariable("image_bg_red" , $this->backgroundColor['red']);
364 $templateInstance->assignVariable("image_bg_green", $this->backgroundColor['green']);
365 $templateInstance->assignVariable("image_bg_blue" , $this->backgroundColor['blue']);
366 $templateInstance->assignVariable("image_fg_red" , $this->foregroundColor['red']);
367 $templateInstance->assignVariable("image_fg_green", $this->foregroundColor['green']);
368 $templateInstance->assignVariable("image_fg_blue" , $this->foregroundColor['blue']);
371 foreach ($this->imageStrings as $id=>$imageString) {
372 // Set current string id
373 $this->currString = $id;
375 // Set variable group
376 $templateInstance->setVariableGroup($id);
378 // Add group variables
379 $templateInstance->addGroupVariable("image_x" , $this->getX());
380 $templateInstance->addGroupVariable("image_y" , $this->getY());
381 $templateInstance->addGroupVariable("image_size" , $this->getFontSize());
382 $templateInstance->addGroupVariable("image_string", $this->getImageString());
385 // Get the raw content
386 $imageContent = $templateInstance->getRawTemplateData();
388 // Transfer all to the template engine
389 $templateInstance->renderImageContent($imageContent);