你可以做這樣的事情,通過查詢字體規格,但也有可能是一個更好的辦法,我不知道....
#!/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