0

我制作了一个脚本,它从字符串中呈现图像。除了不显示撇号这一事实之外,这很好。空格是撇号应该在的位置。

有任何想法吗?

这是渲染代码:(字符串只是普通文本。就像报纸文章一样)

$text = $_SESSION['article'];

$arrText=explode("\n",wordwrap($text,69,"\n"));//change number 75 here to check the wordwrap

$im = @imagecreate(650,2500); //creates an image (width,height)
$background_color = imagecolorallocate($im, 0, 0, 0); //sets image background color
$y=5; //vertical position of text

foreach($arrText as $arr)
{
  $white=imagecolorallocate($im,255,255,255); //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+15;

}

imageantialias($im, true);
imagepng($im);
imagedestroy($im);
?>
4

1 回答 1

0

我对您的代码进行了一些修改:

  • 将高度降低到 500px

  • 删除了我的 gd 版本中似乎缺少的 imageantialias

所以 :

<?php

$text = "this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test";

$arrText = explode("\n", wordwrap($text, 69, "\n")); //change number 75 here to check the wordwrap

$im = @imagecreate(650, 500); //creates an image (width,height)
$background_color = imagecolorallocate($im, 0, 0, 0); //sets image background color
$y = 5; //vertical position of text

foreach ($arrText as $arr)
{
    $white = imagecolorallocate($im, 255, 255, 255); //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 + 15;
}

header("Content-type: image/png");
imagepng($im);
imagedestroy($im);

它给了我:

在此处输入图像描述

所以我有'我自己。有可能在您的系统中,或者在您的 gd 版本中,或某某中,imagestring函数不支持撇号。

您是否尝试使用imagettftext而不是imagestring?除了给您撇号之外,您还可以非常密切地自定义您的字体。

您的代码变为:

$text = "this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test this'is'a'test";

$arrText = explode("\n", wordwrap($text, 69, "\n")); //change number 75 here to check the wordwrap

$im = @imagecreate(650, 500); //creates an image (width,height)
$background_color = imagecolorallocate($im, 0, 0, 0); //sets image background color
$y = 5; //vertical position of text

foreach ($arrText as $key => $arr)
{
    $white = imagecolorallocate($im, 255, 255, 255); //sets text color
    //imagestring($im, 5, 15, $y, trim($arr), $white); //create the text string for image,added trim() to remove unwanted chars
    putenv('GDFONTPATH=' . realpath('.'));
    imagettftext($im, 16, 0, 15, 16 + $y, $white, 'font.ttf', trim($arr));
    $y = $y + 16 + 4; // size of font + newline space
}

header("Content-type: image/png");
imagepng($im);
imagedestroy($im);

预览(使用字体):

在此处输入图像描述

于 2012-10-15T18:24:34.117 回答