PHP CAPTCHA Image Script Generator
PHP Build Your Own Style Simple CAPTCHA Image
Demo CAPTCHA :
Demo Create Captcha Image Style
width:
height:
BG color:
font size:
font color:
font style:
captcha chars:
chars length:
Text position:
Text angle:
Background:
BG shape color:
If you have made any changes, click the Generate Code button before downloading the code
You can download it as a zip file
test-contact
captcha
image.php
font.ttf
index.html
contact.php
<?php
session_start();
//PHP CAPTCHA image
//Generated by https://www.html-code-generator.com/php/captcha-image-code-generator.php
$width = 130;
$height = 30;
$font_size = 20;
$font = "./verdana.ttf";
$font = realpath($font);
$chars_length = 4;
$captcha_characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$image = imagecreatetruecolor($width, $height);
$bg_color = imagecolorallocate($image, 255, 0, 0);
$font_color = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image, 0, 0, $width, $height, $bg_color);
//background random-line
$vert_line = round($width/5);
$color = imagecolorallocate($image, 255, 255, 255);
for($i=0; $i < $vert_line; $i++) {
imageline($image, rand(0,$width), rand(0,$height), rand(0,$height), rand(0,$width), $color);
}
$xw = ($width/$chars_length);
$x = 0;
$font_gap = $xw/2-$font_size/2;
$digit = '';
for($i = 0; $i < $chars_length; $i++) {
$letter = $captcha_characters[rand(0, strlen($captcha_characters)-1)];
$digit .= $letter;
if ($i == 0) {
$x = 0;
}else {
$x = $xw*$i;
}
imagettftext($image, $font_size, rand(-20,20), $x+$font_gap, rand(25, $height-5), $font_color, $font, $letter);
}
// record token in session variable
$_SESSION['captcha_token'] = strtolower($digit);
// display image
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP CAPTCHA test form</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>
<div class="p-5">
<div class="card">
<div class="card-header">Message</div>
<div class="card-body">
<form id="contact-form" method="post" action="contact.php">
<div class="form-group row align-items-center">
<label for="message" class="col-sm-2 col-form-label">Message *</label>
<div class="col-sm-10">
<textarea class="form-control" rows="2" name="message" id="message"></textarea>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Captcha *</label>
<div class="col-sm-10">
<div class="form-row align-items-center">
<div class="col mb-3">
<input type="text" class="form-control" name="token" id="token" placeholder="Captcha" style="min-width: 150px;">
</div>
<div class="col mb-3">
<img src="captcha/image.php?12325" alt="CAPTCHA" id="image-captcha">
<a href="#" id="refresh-captcha" class="align-middle" title="refresh"><i class="material-icons align-middle">refresh</i></a>
</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary" name="submit" id="submit">submit</button>
</form>
</div>
</div>
</div>
<script type="text/javascript">
var refreshButton = document.getElementById("refresh-captcha");
var captchaImage = document.getElementById("image-captcha");
refreshButton.onclick = function(event) {
event.preventDefault();
captchaImage.src = 'captcha/image.php?' + Date.now();
}
</script>
</body>
</html>
<?php
session_start();
if (isset($_POST['submit'])) {
$message = $_POST['message'];
$token = strtolower($_POST['token']);
// validate captcha code
if (isset($_SESSION['captcha_token']) && $_SESSION['captcha_token'] == $token) {
//success your code here
echo "success";
} else {
echo "error CAPTCHA code";
}
}
?>
font.ttf