PHP文本到图像

时间:2012-08-07 17:23:27

标签: php gdlib

我在PHP / GD中得到了很好的结果,将文本写入图像模板,文本包装正常,但不是"顺利",这是我正在使用的代码:

<?php

header("Content-type: image/png");

$text = "go to school go to school go to school go to school go to school go to school go to school go to school go to school go to school go to school ";
$arrText=explode("\n",wordwrap($text,60,"\n"));

$im = imagecreatefrompng("template.png");
$y = 15; //vertical position of text
foreach($arrText as $arr)
{
    $white = imagecolorallocate($im,0,0,0); //sets text color
    imagestring($im,5,15,$y,trim($arr),$white); //create the text string for image,added     trim() to remove unwanted chars
    $y = $y+20;

}
imagepng($im);
imagedestroy($im);
?>

2 个答案:

答案 0 :(得分:2)

请试试这个: -

记住:为font $ font ='arial.ttf'设置正确的路径;

    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 = 'go to school go to school go to school go to school go to school go to school go to school go to school go to school go to school go to school';

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

答案 1 :(得分:0)

尝试打开抗锯齿:

imageantialias($im, true);

这会让它看起来更好吗?

或者尝试使用truetype字体的imagettftext:

imagettftext ( $im, 15, 0, 15, $y, $white, 'fontfile.ttf', trim($arr));