php manule,如何在PHP中使用Imagick將文本包裝成多行文本?

-1

Hi i find some sollution thanks for BMinner for his code i edit his code and get good working sollution

你好,我找到了一些總結感謝BMinner的代碼,我編輯了他的代碼,得到了很好的工作總結

USAGE

使用

$w = 210;

$h = 520;

$canvas = new Imagick();

$canvas->newImage($w,$h,new ImagickPixel('green'),'png');

$draw = new ImagickDraw();

$draw->setFontSize(25);

$text="SomeTextWithoutSpacesAndGoingOn..xxxxx
some short words with spaces
and some text
with manuel page
break
and also multiple spaces spaces end. also w i t o n e c ha ra c ter";

list($lines, $lineHeight)= wordWrapAnnotation($canvas, $draw, $text, $w-20);

$canvas->annotateImage($draw, 10, $lineHeight , 0, $lines);

header("Content-Type: image/png");

echo $canvas;

?>

FUNCTIONS REFERANCED FROM BMinner

從BMinner函數參考

//this is unicode split method for out of english latin characters

function str_split_unicode($str, $l = 0) {

if ($l > 0) {

$ret = array();

$len = mb_strlen($str, "UTF-8");

for ($i = 0; $i < $len; $i += $l) {

$ret[] = mb_substr($str, $i, $l, "UTF-8");

}

return $ret;

}

return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);

}

//this is my function detects long words and split them

function check_long_words($image,$draw,$text,$maxWidth) {

$metrics = $image->queryFontMetrics($draw, $text);

if($metrics['textWidth'] <= $maxWidth)

return array($text);

$words = str_split_unicode($text);

$i = 0;

while($i < count($words) )

{

$currentLine = $words[$i];

if($i+1 >= count($words))

{

$lines[] = $currentLine;

//$lines = $lines + $checked;

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

$t++;

}

//We can't add the next word to this line, so loop to the next line

$lines[] = $currentLine;

$i++;

}

return $lines;

}

//this is BMiner code some fixes for manule breaks

function wordWrapAnnotation(&$image, &$draw, $text, $maxWidth)

{

$brler = explode("
", $text);

$lines = array();

foreach($brler as $br)

{

$i = 0;

$words = explode(" ", $br);

while($i < count($words) )

{

$currentLine = $words[$i];

$metrics = $image->queryFontMetrics($draw, $currentLine . ' ' . $words[$i+1]);

if($i+1 >= count($words))

{

$checked=check_long_words($image,$draw,$currentLine,$maxWidth);

$lines = array_merge($lines, $checked);

if($metrics['textHeight'] > $lineHeight)

$lineHeight = $metrics['textHeight'];

//$lines = $lines + $checked;

break;

}

//Check to see if we can add another word to this line

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

$t++;

}

//We can't add the next word to this line, so loop to the next line

$checked=check_long_words($image,$draw,$currentLine,$maxWidth);

$lines = array_merge($lines, $checked);

$i++;

//Finally, update line height

if($metrics['textHeight'] > $lineHeight)

$lineHeight = $metrics['textHeight'];

}

}

return array(join("\n",$lines), $lineHeight);

}

?>

AND OUTPUT

和輸出

aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS8yOU5YZy5wbmc=

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值