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
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
imagestringwhich 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.
Use like: