Generating a PNG CAPTCHA Image with PHP

This tutorial explains how to create a simple PNG CAPTCHA using PHP by generating a random four‑character code, drawing it with random rotation and position, adding noise lines and dots, and outputting the image for use in form validation.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Generating a PNG CAPTCHA Image with PHP

CAPTCHA images are increasingly used in web forms, and implementing them with PHP can be more convenient than using JavaScript.

This article presents a basic PHP script that generates a PNG CAPTCHA, outlining each step: creating an image canvas, setting a random background color, defining font color and style, generating a four‑character random code, drawing each character with random rotation and position, adding noise lines and dots to hinder automated recognition, outputting the PNG image, and finally releasing the image memory.

The script can be wrapped into a reusable function for easier integration into future projects.

<?php
session_start();
header('Content-type: image/png');
// Create image
$im = imagecreate($x=130,$y=45);
// Allocate background color (first call fills background)
$bg = imagecolorallocate($im,rand(50,200),rand(0,155),rand(0,155));
// Font color
$fontColor = imageColorAllocate($im,255,255,255);
// Font style (place the .ttf file in the same directory as this script)
$fontstyle = 'rock.ttf';
// Generate random characters
for($i = 0; $i < 4; $i++) {
    $randAsciiNumArray = array(rand(48,57),rand(65,90));
    $randAsciiNum = $randAsciiNumArray[rand(0,1)];
    $randStr = chr($randAsciiNum);
    imagettftext($im,30,rand(0,20)-rand(0,25),5+$i*30,rand(30,35),$fontColor,$fontstyle,$randStr);
    $authcode .= $randStr;
}
// Store the code in session for later verification
$_SESSION['authcode'] = $authcode;
// Add interference lines
for($i=0;$i<8;$i++){
    $lineColor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
    imageline($im,rand(0,$x),0,rand(0,$x),$y,$lineColor);
}
// Add interference dots
for($i=0;$i<250;$i++){
    imagesetpixel($im,rand(0,$x),rand(0,$y),$fontColor);
}
// Output image
imagepng($im);
// Free memory
imagedestroy($im);
?>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

SecurityCaptchaimage generation
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.