Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone this is my first post here so please excuse me if I forget something or don't follow some standard. That being cleared I saw this code for generating captcha somewhere and I modified it to get random color for each character and some other small fixes, here is the code:
PHP
<?php
error_reporting(E_ALL);
header("Content-type: image/png");
captcha();
function ran_string($length=10){
        $string="";
        $pattern = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        for($i=0; $i<$length; $i++){
            $string .= $pattern[rand(0,61)];
        }

return $string;
}
function captcha()
{
$string=ran_string();
$im = imagecreate(100, 50);
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color =array( imagecolorallocate($im, 255, 0, 0),imagecolorallocate($im, 0, 255, 0),imagecolorallocate($im, 0, 0, 255),imagecolorallocate($im, 255, 255, 255));
for($i=0;$i<count($string);$i++)
{
    $x=($i+1)*10;
imagestring($im, 10, $x, 5,  $string[$i], $text_color[rand(0,3)]);
}/*
imagestring($im, 10, 10, 5,  $string[0], $text_color[rand(0,3)]);
imagestring($im, 10, 20, 5,  $string[1], $text_color[rand(0,3)]);
imagestring($im, 10, 30, 5,  $string[2], $text_color[rand(0,3)]);
imagestring($im, 10, 40, 5,  $string[3], $text_color[rand(0,3)]);
imagestring($im, 10, 50, 5,  $string[4], $text_color[rand(0,3)]);
imagestring($im, 10, 60, 5,  $string[5], $text_color[rand(0,3)]);*/
imagepng($im);
imagedestroy($im);
}
?>

But the output for the above is just one character and if I remove the "for loop" and uncomment the commented portion I get the desired result, please can someone help, I am sorry if this is a n00b question!!!
EDIT:
It was quite simple and silly of me, I used count($string) where I should have used strlen() anyways the correct version is this:
PHP
$font=3;
$spacing=5;
for($i=0;$i<strlen($string);$i++)
{
	$x=($i+1)*($font+$spacing);
        imagechar($im, $font, $x, 5,  $string[$i], $text_color[rand(0,3)]);
}
Posted
Updated 28-Jul-22 8:34am
v4
Comments
Scott Clayton 5-Oct-12 23:26pm    
Just as a side note, randomizing colors does very little to thwart automated CAPTCHA solvers. In fact it would take only one line to bypass using my CBL scripting language: http://code.google.com/p/captcha-breaking-library/
[no name] 28-Jul-22 14:28pm    
HTML?
[no name] 28-Jul-22 14:30pm    
A
[no name] 28-Jul-22 14:32pm    
mg

G<big></big>
 <?php
error_reporting(E_ALL);
header("Content-type: image/png");
captcha();<pre><pre lang="HTML">

function ran_string($length=10){
$string="";
$pattern = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
for($i=0; $i<$length; $i++){
$string .= $pattern[rand(0,61)];
}

return $string;
}
function captcha()
{
$string=ran_string();
$im = imagecreate(100, 50);
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color =array( imagecolorallocate($im, 255, 0, 0),imagecolorallocate($im, 0, 255, 0),imagecolorallocate($im, 0, 0, 255),imagecolorallocate($im, 255, 255, 255));
for($i=0;$i<count($<code>string);$i++)
{
$x=($i+1)*10;
imagestring($im, 10, $x, 5, $string[$i], $text_color[rand(0,3)]);
}/*
imagestring($im, 10, 10, 5, $string[0], $text_color[rand(0,3)]);
imagestring($im, 10, 20, 5, $string[1], $text_color[rand(0,3)]);
imagestring($im, 10, 30, 5, $string[2], $text_color[rand(0,3)]);
imagestring($im, 10, 40, 5, $string[3], $text_color[rand(0,3)]);
imagestring($im, 10, 50, 5, $string[4], $text_color[rand(0,3)]);
imagestring($im, 10, 60, 5, $string[5], $text_color[rand(0,3)]);*/
imagepng($im);
imagedestroy($im);
}
?>
 
Share this answer
 
Dart
<small><big></big></small>
<pre lang="MSIL">
 <?php
error_reporting(E_ALL);
header("Content-type: image/png");
captcha();
function ran_string($length=10){
        $string="";
        $pattern = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        for($i=0; $i<$length; $i++){
            $string .= $pattern[rand(0,61)];
        }

return $string;
}
function captcha()
{
$string=ran_string();
$im = imagecreate(100, 50);
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color =array( imagecolorallocate($im, 255, 0, 0),imagecolorallocate($im, 0, 255, 0),imagecolorallocate($im, 0, 0, 255),imagecolorallocate($im, 255, 255, 255));
for($i=0;$i<count($string);$i++)
{
    $x=($i+1)*10;
imagestring($im, 10, $x, 5,  $string[$i], $text_color[rand(0,3)]);
}/*
imagestring($im, 10, 10, 5,  $string[0], $text_color[rand(0,3)]);
imagestring($im, 10, 20, 5,  $string[1], $text_color[rand(0,3)]);
imagestring($im, 10, 30, 5,  $string[2], $text_color[rand(0,3)]);
imagestring($im, 10, 40, 5,  $string[3], $text_color[rand(0,3)]);
imagestring($im, 10, 50, 5,  $string[4], $text_color[rand(0,3)]);
imagestring($im, 10, 60, 5,  $string[5], $text_color[rand(0,3)]);*/
imagepng($im);
imagedestroy($im);
}
?>
 <?php
error_reporting(E_ALL);
header("Content-type: image/png");
captcha();
function ran_string($length=10){
        $string="";
        $pattern = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        for($i=0; $i<$length; $i++){
            $string .= $pattern[rand(0,61)];
        }

return $string;
}
function captcha()
{
$string=ran_string();
$im = imagecreate(100, 50);
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color =array( imagecolorallocate($im, 255, 0, 0),imagecolorallocate($im, 0, 255, 0),imagecolorallocate($im, 0, 0, 255),imagecolorallocate($im, 255, 255, 255));
for($i=0;$i<count($string);$i++)
{
    $x=($i+1)*10;
imagestring($im, 10, $x, 5,  $string[$i], $text_color[rand(0,3)]);
}/*
imagestring($im, 10, 10, 5,  $string[0], $text_color[rand(0,3)]);
imagestring($im, 10, 20, 5,  $string[1], $text_color[rand(0,3)]);
imagestring($im, 10, 30, 5,  $string[2], $text_color[rand(0,3)]);
imagestring($im, 10, 40, 5,  $string[3], $text_color[rand(0,3)]);
imagestring($im, 10, 50, 5,  $string[4], $text_color[rand(0,3)]);
imagestring($im, 10, 60, 5,  $string[5], $text_color[rand(0,3)]);*/
imagepng($im);
imagedestroy($im);<pre> <?php
error_reporting(E_ALL);
header("Content-type: image/png");
captcha();
function ran_string($length=10){
        $string="";
        $pattern = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        for($i=0; $i<$length; $i++){
            $string .= $pattern[rand(0,61)];
        }

return $string;
}
function captcha()
{
$string=ran_string();
$im = imagecreate(100, 50);
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color =array( imagecolorallocate($im, 255, 0, 0),imagecolorallocate($im, 0, 255, 0),imagecolorallocate($im, 0, 0, 255),imagecolorallocate($im, 255, 255, 255));
for($i=0;$i<count($string);$i++)
{
    $x=($i+1)*10;
imagestring($im, 10, $x, 5,  $string[$i], $text_color[rand(0,3)]);
}/*
imagestring($im, 10, 10, 5,  $string[0], $text_color[rand(0,3)]);
imagestring($im, 10, 20, 5,  $string[1], $text_color[rand(0,3)]);
imagestring($im, 10, 30, 5,  $string[2], $text_color[rand(0,3)]);
imagestring($im, 10, 40, 5,  $string[3], $text_color[rand(0,3)]);
imagestring($im, 10, 50, 5,  $string[4], $text_color[rand(0,3)]);
imagestring($im, 10, 60, 5,  $string[5], $text_color[rand(0,3)]);*/
imagepng($im);
imagedestroy($im);
}
?>w

}
?>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900