【发布时间】:2011-12-21 19:08:34
【问题描述】:
我正在尝试使用 PHP 创建最大透明文本框的图像。 2行文字。框宽固定为90px;高度取决于包含的文本(同样,文本可能占据 1 到 2 行):example。
我认为最具挑战性的部分是如何根据文本长度“自动”设置框高度。也就是说,脚本应该足够智能:
- 如果文本长于宽度,则换行(到第二行)。
- 调整框高度,因为现在有两行。
假设文本总是适合一行和框,脚本可以相当简单:
<?php
$im = imagecreatetruecolor(90, 22);
$white = imagecolorallocate($im, 255, 255, 255);
$blue = imagecolorallocate($im, 92, 149, 167);
// Set the background to be blue
imagefilledrectangle($im, 0, 0, 90, 22, $blue);
// Path to our font file
$font = './Arial.ttf';
imagefttext($im, 10, 0, 5, 15, $white, $font, "Barton Hotel");
// Output to browser
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
【问题讨论】:
标签: php image image-processing