Inaccurate results from ImageMagick function queryFontMetrics

 ̄綄美尐妖づ 提交于 2020-01-05 12:33:19

问题


Imagick::queryFontMetrics does not seem to be working. When I use the metrics provided by queryFontMetrics to size the image, some fonts are still being cut off. Any ideas?

Here's my code:

if (!file_exists($cache['dirname'].'/'.$cache['basename'])) {

try {
    $draw = new ImagickDraw();
    $draw->setFont($font_path['dirname'].'/'.$font_path['basename']);
    $draw->setFontSize($size);
    $draw->setGravity(Imagick::GRAVITY_CENTER);
    $draw->setFillColor($color);

    $canvas = new Imagick();

    $metrics = $canvas->queryFontMetrics($draw, $text);

    $canvas->newImage($metrics['textWidth'], $metrics['textHeight'], "transparent", "png");
    $canvas->annotateImage($draw, 0, 0, 0, $text);

    $canvas->setImageFormat('PNG');
    mkdir($cache['dirname'], 0777, true);
    $canvas->writeImage($cache['dirname'].'/'.$cache['basename']);

    header("Content-Type: image/png");
    echo $canvas;

    $canvas->clear();
    $canvas->destroy();

    $draw->clear();
    $draw->destroy();

} catch(Exception $e) {
    // Output an error message
    echo 'Error: ',  $e->getMessage(), "";
}

} else {
    // Output the image
    $canvas = new Imagick($cache['dirname'].'/'.$cache['basename']);
    header("Content-Type: image/png");
    echo $canvas;
}

回答1:


Okay, it looks like I'll be answering this one myself. After quite a bit of research, I've discovered that it's basically impossible to get correct metrics from the font itself. This is because each font designer could potentially define the metrics differently. The best way to proceed is to simply make the image much larger than necessary (to ensure that no clipping occurs) and then use the trim command: http://www.php.net/manual/en/imagick.trimimage.php.



来源:https://stackoverflow.com/questions/11235969/inaccurate-results-from-imagemagick-function-queryfontmetrics

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!