Source for file Image.php
Documentation is available at Image.php
* Require Image_Text class for generating the text.
require_once 'Text/CAPTCHA.php';
require_once 'Image/Text.php';
* Text_CAPTCHA_Driver_Image - Text_CAPTCHA driver graphical CAPTCHAs
* Class to create a graphical Turing test
* @license PHP License, version 3.0
* @author Christian Wenz <wenz@php.net>
* @todo refine the obfuscation algorithm :-)
* @todo learn how to use Image_Text better (or remove dependency)
* Further options (here: for Image_Text)
var $_imageOptions = array (
'font_file' => 'COUR.TTF',
'text_color' => '#000000',
'lines_color' => '#CACACA',
'background_color' => false );
* Whether the immage resource has been created
* Initializes the new Text_CAPTCHA_Driver_Image object and creates a GD image
* @param array $options CAPTCHA options
* @return mixed true upon success, PEAR error otherwise
function init($options = array ())
// Compatibility mode ... in future versions, these two
// lines of code will be used:
// $this->_error = PEAR::raiseError('You need to provide a set of CAPTCHA options!');
if (isset ($args[2 ]) && $args[2 ] != null ) {
if (isset ($args[3 ]) && is_array($args[3 ])) {
$o['imageOptions'] = $args[3 ];
if (isset ($options['width']) && is_int($options['width'])) {
$this->_width = $options['width'];
if (isset ($options['height']) && is_int($options['height'])) {
$this->_height = $options['height'];
if (!isset ($options['phrase']) || empty ($options['phrase'])) {
$this->_phrase = $options['phrase'];
if (!isset ($options['output']) || empty ($options['output'])) {
$this->_output = 'resource';
$this->_output = $options['output'];
if (isset ($options['imageOptions']) && is_array($options['imageOptions']) && count($options['imageOptions']) > 0 ) {
$this->_imageOptions = array_merge($this->_imageOptions, $options['imageOptions']);
* Create random CAPTCHA phrase, Image edition (with size check)
* This method creates a random phrase, maximum 8 characters or width / 25, whatever is smaller
$this->_phrase = Text_Password ::create ($len);
* This method creates a CAPTCHA image
* @return void PEAR_Error on error
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';
$this->_imt = new Image_Text (
if (PEAR ::isError ($this->_imt->init ())) {
$this->_error = PEAR ::raiseError ('Error initializing Image_Text (font missing?!)');
$this->_imt->measurize ();
$this->_im = & $this->_imt->getImg ();
$colors = $this->_imt->_convertString2RGB ($this->_imageOptions['lines_color']);
for ($i = 0; $i < 3; $i++ ) {
$x1 = rand(0 , $this->_width - 1 );
$y1 = rand(0 , round($this->_height / 10 , 0 ));
$y2 = rand(0 , $this->_height - 1 );
imageline($this->_im, $x1, $y1, $x2, $y2, $lines_color);
$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($this->_im, $x1, $y1, $x2, $y2, $lines_color);
$cx = rand(0 , $this->_width - 50 ) + 25;
$cy = rand(0 , $this->_height - 50 ) + 25;
imagearc($this->_im, $cx, $cy, $w, $w, 0 , 360 , $lines_color);
* Return CAPTCHA as image resource
* This method returns the CAPTCHA depending on the output format
* @return mixed image resource or PEAR error
$retval = $this->_createCAPTCHA ();
if (PEAR ::isError ($retval)) {
return PEAR ::raiseError ($retval->getMessage ());
switch ($this->_output) {
* This method returns the CAPTCHA as PNG
* @return mixed image contents or PEAR error
$retval = $this->_createCAPTCHA ();
if (PEAR ::isError ($retval)) {
return PEAR ::raiseError ($retval->getMessage ());
$this->_error = PEAR ::raiseError ('Error creating CAPTCHA image (font missing?!)');
* This method returns the CAPTCHA as JPEG
* @return mixed image contents or PEAR error
$retval = $this->_createCAPTCHA ();
if (PEAR ::isError ($retval)) {
return PEAR ::raiseError ($retval->getMessage ());
$this->_error = PEAR ::raiseError ('Error creating CAPTCHA image (font missing?!)');
* This method returns the CAPTCHA as GIF
* @return mixed image contents or PEAR error
$retval = $this->_createCAPTCHA ();
if (PEAR ::isError ($retval)) {
return PEAR ::raiseError ($retval->getMessage ());
$this->_error = PEAR ::raiseError ('Error creating CAPTCHA image (font missing?!)');
* __wakeup method (PHP 5 only)
Documentation generated on Mon, 11 Mar 2019 14:44:40 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|