PHP imagettftext返回边界框与渲染边界框不同 [英] PHP imagettftext return bounding box differs from rendered bounding box

查看:178
本文介绍了PHP imagettftext返回边界框与渲染边界框不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用imagettftext来渲染PNG文件。对imagettftext()的调用将返回文本呈现的边界框,但仔细检查后,文本将稍微呈现在其自己的边界框之外!边界框是正确的(我检查了图像的像素坐标),但文本位置不正确,它输出这个,其中框是渲染文本后返回的边界框:

I'm using imagettftext to render a PNG file. A call to imagettftext() returns the bounding box that the text was rendered in, but on closer inspection, the text is being rendered slightly outside of it's own bounding box! The bounding box is correct (I inspected the pixel coords of the image), but the text location is incorrect, it outputs this, where the box is the returned bounding box after rendering the text:

我的代码是:

// helper function for geting textbox bounds
function bounds($text,$fontFile,$fontSize,$fontAngle) { 
    $rect = imagettfbbox($fontSize,$fontAngle,$fontFile,$text); 
    $minX = min(array($rect[0],$rect[2],$rect[4],$rect[6])); 
    $maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6])); 
    $minY = min(array($rect[1],$rect[3],$rect[5],$rect[7])); 
    $maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7])); 

    return array( 
        "left"   => abs($minX) - 1, 
        "top"    => abs($minY) - 1, 
        "width"  => $maxX - $minX, 
        "height" => $maxY - $minY, 
        "box"    => $rect 
    ); 
}

$canvas = @imagecreate(640, 680)
    or die('Cannot Initialize new GD image stream');

$title_color = imagecolorallocate($canvas, 153, 153, 153);
$content_color = imagecolorallocate($canvas, 51, 51, 51);

$content_bounds = bounds("12", "Helvetica_Reg.ttf", 75, 0);
$test = imagettftext($canvas, 75, 0, 30, 200, $content_color, "Helvetica_Reg.ttf", "12");
imagerectangle($canvas, $test[0], $test[1], $test[4], $test[5], $title_color);


推荐答案

可能是字体有问题,我测试你的代码与另一个,我得到了

maybe is a problem with the font, i test your code with another one, and i got

这篇关于PHP imagettftext返回边界框与渲染边界框不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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