我正在尝试创建一个验证码,它从我命名为dictionary.txt的文件中获取单词,并使用GD库将它们转换为验证码图像。但有些事情是错的。我可以将我的字典短语显示为会话变量,但它们不会生成图像。所以它必须是GD Library代码。有一个名为captcha_validate.php的最终目标文件,但现在并不重要。我只是想让图像发挥作用。
form_captcha.php
<?php
session_start();
session_regenerate_id();
$file = file('dictionary.txt', FILE_IGNORE_NEW_LINES);
$length = count($file) - 1;
$random = rand(0, $length);
$_SESSION['captcha'] = $file[$random];
session_write_close();
function msg($msg) {
$container = imagecreate(250, 170);
$black = imagecolorallocate($container, 0, 0, 0);
$white = imagecolorallocate($container, 255, 255, 255);
$font = "C:\Windows\Fonts\arial.ttf";
imagefilledrectangle($container, 0, 0, 250, 170, $black);
$px = (imagesx($container) / (strlen($msg)/1.15));
$py = (imagesy($container) / 3.5);
imagefttext($container, 28, -27, $px, $py, $white, $font, $msg);
header("Content-type: image/png");
imagepng($container, null);
imagedestroy($container);
}
msg($file[$random]);
?>
captcha.php
function addDigits(source) {
return source
/* convert number to string and split characters to array */
.toString().split('')
/* convert char[] to number[] */
.map(function (elm) { return Number(elm); })
/*
accumilate all numbers in array to value, which is initally zero.
REF: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce#Examples
*/
.reduce(function (result, current) { return result + current; }, 0)
}
function validate(source) {
if (typeof(source) == "undefined" || source == null) { throw 'value can not ne NULL ...'; }
if ((typeof(source) !== 'number')) { throw 'value has to of NUMBER type ....'; }
if ((source % 1)) { throw 'value has to be an INTEGER ....'; }
if (!(source >= 10 && source <= 99)) { throw 'value is out of range 10-99 ....'; }
}
/* TESTING ......*/
var samples = [null, 'sdsad', 9.9, 9, 10, 99, 100];
samples.forEach(function (elm) {
try {
validate(elm);
console.log(elm + ': ' + addDigits(elm));
} catch (exp) {
console.log(elm + ': ' + exp);
}
});
答案 0 :(得分:0)
尝试测试字体路径是否设置正确。我确实测试了你的脚本,当我为我的环境设置正确的字体路径时它就起作用了。
喜欢:
$ font ='./SomeFont.ttf';
答案 1 :(得分:0)
好的,事实证明我在captcha.php文件中使用了html,在这种情况下我不应该使用它。这是我的代码不起作用的主要原因。感谢您的努力,任何评论或回答的人。