随机颜色验证码生成器php-gd库 [英] Random Color Captcha generator php-gd Library

查看:119
本文介绍了随机颜色验证码生成器php-gd库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,这是我的第一篇文章,因此,如果我忘记了某些内容或不遵循某些标准,请原谅.清除后,我看到了在某处生成 captcha 的代码,并对其进行了修改,以使每个字符得到随机颜色以及其他一些小的修正,这是代码:

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
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);
}
?>


但是上面的输出只是一个字符,如果我删除"for循环"并取消注释了注释部分,便得到了想要的结果,请有人帮忙,如果这是一个 n00b问题,对不起> !!!
编辑 :
这对我来说非常简单和愚蠢,我使用了count($ string),无论如何我应该使用strlen(),正确的版本是这样的:


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:

$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)]);
}

推荐答案

length = 10){
length=10){


string = " ;
string="";


模式 = " ; for(
pattern = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; for(


这篇关于随机颜色验证码生成器php-gd库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆