Search code examples
phpgd

Invalid font filename unable to use .ttf file in PHP


I'm trying to add text to an image in PHP with imagettftext. However, it doesn't seem to accept any .ttf file and keeps giving the errors: 'Could not find/open font' and 'Invalid font filename'. The file path is localhost/fonts/arial.ttf

I've tried relative paths, absolute paths, changing / to \ , tried putting ../ before the path but nothing seems to work. I've checked the GD version and its the 2.1.0 and the FreeType Linkage is 'with freetype' whatever that means.

<?php

    $output = "pics/email.png";

    $x = 720;
    $y = 480;
    $image = imagecreate($x, $y);

    //colors to use (rgb)

    $white = imagecolorallocate($image, 255, 255, 255);
    $black = imagecolorallocate($image, 0, 0, 0);

    $font = 'fonts/arial.ttf';

    $text1 = imagettftext($image, 40, 0, 20, 40, $black, $font, "Text Sample");


    imagejpeg($image, $output);

    var_dump(gd_info());



    ?>

I'd be thankful if anyone could give a working answer.


Solution

  • Already solved it, thanks to the people in the comment section.

    I just had to use the full file path (C://.../fonts/arial.ttf)