What are some best practices for generating Captcha images in PHP to prevent automated scripts from bypassing security measures?

To prevent automated scripts from bypassing security measures, it is important to generate Captcha images in PHP that are difficult for bots to decipher. This can be achieved by using a combination of random characters, distortion techniques, and noise to make it harder for automated scripts to recognize the text. Additionally, implementing a time limit for completing the Captcha can also help prevent bots from successfully bypassing the security measure.

<?php
session_start();

$randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 6);

$_SESSION['captcha'] = $randomString;

$width = 120;
$height = 40;

$image = imagecreatetruecolor($width, $height);

$bgColor = imagecolorallocate($image, 255, 255, 255);
$textColor = imagecolorallocate($image, 0, 0, 0);

imagefilledrectangle($image, 0, 0, $width, $height, $bgColor);

imagettftext($image, 20, 0, 10, 30, $textColor, 'arial.ttf', $randomString);

header('Content-type: image/png');

imagepng($image);
imagedestroy($image);
?>