php - imagettftext() 函数不起作用?

标签 php gd

我输入此代码以显示文本转换为图像。

<?php
// Set the content-type
header('Content-Type:image/png');

// Create the image
$im = imagecreatetruecolor(400, 30);

// 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, 399, 29, $white);

// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font="arial.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);
?>

之后的输出是::

Output in mozilla

我在 Windows 中使用 WAMP,并且还安装了 GD...我的 gd_info() :

array (size=12)
'GD Version' => string 'bundled (2.1.0 compatible)' (length=26)
'FreeType Support' => boolean true
'FreeType Linkage' => string 'with freetype' (length=13)
'T1Lib Support' => boolean false
'GIF Read Support' => boolean true
'GIF Create Support' => boolean true
'JPEG Support' => boolean true
'PNG Support' => boolean true
'WBMP Support' => boolean true
'XPM Support' => boolean true
'XBM Support' => boolean true
'JIS-mapped Japanese Font Support' => boolean false

所以最后我必须做什么......请帮助我......

最佳答案

您需要指定字体文件的完整路径。

并给予字体文件755权限

class CaptchaSecurityImages {

    var $font = 'monofont.ttf';

    function getFontPath(){
        return $_SERVER['DOCUMENT_ROOT'].'/captcha/monofont.ttf';
    }

    function generateCode($characters) {
        /* list all possible characters, similar looking characters and vowels have been removed */
        $possible = '012345678';
        $code = '';
        $i = 0;
        while ($i < $characters) { 
            $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
            $i++;
        }
        return $code;
    }

    function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
        $code = $this->generateCode($characters);
        /* font size will be 75% of the image height */
        $font_size = $height * 0.75;
        $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
        /* set the colours */
        $background_color = imagecolorallocate($image, 255, 255, 255);
        $text_color = imagecolorallocate($image, 20, 40, 100);
        $noise_color = imagecolorallocate($image, 100, 120, 180);
        /* generate random dots in background */
        for( $i=0; $i<($width*$height)/3; $i++ ) {
            imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
        }
        /* generate random lines in background */
        for( $i=0; $i<($width*$height)/150; $i++ ) {
            imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
        }
        /* create textbox and add text */
        $textbox = imagettfbbox($font_size, 0, $this->getFontPath(), $code) or die('Error in imagettfbbox function');
        $x = ($width - $textbox[4])/2;
        $y = ($height - $textbox[5])/2;
        imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->getFontPath() , $code) or die('Error in imagettftext function');
        /* output captcha image to browser */
        header('Content-Type: image/jpeg');
        imagejpeg($image);
        imagedestroy($image);
        $_SESSION['security_code'] = $code;
    }

}

$width = isset($_GET['width']) ? $_GET['width'] : '120';
$height = isset($_GET['height']) ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';

$captcha = new CaptchaSecurityImages($width,$height,$characters);

关于php - imagettftext() 函数不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35378971/

相关文章:

php - 如何在CakePHP 3.x中验证URL?

php - 我可以为 MPDF 的最后一页设置特定的页脚吗?

php - 如何通过PHP从字符串中获取子字符串?

php - mySql 之前和之后

php - 如何在 php 中创建带有自定义文本的图像?

PHP 和单例

php - GD/Imagick 不保存我的图像

php - GD vs ImageMagick vs Gmagick for jpg?

php图像箭头线需要平滑

php - PHP 中的 imagecopyresampled,有人可以解释一下吗?