CAPTCHA Image in PHP

CAPTCHA Image in PHP


CAPTCHA Image in PHP

CAPTCHA.png

Introduction

CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart) image is a way to prevent from bot attacks to our websites forms.
This article demonstrates how we can create our own CAPTCHA.

Using the Code

At first, we have to create a new true color image.
We can create it with imagecreatetruecolor [^] function in PHP.


$image = imagecreatetruecolor($this -> winWidth,


$this -> winHeight) //Create a new true color image


or die("<b>" . __FILE__ . "</b><br />" . __LINE__ . " : "

."Cannot Initialize new GD image stream");

$bg = imagecolorallocate($image, 255, 255, 255);//Allocate a color for an image

imagefill($image, 10, 10, $bg);//Flood fill
.
.
.
return imagepng($image);//Outputs or saves a PNG image from the given $image

imagedestroy($image);//rees any memory associated with image $image

This image is too simple, so we can change the background with a simple algorithm:

private $Colors = array ( 0 => 145,

1 => 204,

3 => 184,

2 => 177,
4 => 199,

for ($x=0; $x < $this -> winWidth; $x++)

5 => 255);
.
.
.
{
for ($y=0; $y < $this -> winHeight; $y++)
{

$this -> Colors["$random"],

$random = mt_rand(0 , 5);
$temp_color = imagecolorallocate($image,

}

$this -> Colors["$random"], $this -> Colors["$random"]);
imagesetpixel( $image, $x, $y , $temp_color );

}

The result of the above code:
BG1.png
Then we should write the characters in our image:

//e.g $this->Characters is the string that were writing in the image

//Font
$font = "tahomabd.ttf";
$font_size = 33;
//

$random_y = mt_rand(35 , 45);

$char = $this -> Characters[0];
$random_x = mt_rand(10 , 20);

//Writes the given text into the image using TrueType fonts.

$random_angle = mt_rand(-20 , 20);

$char_color, $font, $char);


imagettftext($image, $font_size, $random_angle, $random_x, $random_y,

We repeat it to fill our image with characters.
The result is as follows:

CAPTCHA-2.png
But I guess it is a little plain yet, so we can add an effect to our image for creating a more complex CAPTCHA.
Ive added a wave effect with a pure mathematic algorithm:


private $applyWave = true;

.
.
.

private function apply_wave($image, $width, $height)

{
$x_period = 10;
$y_period = 10;

$xp = $x_period*rand(1,3);

$y_amplitude = 5;
$x_amplitude = 5;


imagecopy($image, $image, $a-1, sin($k+$a/$xp)*$x_amplitude,

$k = rand(0,100);
for ($a = 0; $a<$width; $a++)

$k = rand(0,100);

$a, 0, 1, $height);

$yp = $y_period*rand(1,2);

imagecopy($image, $image, sin($k+$a/$yp)*$y_amplitude,

for ($a = 0; $a<$height; $a++)
$a-1, 0, $a, $width, 1);

return $image;

}

You can change the intensity of waves by changing these variables:
  • $x_period
  • $y_period
  • $y_amplitude
  • $x_amplitude
Lets see the result:
CAPTCHA.png
Ive also added some lines to the image :

private $showLine = true;

.
.
.
//draws lines

for ($i=0; $i<$this->winWidth; $i++ )

if ($this -> showLine)
{
{

imageline ( $image, $i, 0,

if ($i%10 == 0)
{

imageline ( $image, $i, 0,

$i+10, 50, $char_color );

}

$i-10, 50, $char_color );
}

}

The latest result:
CAPTCHA-lines.png
Thats all.
Good luck.


Dayyan CAPTCHA Class

Source Code:

<?php


/*


Created by Mohammad Dayyan (C)- 1387/3/2

http://www.mds-soft.persianblog.ir/

*/




if ( !defined(MohammadDayyan) )

die ("Hacking attempt <br /> <b>" . __FILE__ .

{


"<br /> Line " . __LINE__ . "</b>" );

exit;

}



class DayyanConfirmImage

private $showLine = true;

{


private $applyWave = true;

private $winHeight = 50;


private $winWidth = 320;



private $Characters; // random characters




private $Colors = array ( 0 => 145,


1 => 204,


2 => 177,


3 => 184,


4 => 199,


5 => 255);




////////////////////////////////////////////////////////////////////////////////

public function __construct($ConfirmCode)

link download

Comments

Popular posts from this blog

ABLiker APK Latest v1 1 Free Download For Android

Cara Install Windows 10

Adobe Photoshop CS6 13 0 1 Extended Free Software