在imagemagick中使用两行不同的字体 [英] Use two different fonts in imagemagick on one line

查看:416
本文介绍了在imagemagick中使用两行不同的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想在我的图像上画出文字,让我们在这个例子中说Trevor,24

So i want to draw on my image text lets say in this example "Trevor, 24"

但是我想用Treve的字体Helvetica,以及24我想使用字体Arial。但是我希望它在同一条线上,看起来像是一条。

But I want to use the font Helvetica for Trevor, and for 24 i want to use the font Arial. But I want it to be on the same line and look like it's one.

是否有可能或者我将如何制作它以便即使我更改了名称和年龄,我可以这样打印出来吗?

Is it possible or how would i go about making it so that even if i change the name and age, that i can print it out like this?

Trevor,(Helvetica)24(Ariel)

"Trevor, (Helvetica) 24(Ariel)"

我会假设将它们打印在彼此旁边,但是如果有人输入的名字比Trevor 24更长,那么就会超过它。

I would assume to print them next to eachother, but if someone enters the name longer than Trevor 24 would go over it.

这个想法是让它开启同一行

the idea is to make it on the same line

你们怎么想?

$draw = new ImagickDraw();
$color = new ImagickPixel('#5b5b5b');
$bgcolor = new ImagickPixel('none');
$font = 'Helvetica';
//$draw->setFont($font);
//$draw->setFontSize(39);
$draw->setFillColor($color);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);

$draw->setFont($font);
$draw->setFontSize(39);

$text = 'Trevor, 24';

$draw->setGravity(Imagick::GRAVITY_WEST);
$image->annotateImage($draw, 50, 241, 0, $text);


推荐答案

这很简单,但你必须这样做一点工作。使用 Imagick :: queryFontMetrics 跟踪每种字体的绘图宽度,并简单地偏移到 X 坐标以确保对齐均匀。

This is easy, but you'll have to do a bit of work. Use Imagick::queryFontMetrics to track the drawing width of each typeface, and simply offset to X coordinate to ensure alignment is uniformed.

// Let's create a generator to simplify context management (YMMV)
function context_generator() {
    $text = array('Trevor (Helventica)',' 24 (Impact)');
    $font = array('Helvetica', 'Impact');
    foreach($text as $k => $v ) yield [$font[$k], $v];
}
$image = new Imagick();
$image->newImage(450, 100, "steelblue", "png");
$draw = new ImagickDraw();
$draw->setFillColor('black');
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);
$draw->setFontSize(24);
$x = $y = 40;
foreach(context_generator() as $attr) {
    // Set context typeface
    $draw->setFont($attr[0]);
    // Calculate how big this type face will be (and any validation to protect overflow)
    $metrics = $image->queryFontMetrics($draw, $attr[1], FALSE);
    // Draw part
    $image->annotateImage($draw, $x, $y, 0, $attr[1]);
    // Offset origin X
    $x += $metrics['textWidth'];
}

当然上面的例子可以简化&减少。

这篇关于在imagemagick中使用两行不同的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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