字体大小取决于图像大小


font size depending image size

我很抱歉我的英语!我有这个问题与imagick php和覆盖文本在图像上。当调整图像宽度时,字体大小较小。这是一个两个图像宽度的例子,第一个是350px,第二个是1728px:https://i.stack.imgur.com/8kRo0.jpghttps://i.stack.imgur.com/TedDO.jpg

$f->userFontSize是登录用户选择在其图像上显示的字体大小。

if(trim($_GET['full']) != 'si'){
$width = 350;
$height = 350;
} else {
$width =    getWidth($root . $f->imagePathFull);
$height =     getHeight($root . $f->imagePathFull);
}
$image = new imagick();
$draw = new imagickDraw();
$pixel = new imagickPixel('white');
$image->newImage($width, $height, $pixel);
$image->readImage($root . $f->imagePathNormal); 
$image->resizeImage ( $width, $height,  imagick::FILTER_LANCZOS, 1, TRUE);
$fontPath = $root . '/assets/fonts/Mermaid.ttf';
$draw->setFillColor('black');
$draw->setFont($fontPath);
$draw->setFontSize(12 + $f->userFontSize);
$draw->setGravity(1);
$draw->setTextAntialias(true);
$draw->setFillOpacity(".55");
$image->annotateImage($draw, 5,0, 0, "text text text");
$image->setImageFormat("jpg");
header( "Content-Type: image/jpeg" );
echo $image->getImageBlob();
$image->clear();

当我打印分辨率为1728px的图像时,文本非常小。如何根据图像大小增加字体大小?谢谢你!:)

我已经用这种方式解决了,我计算了350px的文本占用图像的宽度百分比,根据这个我计算了1728px(或另一个宽度)的百分比。我圈出一些字体大小,直到我找到一个大小相等的百分比。我的解决方案的最终结果是不完美的,但可以接受。这是代码:

$myCP = '40'; // increase o decrease font size.
for($i = 1; $i <= 500;$i++){
    $box = imagettfbbox($i, 0, $fontPath, "lorem ipsum");
    $lT = $box[2] - $box[0];
    $cP = round(($lT / $width) * 100,1);
    // $cP percentage of font width
    if($cP >= $myCP) { $newFontSize = $i; break; } else { continue; }
}