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 に答える