How can I wrap text using Imagick in PHP so that it is drawn as multiline text?(如何在 PHP 中使用 Imagick 换行文本,以便将其绘制为多行文本?)
本文介绍了如何在 PHP 中使用 Imagick 换行文本,以便将其绘制为多行文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
PHP 中的 Imagick 库允许您在图像上绘制文本.
The Imagick library in PHP allows you to draw text on top of an image.
我怎样才能告诉 Imagick 根据一些有界文本框来换行文本,以便单词显示为多行文本而不是单行?
How can I tell Imagick to wrap the text based upon some bounded text box, so that the words appear as multiline text rather than a single line?
推荐答案
用法:
list($lines, $lineHeight) = wordWrapAnnotation($image, $draw, $msg, 140);
for($i = 0; $i < count($lines); $i++)
$image->annotateImage($draw, $xpos, $ypos + $i*$lineHeight, 0, $lines[$i]);
功能:
/* Implement word wrapping... Ughhh... why is this NOT done for me!!!
OK... I know the algorithm sucks at efficiency, but it's for short messages, okay?
Make sure to set the font on the ImagickDraw Object first!
@param image the Imagick Image Object
@param draw the ImagickDraw Object
@param text the text you want to wrap
@param maxWidth the maximum width in pixels for your wrapped "virtual" text box
@return an array of lines and line heights
*/
function wordWrapAnnotation(&$image, &$draw, $text, $maxWidth)
{
$words = explode(" ", $text);
$lines = array();
$i = 0;
$lineHeight = 0;
while($i < count($words) )
{
$currentLine = $words[$i];
if($i+1 >= count($words))
{
$lines[] = $currentLine;
break;
}
//Check to see if we can add another word to this line
$metrics = $image->queryFontMetrics($draw, $currentLine . ' ' . $words[$i+1]);
while($metrics['textWidth'] <= $maxWidth)
{
//If so, do it and keep doing it!
$currentLine .= ' ' . $words[++$i];
if($i+1 >= count($words))
break;
$metrics = $image->queryFontMetrics($draw, $currentLine . ' ' . $words[$i+1]);
}
//We can't add the next word to this line, so loop to the next line
$lines[] = $currentLine;
$i++;
//Finally, update line height
if($metrics['textHeight'] > $lineHeight)
$lineHeight = $metrics['textHeight'];
}
return array($lines, $lineHeight);
}
这篇关于如何在 PHP 中使用 Imagick 换行文本,以便将其绘制为多行文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何在 PHP 中使用 Imagick 换行文本,以便将其绘制


基础教程推荐
猜你喜欢
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01