Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 998827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:15:41+00:00 2026-05-16T07:15:41+00:00

I am currently working on a website and lately have been using GD with

  • 0

I am currently working on a website and lately have been using GD with PHP to automatically create transparent images that consist of text for use with navigation, headers, etc on the website. It saves me a lot of time in Photoshop plus I can change the text on the buttons instantly when needed.

Well I have hit a dead end here. I found the method of sizing the “textbox”, created as my image, to what the size of the text is. But the problem I am facing is the fact that I am using a TTF font which is different then what GD expects the size to be. Basically, the last letter would be chopped off the outputted image. So I was wondering if there was a way to fix this while keeping a tight edge to the text or make the original textbox a much larger size and then “trim” the transparent pixels off the image.

This is the code I’m working with now…

<?
$text = strip_tags($_GET['btn']);
if(file_exists('nav_'.$text.'.png')) {
    header("Content-type: image/png");

    $image = imagecreatefrompng('nav_'.$text.'.png');
    imagesavealpha($image, true);
    imagealphablending($image, false);
    imagepng($image);
    imagedestroy($image);
} else {
    header("Content-type: image/png");

    $fontSize = 10;
    $angle = 0;
    $font = "RonniaBold.ttf";

    $size = imagettfbbox($fontSize, $angle, $font, $text);
    $image = imagecreatetruecolor(abs($size[2]) + abs($size[0]) + 5, abs($size[7]) + abs($size[1]) + 5);
    imagesavealpha($image, true);
    imagealphablending($image, false);

    $transparentColor = imagecolorallocatealpha($image, 200, 200, 200, 127);
    imagefill($image, 0, 0, $transparentColor);

    $textColor = imagecolorallocate($image, 125, 184, 222);
    imagettftext($image, $fontSize, 0, 1, abs($size[5])+1, $textColor, $font, str_replace('_',' ',strtoupper($text)));
    imagepng($image, 'nav_'.$text.'.png', 0);
    imagedestroy($image);
}
?>

Hopefully you guys have some insight to this, I could really use it!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:15:42+00:00Added an answer on May 16, 2026 at 7:15 am

    Wherever possible, I use the Imagick class, as I prefer the ImageMagick library. The following example is taken almost verbatim from the example given here. It creates an image based on the size of the text supplied:

    $text = 'Hello World!';
    
    // Create a new ImageMagick objects.
    $im = new Imagick();
    $draw = new ImagickDraw();
    $colour = new ImagickPixel('#000000');
    $background = new ImagickPixel('none');
    
    // Set font properties.
    $draw->setFont('Fertigo_PRO.otf');
    $draw->setFontSize(72);
    $draw->setFillColor($colour);
    $draw->setStrokeAntialias(true);
    $draw->setTextAntialias(true);
    
    // Get font metrics.
    $fm = $im->queryFontMetrics($draw, $text);
    
    // Create text.
    $draw->annotation(0, $fm['ascender'], $text);
    
    // Create a new empty canvas, using the text size.
    $im->newImage($fm['textWidth'], $fm['textHeight'], $background);
    
    // Create the image.
    $im->setImageFormat('png');
    $im->drawImage($draw);
    
    // Save the image.
    $im->writeImage('test.png');
    

    If you want more information on the Imagick class, I recommend the excellent Imagick articles from Mikko’s Blog.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on a website that will have a high volume of
I'm currently working on a quote retrieval system for my employer's website using mysql/php/html.
Currently I am working on an existing website that was initially created using SMF
I am currently working on a website that is an advertisement portal for businesses.
I am currently working on my first website. I have no idea where to
I am working on a website that currently has a number of disparate search
I'm currently working on a website that needs a sort of sticky footer, but
I am currently working on adding pre-validation to my website. So that pages that
I have a website that my company hosted using hubspot.. now the guy who
I'm currently working on a website that imports XML data that we download from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.