Example #1
0
 /**
  * Create CAPTCHA image.
  *
  * This method creates a CAPTCHA image.
  *
  * @return void
  * @throws Text_CAPTCHA_Exception when image generation with Image_Text produces
  *               an error
  */
 public function createCAPTCHA()
 {
     $options['canvas'] = array('width' => $this->_width, 'height' => $this->_height);
     $options['width'] = $this->_width - 20;
     $options['height'] = $this->_height - 20;
     $options['cx'] = ceil($this->_width / 2 + 10);
     $options['cy'] = ceil($this->_height / 2 + 10);
     $options['angle'] = rand(0, 30) - 15;
     $options['font_size'] = $this->_imageOptions['font_size'];
     $options['font_path'] = $this->_imageOptions['font_path'];
     $options['font_file'] = $this->_imageOptions['font_file'];
     $options['color'] = array($this->_imageOptions['text_color']);
     $options['background_color'] = $this->_imageOptions['background_color'];
     $options['max_lines'] = 1;
     $options['mode'] = 'auto';
     do {
         $imageText = new Image_Text($this->getPhrase(), $options);
         $imageText->init();
         $result = $imageText->measurize();
     } while ($result === false && --$options['font_size'] > 0);
     if ($result === false) {
         throw new Text_CAPTCHA_Exception('The text provided does not fit in the image dimensions');
     }
     $imageText->render();
     $image = $imageText->getImg();
     if ($this->_imageOptions['antialias'] && function_exists('imageantialias')) {
         imageantialias($image, true);
     }
     $colors = Image_Text::convertString2RGB($this->_imageOptions['lines_color']);
     $linesColor = imagecolorallocate($image, $colors['r'], $colors['g'], $colors['b']);
     //some obfuscation
     for ($i = 0; $i < 3; $i++) {
         $x1 = rand(0, $this->_width - 1);
         $y1 = rand(0, round($this->_height / 10, 0));
         $x2 = rand(0, round($this->_width / 10, 0));
         $y2 = rand(0, $this->_height - 1);
         imageline($image, $x1, $y1, $x2, $y2, $linesColor);
         $x1 = rand(0, $this->_width - 1);
         $y1 = $this->_height - rand(1, round($this->_height / 10, 0));
         $x2 = $this->_width - rand(1, round($this->_width / 10, 0));
         $y2 = rand(0, $this->_height - 1);
         imageline($image, $x1, $y1, $x2, $y2, $linesColor);
         $cx = rand(0, $this->_width - 50) + 25;
         $cy = rand(0, $this->_height - 50) + 25;
         $w = rand(1, 24);
         imagearc($image, $cx, $cy, $w, $w, 0, 360, $linesColor);
     }
     // @todo remove hardcoded value
     $this->_output = 'jpg';
     if ($this->_output == 'gif' && imagetypes() & IMG_GIF) {
         $this->setCaptcha($this->_getCAPTCHAAsGIF($image));
     } else {
         if ($this->_output == 'jpg' && imagetypes() & IMG_JPG || $this->_output == 'jpeg' && imagetypes() & IMG_JPEG) {
             $this->setCaptcha($this->_getCAPTCHAAsJPEG($image));
         } else {
             if ($this->_output == 'png' && imagetypes() & IMG_PNG) {
                 $this->setCaptcha($this->_getCAPTCHAAsPNG($image));
             } else {
                 if ($this->_output == 'resource') {
                     $this->setCaptcha($image);
                 } else {
                     throw new Text_CAPTCHA_Exception("Unknown or unsupported output type specified");
                 }
             }
         }
     }
     imagedestroy($image);
 }
Example #2
0
 /**
  *
  */
 public function test_convertString2RGB()
 {
     $this->assertEquals(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0), Image_Text::_convertString2RGB('#FFFFFF'));
     $this->assertEquals(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0), Image_Text::_convertString2RGB('#00FFFFFF'));
     $this->assertEquals(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0), Image_Text::_convertString2RGB('#000000'));
     $this->assertEquals(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 255), Image_Text::_convertString2RGB('#FF000000'));
 }
Example #3
0
 /**
  * Construct and initialize an Image_Text in one step.
  * This method is called statically and creates plus initializes an Image_Text object.
  * Beware: You will have to recall init() if you set an option afterwards manually.
  *
  * @param   string  $text       Text to print.
  * @param   array   $options    Options.
  * @access public
  * @static
  * @see Image_Text::set(), Image_Text::Image_Text(), Image_Text::init()
  */
 function &construct($text, $options)
 {
     $itext = new Image_Text($text, $options);
     $res = $itext->init();
     if (PEAR::isError($res)) {
         return $res;
     }
     return $itext;
 }
Example #4
0
 /**
  * Construct and initialize an Image_Text in one step.
  * This method is called statically and creates plus initializes an Image_Text
  * object. Beware: You will have to recall init() if you set an option afterwards
  * manually.
  *
  * @param string $text    Text to print.
  * @param array  $options Options.
  *
  * @return Image_Text
  * @see Image_Text::set(), Image_Text::Image_Text(), Image_Text::init()
  */
 public static function construct($text, $options)
 {
     $itext = new Image_Text($text, $options);
     $itext->init();
     return $itext;
 }