0

Following is the code which works fine, but i want to add my own background img instead of blue. Also want to add alphabets in the code rand? Please help...

<?php
session_start();
$code=rand(10000,99999);
$_SESSION["code"]=$code ;
$im = imagecreatetruecolor(60, 24);
$bg = imagecolorallocate($im, 22, 86, 165);
$fg = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $bg);
imagestring($im, 5, 5, 5,  $code, $fg);
header("Cache-Control: no-cache, must-revalidate");
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Target
  • 199
  • 4
  • 19

1 Answers1

0

The idea of using a CAPTCHA is to tell Computers and Humans Apart. This CAPTCHA can be easily broken by a OCR program.

Having said that, you need to do the following:

<?php
session_start();
$code=rand(10000,99999);
$_SESSION["code"]=$code ;
$im = imagecreatefrompng("bg.png");
$fg = imagecolorallocate($im, 255, 255, 255);
imagestring($im, 5, 5, 5,  $code, $fg);
header("Cache-Control: no-cache, must-revalidate");
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
msound
  • 445
  • 3
  • 7
  • As for using an Alphanumeric CAPTCHA, you can refer to [this](http://stackoverflow.com/a/4356295/3461549) – msound Apr 01 '14 at 01:28
  • Thank you, this works fine. One last thing how to add alphabets and increase the font size. – Target Apr 01 '14 at 03:01
  • Instead of doing $code=rand(10000,99999), you can do $code=generateRandomString(5). For implementation of the generateRandomeString(), please refer to [this discussion](http://stackoverflow.com/a/4356295/3461549). For increasing the font-size, 5 is the biggest font available and you are already using it. So, you need to consider loading your own font. You can refer to the function [imageloadfont](http://us3.php.net/manual/en/function.imageloadfont.php) – msound Apr 02 '14 at 13:50