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 6049053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:31:57+00:00 2026-05-23T07:31:57+00:00

I have a PHP script that uses GD2 to create an image. It uses

  • 0

I have a PHP script that uses GD2 to create an image. It uses a TrueTypeFont file to generate text in the image with imagettftext (and imagettfbbox).
This script can be run on both Windows and Linux machines so I decided to copy a TTF file from the Windows/Fonts directory into the source-code, else I wouldn’t know where to look for it.
I’m not at all happy with this solution but I don’t know of a better one.

The real problem though is that on Windows/Apache the font file gets locked after it’s been used once. The only way to unlock it is to restart Apache. The locking is a problem because I can’t delete the file when I want to, which is especially annoying if you’re using a version system.

So my problem has 3 solutions:

  • Is there a way to avoid locking of font files (in source code/webroot) on Windows/Apache?
  • Or is there a way to avoid copying the font file and use a native available TrueTypeFont? (OS independent if at all possible, likely hosts are Windows and Linux – Mac, not so much)
  • Or is there a way to avoid using a TrueTypeFont and still get pretty (aliased) text with PHP GD2?

—

GD Support  enabled
GD Version  bundled (2.0.34 compatible)
FreeType Support    enabled
FreeType Linkage    with freetype
FreeType Version    2.1.9
T1Lib Support   enabled
GIF Read Support    enabled
GIF Create Support  enabled
JPG Support     enabled
PNG Support     enabled
WBMP Support    enabled
XBM Support     enabled 
  • 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-23T07:31:57+00:00Added an answer on May 23, 2026 at 7:31 am

    My solution is a combination of suggestions. I checked StackOverflow policy on splitting bounties and it’s not possible. The bounty must be awarded to a single comprehensive answer. I would like to split my bounty to all the people who replied / commented here, if there is an alternative way please contact me.

    I didn’t have much luck with the .gdf fonts, I can’t really find any good looking fonts. It did point me in the direction of imagestring which can draw text without the need of any font file (gdf or ttf) – see the php doc. The ‘default’ font is just some monospace font which isn’t very pretty but it serves nicely as a fallback.

    To avoid the .ttf locking I will attempt to find the OS font folder and load the font from there. This has been tested on my Windows development machine. If the .ttf file can’t be found it will use the native font fallback from imagestring.

    I’ve chosen an Object-Oriented approach to make an abstraction on how the text is written to the image.

    abstract class TextWriter {
    
        public static function getInstance() {
            $osName = php_uname( 's' );
    
            if (strtoupper(substr($osName, 0, 3)) === 'WIN') {
                $path = 'C:/Windows/Fonts/';
            } else if (strtoupper(substr($osName, 0, 5)) === 'LINUX') {
                $path = '/usr/share/fonts/truetype/';
            } else if (strtoupper(substr($osName, 0, 7)) === 'FREEBSD') {
                $path = '/usr/local/lib/X11/fonts/TrueType';
            }
            if (is_dir($path) && is_file($path . "/arial.ttf")) {
                return new TTFTextWriter($path . "/arial.ttf");
            }
            return new NativeTextWriter();
        }
    
        abstract public function get_dimenions($string);
        abstract public function write_text($img_resource, $x, $y, $text, $color);
    
    }
    
    class TTFTextWriter extends TextWriter {
    
        private $ttf_file;
        private $fontsize = 10;
    
        public function __construct($ttf_file) {
            $this->ttf_file = $ttf_file;
        }
    
        public function get_dimenions($text) {
            $dimenions = imagettfbbox($this->fontsize, 0, $this->ttf_file, $text);
            return array($dimenions[2], abs($dimenions[5] - $dimenions[3]));
        }
    
        public function write_text($img_resource, $x, $y, $text, $color) {
            imagettftext($img_resource, $this->fontsize, 0, $x, $y, $color, $this->ttf_file, $text);
        }
    
    }
    
    class NativeTextWriter extends TextWriter {
    
        private $fontsize = 3;  // number between 1-5 see manual for imagestring
        private $text_width = 7;  // corresponds to $fontsize 3
        private $text_height = 15;  // corresponds to $fontsize 3
    
        public function get_dimenions($text) {
            return array(strlen($text) * $this->text_width, $this->text_height);
        }
    
        public function write_text($img_resource, $x, $y, $text, $color) {
            imagestring($img_resource, $this->fontsize, $x, $y - $this->text_height, $text, $color);
        }
    }
    

    Use like:

    $writer = TextWriter::getInstance();
    
    $dimenions = $writer->get_dimenions($text);
    $width = $dimenions[0];
    $height = $dimenions[1];
    $im = imagecreatetruecolor($width, $height);
    $black = imagecolorallocate($im, 1, 1, 1);
    
    $writer->write_text($im, 0, 0, $text, $black);
    header('Content-Type: image/gif');
    
    imagegif($im);
    imagedestroy($im);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP script that initialises an image gallery. It loops through all
I have a PHP script that can encode a PNG image to a Base64
I have a PHP script that processes file uploads. The script tries to organise
I have a PHP script that pushes the headers to allow a file to
I have a PHP script that uses CURL to fetch a remote page and
I have a php script that uses some css/javascript tabs, they work on my
I have developed a PHP script that uses $_REQUEST[] superglobal. A typical client request
I have a PHP script that does the following: Uses file_get_contents() to get content
I have a resize script i made in PHP that uses GD (my VPS
I have a PHP script that runs as a CGI program and the HTTP

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.