2015-12-24 48 views
-1

嗯,首先我需要告诉你我对此很乐观。 我找到了一个生成验证码的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); 
    ?> 

谢谢!

+0

对不起,我正在编辑我从互联网下载的原始php文件(而不是我的项目中的文件)。我的问题现在可以关闭。谢谢 ! –

回答

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