GD rendered image not displaying apostrophes

别说谁变了你拦得住时间么 提交于 2019-12-13 04:43:07

问题


I have made a script which renders an image from a string. It is fine apart from the fact that apostrophes do not show. A space is where the apostrophe should be.

Any ideas?

Here is the rendering code: (the string is just normal text. Like a newspaper article)

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

回答1:


I tried your code with some modifications :

  • reduced height to 500px

  • removed imageantialias that seems to be missing in my gd version

So :

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

It gave me :

So I have the ' myself. It is possible that in your system, or in your gd version, or so and so, imagestring function does not support the apostrophe.

Did you tried to use imagettftext instead of imagestring ? More than giving you apostrophes, you'll be able to customise your fonts very closely.

Your code becomes :

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

Preview (with this font) :



来源:https://stackoverflow.com/questions/12892204/gd-rendered-image-not-displaying-apostrophes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!