GD渲染的图像不显示撇号 [英] GD rendered image not displaying apostrophes

查看:66
本文介绍了GD渲染的图像不显示撇号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个脚本,可以从字符串中渲染图像.除了没有撇号的事实之外,这还不错.一个空格就是撇号所在的位置.

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.

有什么想法吗?

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

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

推荐答案

我尝试了一些修改后的代码:

I tried your code with some modifications :

  • 将高度降低到500px

  • reduced height to 500px

删除了我的gd版本中似乎缺少的图像抗锯齿

removed imageantialias that seems to be missing in my gd version

所以:

<?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 函数可能不支持撇号.

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.

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

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

您的代码变为:

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

预览(带有字体):

这篇关于GD渲染的图像不显示撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆