由于错误,无法显示PHP Captcha Image

时间:2017-02-13 07:42:17

标签: php captcha

这是我在课堂上的静态功能' image_utils.php'

public static function getCaptcha()
{
     try {
        $width = 200;
        $height = 30;
        $font = 'fonts/arial.ttf';
        //
        if (!file_exists($font)) {
            throw new Exception('Error Font not found');
        }
        //
        $image = imagecreatetruecolor($width, $height);
        $white = imagecolorallocate($image, 255, 255, 255);
        $grey = imagecolorallocate($image, 180, 180, 180);
        $black = imagecolorallocate($image, 0, 0, 0);
        //
        for ($sq = 1; $sq <= 10; $sq++) {
            $cx = (int)rand(0, $width / 2);
            $cy = (int)rand(0, $height);
            $h = $cy + (int)rand(0, $height / 5);
            $w = $cx + (int)rand($width / 3, $width);
            if (!imagefilledrectangle($image, $cx, $cy, $w, $h, $white)) {
                throw new Exception('Error in White Fill');
            }
        }
        //
        $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);
            if (!imageellipse($image, $cx, $cy, $w, $h, $grey)) {
                throw new Exception('Error in Grey Fill');
            }
        }
        //
        imagefttext($image, 20, 0, 10, 20, $black, $font, 'Captcha');
        header('Content-type: image/png');
        if (!imagepng($image)) {
            throw new Exception('Error in ImagePng');
        }
    } catch (Exception $ex) {
        echo $ex->getMessage();
    }
}

在&#39; log.php&#39;中调用它在img标签:

<img src='<?php image_utils::getCaptcha() ?>'/>

出现错误&#39;图片无法显示,因为它包含错误&#39;。我已经创建了一个字体文件检查,如代码所示,因此包含了字体文件。请稍微说清楚。

先谢谢。

0 个答案:

没有答案