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