你可以做这样的事情,通过查询字体规格,但也有可能是一个更好的办法,我不知道....
#!/usr/local/bin/php -f
<?php
$image = new Imagick();
$draw = new ImagickDraw();
$draw->setFillColor('black');
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);
$draw->setFontSize(24);
$text="Hello, I am a lovely label";
// Set typeface
$draw->setFont('Impact');
// Calculate size
$metrics = $image->queryFontMetrics($draw,$text,FALSE);
$w=$metrics['textWidth'];
$h=$metrics['textHeight'];
$y=$metrics['ascender'];
$image->newImage($w,$h,"steelblue","png");
$image->annotateImage($draw,0,$y,0,$text);
$image->writeImage("result.png");
?>
使用了非常大的画一个然后使用trimImage() - 示例是:http://phpimagick.com/Tutorial/imageGeometryReset的一部分 - 但是,queryFontMetrics也是一个不错的选择。 – Danack