Results 1 to 3 of 3

Thread: need help calculating font with image width and height

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,137
    Thanks
    96
    Thanked 98 Times in 96 Posts

    Default need help calculating font with image width and height

    I'm working on a project for a friend that will generate an image of text where the two variables are font and size using php's Graphics Draw library.

    For this example, I am using the free font After Shok.
    The main functions I am trying to wrap my mind around are imagettfbbox, imagecreatetruecolor, and imagettftext

    The script seems to work great with getting the horizontal, but the vertical height/positioning is off.

    The first script displays the position of the image using the imagettfbbox function.

    PHP Code:
    <?php
    $text
    ='Imperium';
    $font 'storage/fonts/After_Shok.ttf';
    $size=180;
    $bbox imagettfbbox($size0$font$text);
    $width '0';
    $angle 0;
    $a=abs($bbox[3]);
    $b=abs($bbox[5]);
    $height=$a+$b;
    $im imagecreatetruecolor($bbox[2]-$bbox[0], $height);
    $bg imagecolorallocate($im505);
    $color imagecolorallocate($im25500);
    $width '0';
    $angle 0;
    ##$height=abs($bbox[3])+abs($bbox[5]);
    imagefill($im005);
    imagettftext($im$size$angle$width-$bbox[0], $size+10$color$font$text);
    echo 
    "width=$width-$bbox[0]<br>height=$size+10<br>
    lower  left corner = 
    $bbox[0],$bbox[1]<br>
    lower right corner = 
    $bbox[2]$bbox[3]<br>
    upper right corner = 
    $bbox[4]$bbox[5]<br>
    upper  left corner = 
    $bbox[6]$bbox[7]<br>";
    ##header('Content-type: image/png');
    ##imagepng($im);
    imagedestroy($im);
    ?>
    The second script, which is nearly identical to the first, will display the image.

    PHP Code:
    <?php
    $text
    ='Imperium';
    $font 'storage/fonts/After_Shok.ttf';
    $size=18;
    $bbox imagettfbbox($size0$font$text);
    $width '0';
    $angle 0;
    $a=abs($bbox[3]);
    $b=abs($bbox[5]);
    $height=$a+$b;
    $im imagecreatetruecolor($bbox[2]-$bbox[0], $height);
    $bg imagecolorallocate($im505);
    $color imagecolorallocate($im25500);
    $width '0';
    $angle 0;
    ##$height=abs($bbox[3])+abs($bbox[5]);
    imagefill($im005);
    imagettftext($im$size$angle$width-$bbox[0], $size+10$color$font$text);
    ##echo "width=$width-$bbox[0]<br>height=$size+10<br>
    ##lower  left corner = $bbox[0],$bbox[1]<br>
    ##lower right corner = $bbox[2], $bbox[3]<br>
    ##upper right corner = $bbox[4], $bbox[5]<br>
    ##upper  left corner = $bbox[6], $bbox[7]<br>";
    header('Content-type: image/png');
    imagepng($im);
    imagedestroy($im);
    ?>
    Feel free to play around with it. I'm open to any criticism and ideas.
    Last edited by james438; 11-02-2019 at 03:36 AM. Reason: resolved
    To choose the lesser of two evils is still to choose evil. My personal site

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,137
    Thanks
    96
    Thanked 98 Times in 96 Posts

    Default

    I now have a script that works. I want to refine the code before I post it. I also need to look it over for possible bugs.

    The big clue to solving this was when I figured out that the width between -1 and 1 is not 2, but 3. After that, understanding the width and height of the box and the positioning of the text inside the box began to fall into place.
    Last edited by james438; 11-02-2019 at 04:24 PM. Reason: grammar
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,137
    Thanks
    96
    Thanked 98 Times in 96 Posts

    Default

    There may still be bugs that I have missed, but here it is:

    PHP Code:
    <?php
    $text  
    'uu';
    $font  'ImperatorSmallCapsBold.ttf';
    $size  300;
    $bbox  imagettfbbox($size0$font$text);
    $width 0;
    $box_width=abs($bbox[0]-$bbox[2]);
    $angle 0;
    $a     0;
    $height=abs($bbox[3])+abs($bbox[5]);
    if (
    $bbox[3]<or $bbox[5]<1$a=1;
    $img   imagecreatetruecolor($box_width$height+$a);
    $bg    imagecolorallocate($img505);
    $color imagecolorallocate($img25500);
    imagefill($img00$bg);
    $h_position=$width-$bbox[0];
    $v_position=$height-$a;
    imagettftext($img$size$angle$h_position$v_position$color$font$text);
    header('Content-type: image/png');
    imagepng($img);
    imagedestroy($img);
    ?>
    This script will create a box and write text to that box. The boundaries of the box are a pixel perfect match for the boundaries of the text, so that no text is cut off and there is no excess space wasted horizontally or vertically.

    Some possible uses include writing custom text onto an image, creating a logo with a transparent background, creating a watermark onto an image.

    I tried this with four different fonts. Not a lot, so if anyone tries a font that does not work, please let me know.

    Variables in the script to change include the following:

    Code:
    $text  = 'uu';
    $font  = 'ImperatorSmallCapsBold.ttf';
    $size  = 300;
    color can be in the following format:

    Code:
    $color = imagecolorallocate($img, 255, 0, 0);
    $color='009900';
    to make the background transparent add the following before the header:

    Code:
    imagecolortransparent($img, $bg);
    Limitations:

    • Only works with .ttf fonts.
    • Changing the angle of the script causes the text to not fit the box.
    To choose the lesser of two evils is still to choose evil. My personal site

Similar Threads

  1. calculating div height
    By gib65 in forum CSS
    Replies: 3
    Last Post: 12-04-2015, 08:29 PM
  2. javascript code calculating average height
    By mamita in forum JavaScript
    Replies: 1
    Last Post: 03-02-2010, 11:53 PM
  3. Replies: 0
    Last Post: 07-06-2009, 05:33 AM
  4. Image w/ description tooltip height and width how?
    By melias in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 11-29-2007, 05:00 AM
  5. Replies: 3
    Last Post: 05-23-2007, 08:19 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •