PHP中的Capctha创建代码不起作用

PHP中的Capctha创建代码不起作用,php,captcha,Php,Captcha,我正在尝试用php制作验证码。这是我的代码: $amountChars = 5; $randString = substr(md5(uniqid()), 0, $amountChars); $img = imagecreatfromjpeg("noise.jpg"); imageantialias($img, true); $x = 20; $y = 35; $deltaX = 40; for($i = 0; $i < $amountChars; $i++){ $size =

我正在尝试用php制作验证码。这是我的代码:

$amountChars = 5;
$randString = substr(md5(uniqid()), 0, $amountChars);
$img = imagecreatfromjpeg("noise.jpg");
imageantialias($img, true);

$x = 20;
$y = 35;
$deltaX = 40;

for($i = 0; $i < $amountChars; $i++){
    $size = rand(18, 28);
    $r = rand(0,152);
    $g = rand(0,152);
    $b = rand(0,152);
    $color = imagecolorallocate($img, $r, $g, $b);
    $angle = -30 + rand(0, 60);
    imagettftext($img, $size, $angle, $x, $y, $color, "Blokletters-Balpen.ttf", $randString{$i});
    $x += $deltaX;
}

header("Content-Type: image/png");
imagePNG($img);
$amountChars=5;
$randString=substr(md5(uniqid()),0,$amountChars);
$img=imagecreatfromjpeg(“noise.jpg”);
imageantialias($img,true);
$x=20;
$y=35;
$deltaX=40;
对于($i=0;$i<$amountChars;$i++){
$size=兰特(18,28);
$r=兰特(0152);
$g=兰特(0152);
$b=兰特(0152);
$color=imagecolorallocate($img,$r,$g,$b);
$angle=-30+rand(0,60);
imagettftext($img、$size、$angle、$x、$y、$color,“Blokletters-Balpen.ttf”,$randString{$i});
$x+=$deltaX;
}
标题(“内容类型:图像/png”);
图像PNG($img);

问题是,它不起作用。我花了一个多小时在寻找一个错误,但没有成功。

我因为没有给出真正的答案而被激怒,所以再次尝试。。。通过一步一步地浏览您的代码,我能够通过以下修改使其运行:

$amountChars = 5;
$randString = substr(md5(uniqid()), 0, $amountChars);
// TYPO: MISSING E
$img = imagecreatefromjpeg("noise.jpg");

// IT'S POSSIBLE THIS FUNCTION ISN'T DEFINED:
// SEE REFERENCE LINK BELOW CODE
// imageantialias($img, true);

$x = 20;
$y = 35;
$deltaX = 40;

for($i = 0; $i < $amountChars; $i++) {
    $size = rand(18, 28);
    $r = rand(0,152);
    $g = rand(0,152);
    $b = rand(0,152);
    $color = imagecolorallocate($img, $r, $g, $b);
    $angle = -30 + rand(0, 60);
    // ADDED ./ TO FONT PATH, CHANGED $randString{$i} TO $randString[$i]
    imagettftext($img, $size, $angle, $x, $y, $color, "./Blokletters-Balpen.ttf", $randString[$i]);
    // WHY IS THIS HERE?
    $x += $deltaX;
}
header("Content-Type: image/png");
imagePNG($img);
$amountChars=5;
$randString=substr(md5(uniqid()),0,$amountChars);
//打字错误:缺少E
$img=imagecreatefromjpeg(“noise.jpg”);
//可能未定义此函数:
//请参阅下面的参考链接代码
//imageantialias($img,true);
$x=20;
$y=35;
$deltaX=40;
对于($i=0;$i<$amountChars;$i++){
$size=兰特(18,28);
$r=兰特(0152);
$g=兰特(0152);
$b=兰特(0152);
$color=imagecolorallocate($img,$r,$g,$b);
$angle=-30+rand(0,60);
//添加./到字体路径,将$randString{$i}更改为$randString[$i]
imagettftext($img、$size、$angle、$x、$y、$color,“./Blokletters-Balpen.ttf“,$randString[$i]);
//这是为什么?
$x+=$deltaX;
}
标题(“内容类型:图像/png”);
图像PNG($img);
imageantialias
功能。我的上没有,所以我只是把它注释掉了


这些更改至少可以让代码正常工作。如果你想找一个更深入的例子,我建议你回顾一下。

我拿了你的代码,创建了一个
noise.jpg
图像,甚至从这里下载来看看有什么进展。快速查看您的代码会发现输入错误:

$img = imagecreatfromjpeg("noise.jpg");
难道不是吗?注意原始代码中缺少的
e

$img = imagecreatefromjpeg("noise.jpg");

我想现在应该可以用了吧?除非您将其放入

中时遇到问题,否则请解释什么不起作用。(一第纳尔是多少)图像需要在自己的页面上渲染。为什么要混合PNG和JPEG?(imagecreatefromjpeg和更高版本的imagePNG)您确定这是允许的吗?如果没有,您是否尝试过将基础图像转换为PNG并使用imagecreatefrompng?占位符\u in_use I have。。。nl-x图像不显示。
image\u capcha.php
是否将输出渲染到文件中?这是你应该首先尝试的。如果可以,那么再次检查PHP代码,看看如何将其直接呈现到image
SRC
标记中。你不应该添加新答案,而应该用它编辑你的原始答案。当我发表评论时,它仍然处于运行状态。道歉。