Skip to content

A working example of the PHP function imagettftext() on Windows

License

Notifications You must be signed in to change notification settings

thethales/PHPImagettftextExample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

The PHP function imagettftext() is used to write text to an image using TrueType fonts. It's usage is not exatcly straight foward, depending a lot on factors such as the PHP Installation, PHP GD Library and it's relation with the Operating System, often otputing the following message:

Warning: imagettftext() [function.imagettftext]: Could not find/open font

There's a lot of discussion over the internet with equally abundant mixed results regarding the implementation of this specific function. My own experience with the function was frustrating to say the least. And since, researching the topic did not yield a full working example for my setup, here is my humble contribution:

Note: The index.php contains a working example that creates watermarked file at the output directory, you can skip directly to that file

On Windows

One possible solution is to set, the font file path as one of the installed fonts of the OS.

$font = c:/windows/fonts/arial.ttf
imagettftext($image_png, $fontsize, $angle, $margin_x, $margin_y, $color_red, $font, $watermark_text);

For using custom fonts not loaded with the OS, you could set the GD Library font environment as per recommended by PHP.net (though exactly not for the same reasons) and specify the full os path:

putenv('GDFONTPATH=' . realpath('.'));

$font = dirname(__FILE__) . '/font/freesans.ttf
imagettftext($image_png, $fontsize, $angle, $margin_x, $margin_y, $color_red, $font, $watermark_text);

On Linux

I have yet to find any problems with this function running on the Linux Apache environment, that being said, the following configuration should work:

$font = dirname(__FILE__) . '/font/freesans.ttf');
imagettftext($image_png, $fontsize, $angle, $margin_x, $margin_y, $color_red, $font, $watermark_text);

Prototype

imagettftext() Output Example

The index.php contains a working example that creates watermarked files in the output directory. This solution defines a constant for the ```fontpath`` based on the operating system, the implementation is designed for instructives purposes

    if (PHP_OS == 'WINNT'){

        //Option 1  
        putenv('GDFONTPATH=' . realpath('.'));  //This is necessary to reference font files files on Windows
        define("FONT_PATH_DEFAULT" , dirname(__FILE__) . '/font/freesans.ttf');

        //Option 2
        //define("FONT_PATH_DEFAULT" , 'c:/windows/fonts/arial.ttf'); 
    }else{
        define("FONT_PATH_DEFAULT" , dirname(__FILE__) . '/font/freesans.ttf');
    }

    PNGApplyWaterMark($watermark_text,$original_image);

Basic Troubleshooting

As stated on the PHP manual for this function, it will only work if your version of php as freetype support. To check that, run phpinfo() on a new web page in your server:

<?php
    echo phpinfo();
?>

The page will output a list with every characterist of your system:

phpInfo Output Example

Confirm if the following libraries are either enabled or installed:

GD Library => Installed
GD Support => ENABLED
FreeType Support => Enabled
PNG Support => Enabled
JPEG Support => Enabled

About

A working example of the PHP function imagettftext() on Windows

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published