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__);
84 * Creates the helper class
86 * @param $templateInstance An instance of a template engine
87 * @param $imageType Type of the image
88 * @return $helperInstance A preparedf instance of this helper
90 public final static function createImageHelper (CompileableTemplate $templateInstance, $imageType) {
92 $helperInstance = new ImageHelper();
94 // Set template instance
95 $helperInstance->setTemplateInstance($templateInstance);
97 // Set image type (blindly)
98 $helperInstance->setImageType($imageType);
101 $helperInstance->initializeRandomNumberGenerator($templateInstance);
103 // Return the prepared instance
104 return $helperInstance;
108 * Setter for image type
110 * @param $imageType Type of the image
113 protected final function setImageType ($imageType) {
114 $this->imageType = (string) $imageType;
118 * Getter for image name
120 * @return $imageType Type of the image
122 public final function getImageType () {
123 return $this->imageType;
127 * Setter for base image
129 * @param $baseImage A base image template
132 public final function setBaseImage ($baseImage) {
133 $this->baseImage = (string) $baseImage;
137 * Getter for base image
139 * @return $baseImage A base image template
141 public final function getBaseImage () {
142 return $this->baseImage;
146 * Setter for image name
148 * @param $imageName Name of the image
151 public final function setImageName ($imageName) {
152 $this->imageName = (string) $imageName;
156 * Getter for image name
158 * @return $imageName Name of the image
160 protected final function getImageName () {
161 return $this->imageName;
165 * Setter for image width
167 * @param $width Width of the image
170 public final function setWidth ($width) {
171 $this->width = (int) $width;
175 * Getter for image width
177 * @return $width Width of the image
179 public final function getWidth () {
184 * Setter for image height
186 * @param $height Height of the image
189 public final function setHeight ($height) {
190 $this->height = (int) $height;
194 * Getter for image height
196 * @return $height Height of the image
198 public final function getHeight () {
199 return $this->height;
203 * Setter for RGB of background color
205 * @param $red Color value for red
206 * @param $green Color value for green
207 * @param $blue Color value for blue
210 public final function setBackgroundColorRedGreenBlue ($red, $green, $blue) {
212 if ($red === "rand") {
213 $red = $this->getRngInstance()->randomNumber(0, 255);
215 if ($green === "rand") {
216 $green = $this->getRngInstance()->randomNumber(0, 255);
218 if ($blue === "rand") {
219 $blue = $this->getRngInstance()->randomNumber(0, 255);
222 $this->backgroundColor['red'] = (int) $red;
223 $this->backgroundColor['green'] = (int) $green;
224 $this->backgroundColor['blue'] = (int) $blue;
228 * Setter for RGB of foreground color
230 * @param $red Color value for red
231 * @param $green Color value for green
232 * @param $blue Color value for blue
235 public final function setForegroundColorRedGreenBlue ($red, $green, $blue) {
237 if ($red === "rand") {
238 $red = $this->getRngInstance()->randomNumber(0, 255);
240 if ($green === "rand") {
241 $green = $this->getRngInstance()->randomNumber(0, 255);
243 if ($blue === "rand") {
244 $blue = $this->getRngInstance()->randomNumber(0, 255);
247 $this->foregroundColor['red'] = (int) $red;
248 $this->foregroundColor['green'] = (int) $green;
249 $this->foregroundColor['blue'] = (int) $blue;
253 * Adds an image string to the buffer by the given string name
255 * @param $stringName String name (identifier)
257 public function addTextLine ($stringName) {
258 // Create the image string
259 $this->imageStrings[$stringName] = array(
266 // Set current string name
267 $this->currString = $stringName;
271 * Setter for image message string
273 * @param $imageString A message to display in the image
276 public final function setImageString ($imageString) {
277 $this->imageStrings[$this->currString]['string'] = (string) $imageString;
281 * Getter for image message string
283 * @return $imageString A message to display in the image
285 public final function getImageString () {
286 return $this->imageStrings[$this->currString]['string'];
290 * Setter for X/Y coordinates for strings
292 * @param $x X coordinate
293 * @param $y Y coordinate
296 public final function setCoord ($x, $y) {
297 $this->imageStrings[$this->currString]['x'] = (int) $x;
298 $this->imageStrings[$this->currString]['y'] = (int) $y;
302 * Getter for X coordinate
304 * @return $x X coordinate
306 public final function getX () {
307 return $this->imageStrings[$this->currString]['x'];
311 * Getter for Y coordinate
313 * @return $y Y coordinate
315 public final function getY () {
316 return $this->imageStrings[$this->currString]['y'];
320 * Setter for font size
322 * @param $fontSize Font size for strings
325 public final function setFontSize ($fontSize) {
327 if ($fontSize === "rand") {
328 $fontSize = $this->getRngInstance()->randomNumber(4, 9);
331 $this->imageStrings[$this->currString]['size'] = (int) $fontSize;
335 * Getter for font size
337 * @return $fontSize Font size for strings
339 public final function getFontSize () {
340 return $this->imageStrings[$this->currString]['size'];
344 * Flushs the content out
348 public function flushContent () {
349 // Get a template instance
350 $templateInstance = $this->getTemplateInstance();
352 // Get the base image
353 $templateInstance->loadCodeTemplate($this->getBaseImage());
355 // Assign all the image values with the template
356 $templateInstance->assignVariable('image_name' , $this->getImageName());
357 $templateInstance->assignVariable('image_type' , $this->getImageType());
358 $templateInstance->assignVariable('image_width' , $this->getWidth());
359 $templateInstance->assignVariable('image_height' , $this->getHeight());
360 $templateInstance->assignVariable('image_bg_red' , $this->backgroundColor['red']);
361 $templateInstance->assignVariable('image_bg_green', $this->backgroundColor['green']);
362 $templateInstance->assignVariable('image_bg_blue' , $this->backgroundColor['blue']);
363 $templateInstance->assignVariable('image_fg_red' , $this->foregroundColor['red']);
364 $templateInstance->assignVariable('image_fg_green', $this->foregroundColor['green']);
365 $templateInstance->assignVariable('image_fg_blue' , $this->foregroundColor['blue']);
368 foreach ($this->imageStrings as $id => $imageString) {
369 // Set current string id to keep this helper in sync with template engine
370 $this->currString = $id;
372 // Set variable group
373 $templateInstance->setVariableGroup($id);
375 // Add group variables
376 $templateInstance->addGroupVariable('image_x' , $this->getX());
377 $templateInstance->addGroupVariable('image_y' , $this->getY());
378 $templateInstance->addGroupVariable('image_size' , $this->getFontSize());
379 $templateInstance->addGroupVariable('image_string', $this->getImageString());
382 // Get the raw content
383 $imageContent = $templateInstance->getRawTemplateData();
385 // Transfer all to the template engine
386 $templateInstance->renderXmlContent($imageContent);