问题
So i want to draw on my image text lets say in this example "Trevor, 24"
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)"
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
What do you guys think?
$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);
回答1:
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'];
}
Of course the above example can be simplified & reduced.
回答2:
You could look at using Pango with ImageMagick. I know it works at the command line but have never tried with the PHP binding...
convert \
pango:'<span font="Times 48" foreground="white" background="blue">Trevor</span><span font="Arial 32" foreground="yellow" background="black">24</span>' \
pango.png
来源:https://stackoverflow.com/questions/33115865/use-two-different-fonts-in-imagemagick-on-one-line