Online PHP CAPTCHA Image Script Generator

Create your own style of simple CAPTCHA image using PHP. This tool generates random CAPTCHA images. Enter image width, height, background color, text color, and character length in the textbox below. You can see the image style instantly when entering image attributes.

After entering all the image attributes, click the generate button below to get the code.

Demo CAPTCHA image
Demo

Features Of This CAPTCHA Image

  • Set Random Background Color
  • Multiple Font Style
  • Captcha String Can Contain Numbers And Letters, Uppercase And Lowercase Letters
  • Captcha String Length Can Be Set From 3 To 6 Characters
  • Multiple Background Style
  • Set Image Background Design Fixed Color Or Random Color
  • Text Angle Style Rotate

Set Captcha Image Style

A few notes about creating this captcha image

  • It is better to avoid image background effect while choosing some font style. Sometimes letters cannot be seen properly.
  • Increasing the number of characters in the captcha image should increase the width of the image accordingly.
  • You can use multiple font styles if no background effect is selected.
Width
Height
Font size
BG color
Font color
Font style
Captcha chars
Chars length
Text position
Text angle
Background
BG shape color
Preview
CAPTCHA

The captcha-image.php page and font.ttf file should be in the same folder.

You can download it as a zip file including fonts and all pages.

Folder File Structure
test-contact
captcha
image.php
font.ttf
index.html
contact.php
Download Font files
captcha-image.php
<?php
    session_start();
    //PHP CAPTCHA image
    //Generated by https://www.html-code-generator.com/php/captcha-image-code-generator

    $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;
    $token = '';
    for($i = 0; $i < $chars_length; $i++) {
        $letter = $captcha_characters[rand(0, strlen($captcha_characters)-1)];
        $token .= $letter;
        if ($i == 0) {
            $x = 0;
        }else {
            $x = $xw*$i;
        }
        imagettftext($image, $font_size, rand(-20,20), $x+$font_gap, rand(19, $height-5), $font_color, $font, $letter);
    }

    // Store the token in the session variable
    $_SESSION['captcha_token'] = strtolower($token);

    // display image
    header('Content-Type: image/png');
    imagepng($image);
    imagedestroy($image);
?>
index.html
<!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>
        const refreshButton = document.getElementById("refresh-captcha");
        const captchaImage = document.getElementById("image-captcha");

        refreshButton.onclick = function(event) {
            event.preventDefault();
            captchaImage.src = './captcha/image.php?' + Date.now();
        };
    </script>

</body>
</html>
contact.php
<?php
    session_start();
    if ( isset($_POST['message'], $_POST['token']) ) {
        $token = strtolower($_POST['token']);
        // validate captcha code
        if (isset($_SESSION['captcha_token']) && $_SESSION['captcha_token'] == $token) {

            //success your code here
            $to = "somebody@example.com";
            $subject = "subject";
            $message = $_POST['message'];
            $headers = "From: webmaster@example.com" . "\r\n" .  "CC: somebodyelse@example.com";

            //mail($to, $subject, $message, $headers);
            echo "success";
        } else {
            echo "error CAPTCHA code";
        }
    }
?>