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 BaseHelper 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);
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) {
211 $this->backgroundColor['red'] = (int) $red;
212 $this->backgroundColor['green'] = (int) $green;
213 $this->backgroundColor['blue'] = (int) $blue;
217 * Setter for RGB of foreground color
219 * @param $red Color value for red
220 * @param $green Color value for green
221 * @param $blue Color value for blue
224 public final function setForegroundColorRedGreenBlue ($red, $green, $blue) {
225 $this->foregroundColor['red'] = (int) $red;
226 $this->foregroundColor['green'] = (int) $green;
227 $this->foregroundColor['blue'] = (int) $blue;
231 * Adds an image string to the buffer by the given string name
233 * @param $stringName String name (identifier)
235 public function addTextLine ($stringName) {
236 // Create the image string
237 $this->imageStrings[$stringName] = array(
244 // Set current string name
245 $this->currString = $stringName;
249 * Setter for image message string
251 * @param $imageString A message to display in the image
254 public final function setImageString ($imageString) {
255 $this->imageStrings[$this->currString]['string'] = (string) $imageString;
259 * Getter for image message string
261 * @return $imageString A message to display in the image
263 public final function getImageString () {
264 return $this->imageStrings[$this->currString]['string'];
268 * Setter for X/Y coordinates for strings
270 * @param $x X coordinate
271 * @param $y Y coordinate
274 public final function setCoord ($x, $y) {
275 $this->imageStrings[$this->currString]['x'] = (int) $x;
276 $this->imageStrings[$this->currString]['y'] = (int) $y;
280 * Getter for X coordinate
282 * @return $x X coordinate
284 public final function getX () {
285 return $this->imageStrings[$this->currString]['x'];
289 * Getter for Y coordinate
291 * @return $y Y coordinate
293 public final function getY () {
294 return $this->imageStrings[$this->currString]['y'];
298 * Setter for font size
300 * @param $fontSize Font size for strings
303 public final function setFontSize ($fontSize) {
304 $this->imageStrings[$this->currString]['size'] = (int) $fontSize;
308 * Getter for font size
310 * @return $fontSize Font size for strings
312 public final function getFontSize () {
313 return $this->imageStrings[$this->currString]['size'];
317 * Flushs the content out
321 public function flushContent () {
322 // Get a template instance
323 $templateInstance = $this->getTemplateInstance();
325 // Get the base image
326 $templateInstance->loadCodeTemplate($this->getBaseImage());
328 // Assign all the image values with the template
329 $templateInstance->assignVariable("image_name" , $this->getImageName());
330 $templateInstance->assignVariable("image_type" , $this->getImageType());
331 $templateInstance->assignVariable("image_width" , $this->getWidth());
332 $templateInstance->assignVariable("image_height" , $this->getHeight());
333 $templateInstance->assignVariable("image_bg_red" , $this->backgroundColor['red']);
334 $templateInstance->assignVariable("image_bg_green", $this->backgroundColor['green']);
335 $templateInstance->assignVariable("image_bg_blue" , $this->backgroundColor['blue']);
336 $templateInstance->assignVariable("image_fg_red" , $this->foregroundColor['red']);
337 $templateInstance->assignVariable("image_fg_green", $this->foregroundColor['green']);
338 $templateInstance->assignVariable("image_fg_blue" , $this->foregroundColor['blue']);
341 foreach ($this->imageStrings as $id=>$imageString) {
342 // Set current string id
343 $this->currString = $id;
345 // Set variable group
346 $templateInstance->setVariableGroup($id);
348 // Add group variables
349 $templateInstance->addGroupVariable("image_x" , $this->getX());
350 $templateInstance->addGroupVariable("image_y" , $this->getY());
351 $templateInstance->addGroupVariable("image_size" , $this->getFontSize());
352 $templateInstance->addGroupVariable("image_string", $this->getImageString());
355 // Get the raw content
356 $imageContent = $templateInstance->getRawTemplateData();
358 // Transfer all to the template engine
359 $templateInstance->renderImageContent($imageContent);