使用imagecreatetruecolor创建图像时出现PHP错误

时间:2016-07-10 02:03:00

标签: php html image php-7

我有一个简单的 HTML ,它调用从PHP文件生成的图像,但图像没有显示。 HTML部分是:

<!DOCTYPE html>
<html>

<head>
</head>

<body>
    <img src="imgcaptcha.php"/>
</body>

</html>

imgcaptcha.php 是:

<?php
//error_reporting(E_ALL);
//header('Content-Type: image/png');

//Generates rando string
function geraStringAleatoria($length = 10) {
    $charSet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $stringAleatoria = '';
    for ($i = 0; $i < $length; $i++) {
        $stringAleatoria .= $charSet[rand(0, strlen($charSet) - 1)];
    }
    return $stringAleatoria;
}
//Generate the captcha image
function geraImagemCaptcha($text = 'good'){
    // Set the content-type
    $width  = 200;
    $height = 30;
    // Create the image
    $im = imagecreatetruecolor($width, $height) or die('display nao instalado');;

    // Create some colors
    $white  = imagecolorallocate($im, 255, 255, 255);
    $grey   = imagecolorallocate($im, 128, 128, 128);
    $black  = imagecolorallocate($im, 0, 0, 0);
    imagefilledrectangle($im, 0, 0, $width-1, $height-1, $white);

    //ADD NOISE - DRAW background squares
    $square_count = 6;
    for($i = 0; $i < $square_count; $i++){
        $cx = rand(0,$width);
        $cy = (int)rand(0, $width/2);
        $h  = $cy + (int)rand(0, $height/5);
        $w  = $cx + (int)rand($width/3, $width);
        imagefilledrectangle($im, $cx, $cy, $w, $h, $white);
    }

    //ADD NOISE - DRAW ELLIPSES
    $ellipse_count = 5;
    for ($i = 0; $i < $ellipse_count; $i++) {
      $cx = (int)rand(-1*($width/2), $width + ($width/2));
      $cy = (int)rand(-1*($height/2), $height + ($height/2));
      $h  = (int)rand($height/2, 2*$height);
      $w  = (int)rand($width/2, 2*$width);
      imageellipse($im, $cx, $cy, $w, $h, $grey);
    }

    // Replace path by your own font path
    $font = 'flushout.ttf';

    // Add some shadow to the text
    imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

    // Add the text
    imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

    // Using imagepng() results in clearer text compared with imagejpeg()
    imagepng($im);

    imagedestroy($im);

}
geraImagemCaptcha(geraStringAleatoria());
?>

我正在使用 PHP 7
尝试已发表评论header();并添加error_reporting(E_ALL);,但未显示任何错误消息。根据{{​​3}}尝试了许多其他不同的font.ttf文件,也没有工作,所以我想问题是在代码的另一部分而不是字体,因为我尝试了很多网站,也有一些我从安装的其他程序中获得的其他.ttf文件。我测试的所有浏览器都出现了这个问题。代码中可能缺少什么?顺便说一下,我从this生成了验证码图像。

0 个答案:

没有答案