class_ImageHelper.php
Go to the documentation of this file.00001 <?php
00024 class ImageHelper extends BaseCaptcha implements HelpableTemplate {
00028 private $imageType = "png";
00029
00033 private $imageName = "";
00034
00038 private $width = 0;
00039
00043 private $height = 0;
00044
00048 private $backgroundColor = array(
00049 'red' => 0,
00050 'green' => 0,
00051 'blue' => 0
00052 );
00053
00057 private $foregroundColor = array(
00058 'red' => 0,
00059 'green' => 0,
00060 'blue' => 0
00061 );
00062
00066 private $imageStrings = array();
00067
00071 private $currString = "";
00072
00078 protected function __construct () {
00079
00080 parent::__construct(__CLASS__);
00081 }
00082
00090 public final static function createImageHelper (CompileableTemplate $templateInstance, $imageType) {
00091
00092 $helperInstance = new ImageHelper();
00093
00094
00095 $helperInstance->setTemplateInstance($templateInstance);
00096
00097
00098 $helperInstance->setImageType($imageType);
00099
00100
00101 $helperInstance->initializeRandomNumberGenerator($templateInstance);
00102
00103
00104 return $helperInstance;
00105 }
00106
00113 protected final function setImageType ($imageType) {
00114 $this->imageType = (string) $imageType;
00115 }
00116
00122 public final function getImageType () {
00123 return $this->imageType;
00124 }
00125
00132 public final function setBaseImage ($baseImage) {
00133 $this->baseImage = (string) $baseImage;
00134 }
00135
00141 public final function getBaseImage () {
00142 return $this->baseImage;
00143 }
00144
00151 public final function setImageName ($imageName) {
00152 $this->imageName = (string) $imageName;
00153 }
00154
00160 protected final function getImageName () {
00161 return $this->imageName;
00162 }
00163
00170 public final function setWidth ($width) {
00171 $this->width = (int) $width;
00172 }
00173
00179 public final function getWidth () {
00180 return $this->width;
00181 }
00182
00189 public final function setHeight ($height) {
00190 $this->height = (int) $height;
00191 }
00192
00198 public final function getHeight () {
00199 return $this->height;
00200 }
00201
00210 public final function setBackgroundColorRedGreenBlue ($red, $green, $blue) {
00211
00212 if ($red === "rand") {
00213 $red = $this->getRngInstance()->randomNumber(0, 255);
00214 }
00215 if ($green === "rand") {
00216 $green = $this->getRngInstance()->randomNumber(0, 255);
00217 }
00218 if ($blue === "rand") {
00219 $blue = $this->getRngInstance()->randomNumber(0, 255);
00220 }
00221
00222 $this->backgroundColor['red'] = (int) $red;
00223 $this->backgroundColor['green'] = (int) $green;
00224 $this->backgroundColor['blue'] = (int) $blue;
00225 }
00226
00235 public final function setForegroundColorRedGreenBlue ($red, $green, $blue) {
00236
00237 if ($red === "rand") {
00238 $red = $this->getRngInstance()->randomNumber(0, 255);
00239 }
00240 if ($green === "rand") {
00241 $green = $this->getRngInstance()->randomNumber(0, 255);
00242 }
00243 if ($blue === "rand") {
00244 $blue = $this->getRngInstance()->randomNumber(0, 255);
00245 }
00246
00247 $this->foregroundColor['red'] = (int) $red;
00248 $this->foregroundColor['green'] = (int) $green;
00249 $this->foregroundColor['blue'] = (int) $blue;
00250 }
00251
00257 public function addTextLine ($stringName) {
00258
00259 $this->imageStrings[$stringName] = array(
00260 'x' => "",
00261 'y' => "",
00262 'size' => "",
00263 'string' => ""
00264 );
00265
00266
00267 $this->currString = $stringName;
00268 }
00269
00276 public final function setImageString ($imageString) {
00277 $this->imageStrings[$this->currString]['string'] = (string) $imageString;
00278 }
00279
00285 public final function getImageString () {
00286 return $this->imageStrings[$this->currString]['string'];
00287 }
00288
00296 public final function setCoord ($x, $y) {
00297 $this->imageStrings[$this->currString]['x'] = (int) $x;
00298 $this->imageStrings[$this->currString]['y'] = (int) $y;
00299 }
00300
00306 public final function getX () {
00307 return $this->imageStrings[$this->currString]['x'];
00308 }
00309
00315 public final function getY () {
00316 return $this->imageStrings[$this->currString]['y'];
00317 }
00318
00325 public final function setFontSize ($fontSize) {
00326
00327 if ($fontSize === "rand") {
00328 $fontSize = $this->getRngInstance()->randomNumber(4, 9);
00329 }
00330
00331 $this->imageStrings[$this->currString]['size'] = (int) $fontSize;
00332 }
00333
00339 public final function getFontSize () {
00340 return $this->imageStrings[$this->currString]['size'];
00341 }
00342
00348 public function flushContent () {
00349
00350 $templateInstance = $this->getTemplateInstance();
00351
00352
00353 $templateInstance->loadCodeTemplate($this->getBaseImage());
00354
00355
00356 $templateInstance->assignVariable('image_name' , $this->getImageName());
00357 $templateInstance->assignVariable('image_type' , $this->getImageType());
00358 $templateInstance->assignVariable('image_width' , $this->getWidth());
00359 $templateInstance->assignVariable('image_height' , $this->getHeight());
00360 $templateInstance->assignVariable('image_bg_red' , $this->backgroundColor['red']);
00361 $templateInstance->assignVariable('image_bg_green', $this->backgroundColor['green']);
00362 $templateInstance->assignVariable('image_bg_blue' , $this->backgroundColor['blue']);
00363 $templateInstance->assignVariable('image_fg_red' , $this->foregroundColor['red']);
00364 $templateInstance->assignVariable('image_fg_green', $this->foregroundColor['green']);
00365 $templateInstance->assignVariable('image_fg_blue' , $this->foregroundColor['blue']);
00366
00367
00368 foreach ($this->imageStrings as $id => $imageString) {
00369
00370 $this->currString = $id;
00371
00372
00373 $templateInstance->setVariableGroup($id);
00374
00375
00376 $templateInstance->addGroupVariable('image_x' , $this->getX());
00377 $templateInstance->addGroupVariable('image_y' , $this->getY());
00378 $templateInstance->addGroupVariable('image_size' , $this->getFontSize());
00379 $templateInstance->addGroupVariable('image_string', $this->getImageString());
00380 }
00381
00382
00383 $imageContent = $templateInstance->getRawTemplateData();
00384
00385
00386 $templateInstance->renderXmlContent($imageContent);
00387 }
00388 }
00389
00390
00391 ?>