简体   繁体   中英

font size depending image size

I'm sorry for my English! I've this problem with imagick php and overlay text on an image. Size of font is small when resize the width of image. This is an example for two image width, first is 350px and second is 1728px: http://i.stack.imgur.com/8kRo0.jpg http://i.stack.imgur.com/TedDO.jpg

$f->userFontSize is the size of font that logged user choose to show on his image.

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();

When I print the image with resolution as 1728px the text is very small. How I do to do to increase font size depending image size? Thank you! :)

I've solved with this way, I calculated the percentage of width that text occupy on image at 350px and according to this I calculated the percentage at 1728px(or another width). I do cicle a some font size until when I find a size that is egual at percentage. The final result of my solution is non perfect but acceptable. This is the code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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