簡體   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