【问题标题】:font size depending image size字体大小取决于图像大小
【发布时间】:2015-07-11 09:06:08
【问题描述】:

对不起我的英语! 我在图像上使用 imagick php 和覆盖文本时遇到了这个问题。 调整图像宽度时,字体变小。 这是两个图像宽度的示例,第一个是 350px,第二个是 1728px: http://i.stack.imgur.com/8kRo0.jpg http://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();

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

【问题讨论】:

  • 如果您知道字体大小 12 适合您的 350 像素宽度,那么可能会进行一些试验和错误,对于此示例,假设字体大小 20 适合您的 1728 像素宽度。我们有可能的字体大小为 12、13、14、15、16、17、18、19 和 20。在这种情况下,您的最大宽度和最小宽度之间的差异是 1378 像素,让我们将其除以可能的字体大小(即9),因此对于 153px 的每一步,您大致希望将字体大小增加一。逻辑应该成立,但请调整确切的变量以适合您的项目。 :)

标签: php image imagick


【解决方案1】:

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

$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; }
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    相关资源
    最近更新 更多