我有几行代码来生成一个普通的图像与一些给定的文字在PHP中。但是,当我给图像文字的宽度超出图像。如何将图像内的文字与固定宽度但动态高度对齐?我的代码如下。如何在PHP中自动将文本与图像对齐?
header ("Content-type: image/png");
$string = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s';
$font = 4;
$width = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);
$im = @imagecreate (200,500);
$background_color = imagecolorallocate ($im, 255, 255, 255); //white background
$text_color = imagecolorallocate ($im, 0, 0,0);//black text
imagestring ($im, $font, 0, 0, $string, $text_color);
imagepng ($im);
我希望这个图像根据给定的段落自动调整文本。怎么做?
我有这么多的麻烦做的是,在其他项目由于字母间距和每个字母的宽度,你不能以可接受的方式做到这一点。用PHP创建图像,你可以做的是用文本创建一个简单的html文件,然后使用phantomjs截图并在该文件夹中创建图像。这将是最简单的方式来相信我。 phantomjs非常简单,可以在几秒钟内完成你所要求的任何事情。这是你需要的:http://phantomjs.org/screen-capture.html – artuc 2014-10-08 12:54:59