简单的验证码 php 脚本不会在图像上显示文本



我希望在任何地方都找不到解决方案的情况下与某人取得联系。

我正在尝试使用 php 在我的网站上创建一个验证码,尽管我能够创建一个图像并创建随机验证码文本。

我无法覆盖两者。这是我的代码:

<?PHP
session_start();

function generateRandomString($length = 10) {
    $letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    $len = strlen($letters);
    $letter = $letters[rand(0, $len - 1)];
    $text_color = imagecolorallocate($image, 0, 0, 0);
    $word = "";
    for ($i = 0; $i < 6; $i++) {
        $letter = $letters[rand(0, $len - 1)];
        imagestring($image, 7, 5 + ($i * 30), 20, $letter, $text_color);
        $word .= $letter;
    }
    $_SESSION['captcha_string'] = $word;
}

function security_image(){
   // $code = isset($_SESSION['captcha']) ? $_SESSION['captcha'] : generate_code();
    //$font = 'content/fonts/comic.ttf';
    $width = '110';
    $height = '20';
    $font_size = $height * 0.75;
   // $image = @imagecreate($width, $height) or die('GD not installed');
               global $image;
    $image = imagecreatetruecolor($width, $height) or die("Cannot Initialize new GD image stream");
               $background_color = imagecolorallocate($image, 255, 255, 255);  
               imagefilledrectangle($image,0,0,200,50,$background_color);

               $line_color = imagecolorallocate($image, 64,64,64); 
               for($i=0;$i<10;$i++) {
    imageline($image,0,rand()%50,200,rand()%50,$line_color);
               }
               $pixel_color = imagecolorallocate($image, 0,0,255);
               for($i=0;$i<1000;$i++) {
                              imagesetpixel($image,rand()%200,rand()%50,$pixel_color);
               }
               //$textbox = imagettfbbox($font_size, 0, $font, $code);
               //$textbox = imagettfbbox($font_size, 0, $randomString);
    $x = ($width - $textbox[4]) / 2;
    $y = ($height - $textbox[5]) / 2;
   // imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
   imagettftext($image, $font_size, 0, $x, $y, $text_color , $word);

    $images = glob("*.png");
    foreach ($images as $image_to_delete) {
        @unlink($image_to_delete);
    }
    imagepng($image, "image" . $_SESSION['count'] . ".png");
    header('Content-Type: image/png');
    imagepng($image);
    imagedestroy($image);
}
security_image();


?>

我不知道我做错了什么。我在这个"显示文本"问题上花了 10 多个小时。我不明白,我迫切需要帮助。我什至从其他资源下载了有效的验证码版本,一旦我将其上传到我的服务器,这些资源就会中断。我不知道发生了什么。起初我认为我的服务器有问题,但我甚至可以创建像素、线条图像这一事实意味着它至少可以工作。

请帮忙!!! 

更新---------------------------------------------感谢您的建议。这是编辑后的代码。我仍然遇到同样的问题。

<?PHP
session_start();

function security_image(){
    global $image;
   // $code = isset($_SESSION['captcha']) ? $_SESSION['captcha'] : generate_code();
    $font = 'content/fonts/comic.ttf';
    $width = '110';
    $height = '20';
    $font_size = $height * 0.75;
    $image = imagecreatetruecolor($width, $height) or die("Cannot Initialize new GD image stream");
    $background_color = imagecolorallocate($image, 255, 255, 255);  
    imagefilledrectangle($image,0,0,200,50,$background_color);

    $line_color = imagecolorallocate($image, 64,64,64); 
    for($i=0;$i<10;$i++) {
    imageline($image,0,rand()%50,200,rand()%50,$line_color);
    }
    $pixel_color = imagecolorallocate($image, 0,0,255);
    for($i=0;$i<1000;$i++) {
        imagesetpixel($image,rand()%200,rand()%50,$pixel_color);
    }
    $letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    $len = strlen($letters);
    $letter = $letters[rand(0, $len - 1)];
    $text_color = imagecolorallocate($image, 0, 0, 0);
    $word = "";
    for ($i = 0; $i < 6; $i++) {
        $letter = $letters[rand(0, $len - 1)];
        imagestring($image, 7, 5 + ($i * 30), 20, $letter, $text_color);
        $word .= $letter;
    }
    $_SESSION['captcha_string'] = $word;
    /*texbox unitinitialized (removed for the sake of just showing the word size doesnt matter)
        $x = ($width - $textbox[4]) / 2;
        $y = ($height - $textbox[5]) / 2;
    */
    $x = ($width) / 2;
    $y = ($height) / 2;
    imagettftext($image,$font, 4, $x, $y, $word);

    header('Content-Type: image/png');
    imagepng($image);
    imagedestroy($image);
}
security_image();?>

我做了一些建议的更改,但代码似乎仍然做同样的事情。只是按预期显示线条和像素,但文本仍然丢失... ?

您的函数中有一些"错误",让我们修复它们:

In generateRandomString()

  • generateRandomString($length = 10)

$lenght 不会使用其范围。

  • $text_color = imagecolorallocate($image, 0, 0, 0);

$image未初始化


security_image() 函数中:

  • $textbox未初始化

  • $text_color$word未初始化。

  • Wrong parameter count for imagettftext() 您添加了 7 个参数,忘记了font file参数。

发现了问题。 使用这个:http://php.net/manual/en/function.imagettftext.php

我能够看到字体位置不正确。 使用该页面上的示例并根据我的需要进行编辑。

最新更新