4.
– With a Captcha you are asked to re-enter some text displayed in a graphic image.
A bot will not be able to decipher it if the image is complex enough and so only human input is able to get through. This still doesn’t guarantee you will be spam free, but with this plug-in you’ll prevent the majority of it from getting through.
<?php
$result = PIPHP_CreateCaptcha(26, 8, 'captcha.ttf', '',
'!*a&K', ".fs£!+");
$captcha = $result[0];
$token = $result[1];
$image = $result[2];
echo "The Captcha is '$captcha', the token is:<br />";
echo "'$token', and the image is in:<br />$image:<br />";
echo "<img src=\"$image\">";
function PIPHP_CreateCaptcha($size, $length, $font,
$folder, $salt1, $salt2)
{
// Plug-in 33: Create Captcha
//
// This plug-in creates a GIF image containing a word the
// user must type in to prove they are not a program. The
// function returns a three element array containing the
// following:
//
// Element 0: The Captcha word to be entered
// Element 1: A unique 32 character token
// Element 2: The location of a GIF file with the Captcha
// text
//
// The function expects a file dictionary.txt to exist in the
// current folder. This must be a text file of words separated
// by \r\n carriage return, line feed pairs. The arguments
// required are:
//
// $size: Font size for the Captcha
// $length: Length of Captcha in letters
// $font: Location of a TrueType font
// $folder: Location of a temporary, web-
// accessible folder to store the
// captcha GIF. Must end with /
// $salt1: A sequence of characters to help
// make the Captcha uncrackable
// $salt2: A second sequence to make it even
// less crackable
$file = file_get_contents('dictionary.txt');
$temps = explode("\r\n", $file);
$dict = array();
foreach ($temps as $temp)
if (strlen($temp) == $length)
$dict[] = $temp;
$captcha = $dict[rand(0, count($dict) - 1)];
$token = md5("$salt1$captcha$salt2");
$fname = "$folder" . $token . ".gif";
PIPHP_GifText($fname, $captcha, $font, $size, "444444",
"ffffff", $size / 10, "666666");
$image = imagecreatefromgif($fname);
$image = PIPHP_ImageAlter($image, 2);
$image = PIPHP_ImageAlter($image, 13);
for ($j = 0 ; $j < 3 ; ++$j)
$image = PIPHP_ImageAlter($image, 3);
for ($j = 0 ; $j < 2 ; ++$j)
$image = PIPHP_ImageAlter($image, 5);
imagegif($image, $fname);
return array($captcha, $token, $fname);
}
// The plug-ins below are in this file to ensure they
// are available to the main plug-in which uses them
function PIPHP_GifText($file, $text, $font, $size, $fore, $back,
$shadow, $shadowcolor)
{
// Plug-in 19: Gif Text
//
// This plug-in accepts text input and then turns it into
// a gif image. Various font sizes and effects are available
// The arguments are:
//
// $file: The path and file to save the finished gif
// $text: The text to display
// $font: Filename of a TTF font file
// $size: Font size to use
// $fore: The foreground color
// $back: The background color
// $shadow: 0 = None, 1 or more = The offset to use
// $shadowcolor: The shadow color (if selected)
$bound = imagettfbbox($size, 0, $font, $text);
$width = $bound[2] + $bound[0] + 6 + $shadow;
$height = abs($bound[1]) + abs($bound[7]) + 5 + $shadow;
$image = imagecreatetruecolor($width, $height);
$bgcol = PIPHP_GD_FN1($image, $back);
$fgcol = PIPHP_GD_FN1($image, $fore);
$shcol = PIPHP_GD_FN1($image, $shadowcolor);
imagefilledrectangle($image, 0, 0, $width, $height, $bgcol);
if ($shadow > 0) imagettftext($image, $size, 0, $shadow + 2,
abs($bound[5]) + $shadow + 2, $shcol, $font, $text);
imagettftext($image, $size, 0, 2, abs($bound[5]) + 2, $fgcol,
$font, $text);
imagegif($image, $file);
}
function PIPHP_GD_FN1($image, $color)
{
return imagecolorallocate($image,
hexdec(substr($color, 0, 2)),
hexdec(substr($color, 2, 2)),
hexdec(substr($color, 4, 2)));
}
function PIPHP_ImageAlter($image, $effect)
{
// Plug-in 14: Image Alter
//
// This plug-in takes a GD image and modifies it
// according to the selected effect. The arguments are:
//
// $image: The image source
// $effect: The effect to use between 1 and 14:
// 1 = Sharpen
// 2 = Blur
// 3 = Brighten
// 4 = Darken
// 5 = Increase Contrast
// 6 = Decrease Contrast
// 7 = Grayscale
// 8 = Invert
// 9 = Increase Red
// 10 = Increase Green
// 11 = Increase Blue
// 12 = Edge Detect
// 13 = Emboss
// 14 = Sketchify
switch($effect)
{
case 1: imageconvolution($image, array(array(-1, -1, -1),
array(-1, 16, -1), array(-1, -1, -1)), 8, 0);
break;
case 2: imagefilter($image,
IMG_FILTER_GAUSSIAN_BLUR); break;
case 3: imagefilter($image,
IMG_FILTER_BRIGHTNESS, 20); break;
case 4: imagefilter($image,
IMG_FILTER_BRIGHTNESS, -20); break;
case 5: imagefilter($image,
IMG_FILTER_CONTRAST, -20); break;
case 6: imagefilter($image,
IMG_FILTER_CONTRAST, 20); break;
case 7: imagefilter($image,
IMG_FILTER_GRAYSCALE); break;
case 8: imagefilter($image,
IMG_FILTER_NEGATE); break;
case 9: imagefilter($image,
IMG_FILTER_COLORIZE, 128, 0, 0, 50); break;
case 10: imagefilter($image,
IMG_FILTER_COLORIZE, 0, 128, 0, 50); break;
case 11: imagefilter($image,
IMG_FILTER_COLORIZE, 0, 0, 128, 50); break;
case 12: imagefilter($image,
IMG_FILTER_EDGEDETECT); break;
case 13: imagefilter($image,
IMG_FILTER_EMBOSS); break;
case 14: imagefilter($image,
IMG_FILTER_MEAN_REMOVAL); break;
}
return $image;
}
?>