GD2 Fonts locking on Windows/Apache GD2 Fonts locking on Windows/Apache apache apache

GD2 Fonts locking on Windows/Apache


I also faced such an issue with GD but on a Debian distro. I found two solutions that might lead to wamp solutions as well :

a- install the bundle graphic library from dotdeb.org libgd i/o native php GD library and recompile the sources the with-freetype option,

or

b- install imagick instead of GD

I applied solution "a" successfully on a debian distro.

Here are some links I used at that time :
imagick on wamp
howtoforge
drupal
libgd
boutell


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


Why not using the library-defined font path?

From the imagettftext() docs, you can use the library font path:

Depending on which version of the GD library PHP is using, when fontfile does not begin with a leading / then .ttf will be appended to the filename and the library will attempt to search for that filename along a library-defined font path.

See the gd_info() page to find out which ttf library your windows php version is using. Then check the according library documentation what the font-path is.

Using TTFs from the fontpath might have the problem.