Captcha的自定义颜色

时间:2015-12-24 16:57:30

标签: php web captcha

好吧,首先我需要告诉你我是这样的菜鸟。 我发现了一个生成验证码的php文件。 它工作但背景总是橙色,文字是黑色的。 示例验证码:“3ab6de”。我想要做的就是将背景颜色更改为另一种颜色,并使文本具有不同的颜色(3ab为红色,6de为蓝色)。 我试过但失败请帮帮我们! 这是代码:

    <?php
    session_start();
    $random_alpha = md5(rand());
    $captcha_code = substr($random_alpha, 0, 6);
    $_SESSION["captcha_code"] = $captcha_code;
    $target_layer = imagecreatetruecolor(70,30);
    $captcha_background = imagecolorallocate($target_layer, 255, 160, 119);
    imagefill($target_layer,0,0,$captcha_background);
    $captcha_text_color = imagecolorallocate($target_layer, 0, 0, 0);
    imagestring($target_layer, 5, 5, 5, $captcha_code, $captcha_text_color);
    header("Content-type: image/jpeg");
    imagejpeg($target_layer);
    ?>

谢谢!

2 个答案:

答案 0 :(得分:0)

我认为你应该将验证码字符串分成两个区别,有些像这样。

<?php
session_start();
$random_alpha = md5(rand());
$captcha_code = substr($random_alpha, 0, 6);
$_SESSION["captcha_code"] = $captcha_code;
$leng1 = count($captcha_code)/2;


$captcha_code1 = substr($captcha_code, $leng1);
$captcha_code2 = substr($captcha_code,- count($captcha_code) + $leng1);



$target_layer = imagecreatetruecolor(70,30);
$captcha_background = imagecolorallocate($target_layer, 255, 160, 119);
imagefill($target_layer,0,0,$captcha_background);
$captcha_text_color1 = imagecolorallocate($target_layer, 0, 0, 0);
$captcha_text_color2 = imagecolorallocate($target_layer, 0, 0, 0);

imagestring($target_layer, 5, 5, 5, $captcha_code1, $captcha_text_color);
imagestring($target_layer, 5, 5, 5, $captcha_code2, $captcha_text_color2);


header("Content-type: image/jpeg");
imagejpeg($target_layer);
?>

答案 1 :(得分:0)

您可以尝试使用captcha.com库,https://captcha.com/captcha-examples.html

有几个例子

我希望这对你有所帮助。