imagettftext()函数不工作?

问题描述 投票:1回答:3

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

<?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是::

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

所以最后我要做的......请帮助我......

php gd
3个回答
0
投票

尝试我的代码...创建一个PHP文件 - >把这个代码,然后运行你肯定有结果...快乐编码....

不要忘了添加“arial.ttf”文件,你运行你的php文件同一目录...

 <?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);
?>

0
投票

如果在对两者调用imagettftext()进行注释后它起作用,请确保文件arial.ttf存在于脚本的同一目录中。还注释掉对header()的调用,以便能够查看返回的任何错误消息。


0
投票

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

并给予该字体文件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);

© www.soinside.com 2019 - 2024. All rights reserved.