如何在php中创建带有对齐文本的图像
我想创建一个包含文本的图像,其中调整了字母间距,以便文本在给定的空间内拉伸。我的目标是将文本拆分为最大x毫米的行,并对文本进行对齐(增加字母间距) 以下是电流输出的示例: 它应该是这样的: 这是我目前的代码:如何在php中创建带有对齐文本的图像,php,gd,Php,Gd,我想创建一个包含文本的图像,其中调整了字母间距,以便文本在给定的空间内拉伸。我的目标是将文本拆分为最大x毫米的行,并对文本进行对齐(增加字母间距) 以下是电流输出的示例: 它应该是这样的: 这是我目前的代码: function ImageStringWrap($image, $font, $x, $y, $text, $color, $maxwidth, $textcolor) { $fontwidth = ImageFontWidth($font); $fontheight
function ImageStringWrap($image, $font, $x, $y, $text, $color, $maxwidth, $textcolor)
{
$fontwidth = ImageFontWidth($font);
$fontheight = ImageFontHeight($font);
if ($maxwidth != NULL) {
$maxcharsperline = floor($maxwidth / $fontwidth);
$text = wordwrap($text, $maxcharsperline, "\n", 1);
}
$lines = explode("\n", $text);
while (list($numl, $line) = each($lines)) {
$fontfile = imageloadfont('app_global/ttf/verdana.ttf');
ImageString($image, 14, $x, $y, $line, $color);
#Imagettftext ($image, 14 , 0, $x , $y , $color , $fontfile , $line );
$y += $fontheight;
}
$hight = 20*count($lines);
imageline ($image , 10 , 5 , 290, 5 , $textcolor );
imageline ($image , 10 , $hight , 290, $hight , $textcolor );
}
header("Content-type: image/png");
$string = urldecode($_GET['text']);
$im = imagecreate(300, 150);
// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 0);
#imageline ($im , 0 , 10 , 200, 0 , $textcolor );
// Write the string at the top left
#imagestring($im, 11, 10, 10, $string, $textcolor);
ImageStringWrap($im, 11, 10, 10, $string, $textcolor, ImageSX($im), $textcolor);
如何使用php实现这一点
评论部分中提到的php.net网站的代码确实让我感觉非常接近,但是文本看起来很模糊:
如果你看一看,我认为有一个函数可以满足你的需要,谢谢。这似乎是可行的,尽管它有点慢,因为我在文本输入过程中动态创建它。更大的问题似乎是字体有点模糊。将更新问题。是的,我可以想象它会很慢-OP确实声明它是CPU密集型的…谢谢。你知道为什么文本如此模糊吗?我看不模糊-字体很轻。除此之外-抱歉,没有-您从php.net使用的代码与我无关,我根本没有研究过它。