Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00015 final class TuringImage
00016 {
00017 private $textColors = null;
00018 private $backgroundColors = null;
00019
00020 private $font = null;
00021
00022 private $imageId = null;
00023
00024 private $width = null;
00025 private $height = null;
00026
00027 private $generator = null;
00028
00029 private $drawer = null;
00030 private $backgroundDrawer = null;
00031
00032 private $code = null;
00033
00034 public function __construct($width, $height)
00035 {
00036 $this->width = $width;
00037 $this->height = $height;
00038
00039 $this->generator = new CodeGenerator();
00040 $this->textColors = new ColorArray();
00041 $this->backgroundColors = new ColorArray();
00042 }
00043
00047 public function setGeneratedCode($code)
00048 {
00049 $this->code = $code;
00050
00051 return $this;
00052 }
00053
00054 public function getGeneratedCode()
00055 {
00056 if (!$this->code)
00057 $this->code = $this->generator->generate();
00058
00059 return $this->code;
00060 }
00061
00062 public function getTextColors()
00063 {
00064 return $this->textColors;
00065 }
00066
00067 public function getBackgroundColors()
00068 {
00069 return $this->backgroundColors;
00070 }
00071
00072 public function getWidth()
00073 {
00074 return $this->width;
00075 }
00076
00077 public function getHeight()
00078 {
00079 return $this->height;
00080 }
00081
00082 public function getImageId()
00083 {
00084 return $this->imageId;
00085 }
00086
00087 public function getFont()
00088 {
00089 return $this->font;
00090 }
00091
00092 public function setFont($font)
00093 {
00094 $this->font = $font;
00095
00096 return $this;
00097 }
00098
00102 public function setTextDrawer(TextDrawer $drawer)
00103 {
00104 $drawer->setTuringImage($this);
00105 $this->drawer = $drawer;
00106
00107 return $this;
00108 }
00109
00113 public function setBackgroundDrawer(BackgroundDrawer $drawer)
00114 {
00115 $drawer->setTuringImage($this);
00116 $this->backgroundDrawer = $drawer;
00117
00118 return $this;
00119 }
00120
00124 public function getCodeGenerator()
00125 {
00126 return $this->generator;
00127 }
00128
00129 public function getColorIdentifier(Color $color)
00130 {
00131 $colorId =
00132 imagecolorexact(
00133 $this->imageId,
00134 $color->getRed(),
00135 $color->getGreen(),
00136 $color->getBlue()
00137 );
00138
00139 if ($colorId === -1)
00140 $colorId =
00141 imagecolorallocate(
00142 $this->imageId,
00143 $color->getRed(),
00144 $color->getGreen(),
00145 $color->getBlue()
00146 );
00147
00148 return $colorId;
00149 }
00150
00151 public function getOneCharacterColor()
00152 {
00153 $textColor=$this->textColors->getRandomTextColor();
00154
00155 return $this->getColorIdentifier($textColor);
00156 }
00157
00161 public function toImage(ImageType $imageType)
00162 {
00163 if ($this->drawer === null)
00164 throw new WrongStateException('drawer must present');
00165
00166 $this->init();
00167
00168 $this->drawBackGround();
00169
00170 $this->drawer->draw($this->getGeneratedCode());
00171
00172 $this->outputImage($imageType);
00173
00174 imagedestroy($this->getImageId());
00175
00176 return $this;
00177 }
00178
00182 private function init()
00183 {
00184 $imageId = imagecreate($this->getWidth(), $this->getHeight());
00185 $this->imageId = $imageId;
00186
00187 $this->getColorIdentifier(new Color('FFFFFF'));
00188
00189 return $this;
00190 }
00191
00195 private function drawBackGround()
00196 {
00197 if (!$this->backgroundColors->isEmpty()) {
00198 $backgroundColor = $this->backgroundColors->getRandomTextColor();
00199
00200 if ($backgroundColor !== null) {
00201 $backgroundColorId = $this->getColorIdentifier($backgroundColor);
00202
00203 imagefilledrectangle(
00204 $this->imageId,
00205 0,
00206 0,
00207 $this->getWidth(),
00208 $this->getHeight(),
00209 $backgroundColorId
00210 );
00211 }
00212 }
00213
00214 if ($this->backgroundDrawer !== null)
00215 $this->backgroundDrawer->draw();
00216
00217 return $this;
00218 }
00219
00223 private function outputImage(ImageType $imageType)
00224 {
00225 $gdImageTypes = imagetypes();
00226
00227 switch ($imageType->getId()) {
00228
00229 case ImageType::WBMP:
00230
00231 if ($gdImageTypes & IMG_WBMP) {
00232 header("Content-type: image/vnd.wap.wbmp");
00233 imagewbmp($this->imageId);
00234 break;
00235 }
00236
00237 case ImageType::PNG:
00238
00239 if ($gdImageTypes & IMG_PNG) {
00240 header("Content-type: image/png");
00241 imagepng($this->imageId);
00242 break;
00243 }
00244
00245 case ImageType::JPEG:
00246
00247 if ($gdImageTypes & IMG_JPG) {
00248 header("Content-type: image/jpeg");
00249 imagejpeg($this->imageId);
00250 break;
00251 }
00252
00253 case ImageType::GIF:
00254
00255 if ($gdImageTypes & IMG_GIF ) {
00256 header("Content-type: image/gif");
00257 imagegif($this->imageId);
00258 break;
00259 }
00260
00261 default:
00262
00263 throw new UnimplementedFeatureException(
00264 'requesting non-supported format'
00265 );
00266 }
00267
00268 return $this;
00269 }
00270 }
00271 ?>