2015-12-24 27 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); 
?>