2014-04-01 67 views
0

以下是代碼工作正常,但我想添加自己的背景img而不是藍色。還想在代碼中添加字母?請幫助...如何插入自己的bg到驗證碼。?

<?php 
session_start(); 
$code=rand(10000,99999); 
$_SESSION["code"]=$code ; 
$im = imagecreatetruecolor(60, 24); 
$bg = imagecolorallocate($im, 22, 86, 165); 
$fg = imagecolorallocate($im, 255, 255, 255); 
imagefill($im, 0, 0, $bg); 
imagestring($im, 5, 5, 5, $code, $fg); 
header("Cache-Control: no-cache, must-revalidate"); 
header('Content-type: image/png'); 
imagepng($im); 
imagedestroy($im); 
?> 

回答

0

使用CAPTCHA的想法是告訴計算機和人類分開。這個驗證碼很容易被OCR程序破解。

說了這麼多,你需要做到以下幾點:

<?php 
session_start(); 
$code=rand(10000,99999); 
$_SESSION["code"]=$code ; 
$im = imagecreatefrompng("bg.png"); 
$fg = imagecolorallocate($im, 255, 255, 255); 
imagestring($im, 5, 5, 5, $code, $fg); 
header("Cache-Control: no-cache, must-revalidate"); 
header('Content-type: image/png'); 
imagepng($im); 
imagedestroy($im); 
?> 
+0

作爲使用字母數字驗證碼,你可以參考[這裏](http://stackoverflow.com/a/4356295/3461549) – msound

+0

謝謝,這工作正常。最後一件事是如何添加字母並增加字體大小。 – Target

+0

而不是做$ code = rand(10000,99999),你可以做$ code = generateRandomString(5)。有關generateRandomeString()的實現,請參閱[本討論](http://stackoverflow.com/a/4356295/3461549)。爲了增加字體大小,5是最大的字體,你已經在使用它了。所以,你需要考慮加載你自己的字體。你可以參考函數[imageloadfont](http://us3.php.net/manual/en/function.imageloadfont.php) – msound