Code:
<?php
function Generate() {
// Set the content-type to display the image properly.
header('Content-type: image/png');
// Create the image. imagecreatetruecolor(Width, Height);
$image = imagecreatetruecolor(400, 30);
// Set the colors to be used. These use RGB color codes.
// View the following link to easliy find RGB color codes:
// https://www.color-he*****m/
$white = imagecolorallocate($image, 255, 255, 255); // Used for the background fill, see below. (Default White = 255, 255, 255)
$text_shadow = imagecolorallocate($image, 128, 128, 128); // Used for the text shadowing, see below. (Default Gray = 128, 128, 128)
$text_color = imagecolorallocate($image, 0, 0, 0); // Used for the text color, see below. (Default Black = 0, 0, 0)
imagefilledrectangle($image, 0, 0, 399, 29, $white); // Fills the background in white.
// Call for the random text to be used and set the value.
$text = RandomText();
// You may change this to your own .ttf font file.
// Thanks and credit to the following website and author for this great default font!
// https://www.dafon*****m/chaos-times.font
$font = 'ChaosTimes_lig.ttf';
// Adds gray shadowing to the text, using the $text_shadow color.
// This also adds slightly more distortion to the image, making it
// slightly more difficult.
imagettftext($image, 20, 0, 11, 21, $text_shadow, $font, $text);
// Random text and colors have been set, time to put it all together.
// This is also where we use $text_color to set the font color.
imagettftext($image, 20, 0, 10, 20, $text_color, $font, $text);
// Using imagepng() results in clear text compared to imagejpeg().
// If you wanted to distort the image even more, you could use
// imagejpeg($im); in place of the following. Be sure to update
// header('Content-type: image/png'); TO header('Content-type: image/jpeg');
imagepng($image);
imagedestroy($image);
}
// Randomly generates the text to be shown.
// The following line is also where to edit the different values,
// such as the $length which is how long the captcha will be.
// Also $chars which contains all the things to use in the captcha.
function RandomText($length = 8, $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") {
$charl = strlen($chars); // Here we get the length of $chars, used to create random text from it.
$string = ''; // Make sure the string is empty. Shouldn't normally be required but a good habit.
for ($i=0; $i<$length; $i++) { // If you require some explaining here please view: https://goo .gl/yYjC2j
$string .= $chars[rand(0, $charl - 1)]; // Set $string to random text using $chars as the text options.
}
return $string; // Return the random text generated.
}
// Output the finished image
echo Generate();
?>