ImageMagick:如何在图像上绘制两个不同大小的字符串? [英] ImageMagick: how to draw two strings with different size over image?

查看:120
本文介绍了ImageMagick:如何在图像上绘制两个不同大小的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在其中绘制一个带有两个字符串的矩形。我希望第一个字符串是15pt大小(它是一个数字),第二个是10pt大小(标签)。我可以很容易地将一个尺寸的单个字符串绘制到矩形中,我这样做:

I want to draw a rectangle with two strings in it. I want the first string to be 15pt size (its a number), second to be 10pt size (the label). It's easy to draw single string with one size to the rectangle, I do it like this:

$image = new Imagick('someimage.png');
$draw = new ImagickDraw;
$draw->setGravity(Imagick::GRAVITY_CENTER);
$draw->setfont(__DIR__ . DS . 'TREBUCBD.TTF');
$draw->setfontsize(15);
$draw->annotation(0, 0, '50 points');
$image->drawImage($draw);

这里的想法是让50分中的50变大。

The idea here is to have "50" in "50 points" to be big.

我试图用$ draw-> push()将当前设置推送到堆栈然后再次设置字体大小和注释,但两个字符串重叠。我一直试图这样做几个小时。非常感谢任何帮助!

I tried to do a $draw->push() to push the current settings to the stack then set the font size and annotation again but then the two strings overlap. I've been trying to do this for hours. Any help is very appreciated!

上面的实现是在PHP中,但我可能会通过使用命令行ImageMagick用法显示它的示例来实现它。

The above implementation is in PHP but probably I will manage to do it even by example that shows it with command line ImageMagick usage.

编辑:我已经开始奖励我将用PHP实现的解决方案。

edit: I've started a bounty that I will award for a solution implemented in PHP.

推荐答案

在点之前和50之后添加足够的空格,以便你很好地对齐它们:

Add enough space before "points" and after the "50" so you align them nicely:

<?php
$image = new Imagick('test.png');
$draw = new ImagickDraw;
$draw->setGravity(Imagick::GRAVITY_CENTER);
$draw->setfontsize(30);
$draw->annotation(0, 0, '50         ');
$image->drawImage($draw);

$draw = new ImagickDraw;
$draw->setGravity(Imagick::GRAVITY_CENTER);
$draw->setfontsize(15);
$draw->annotation(0, 0, '      points');
$image->drawImage($draw);

file_put_contents('test.png', $image->getImageBlob());
?>

这篇关于ImageMagick:如何在图像上绘制两个不同大小的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆