繁体   English   中英

字体大小取决于图像大小

[英]font size depending image size

对不起,我的英语! 我在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();

当我以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; }
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM