imagettftext()函数不工作?

时间:2016-02-13 10:53:43

标签: 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为::

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

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

  

试试我的代码...创建一个php文件 - &gt;把这个代码运行起来   结果肯定......快乐的编码......

不要忘记添加&#34; arial.ttf&#34;文件,你运行你的php文件相同的目录...

table-cells

答案 2 :(得分: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 =新的CaptchaSecurityImages($ width,$ height,$ characters);