0

I am trying to use a font that I've uploaded in my website, but I keep getting this error:

"imagettftext(): Could not find/open font".

I have already tried using the putenv tool, but it still doesn't open the file. Is there an option on Bonfire that limits what kind of files that can be used? I am able to use the imagestring function, but I want to have other fonts.

I was able to load the font in an HTML file, so it looks like it has a problem with imagettftext().

    $image = imagecreatefrompng('/home/dev3/public_html/themes/admin/images/countdown.png');
    $font = array(
        'size'=>40,
        'angle'=>0,
        'x-offset'=>10,
        'y-offset'=>70,
        'file'=>'/home/dev3/public_html/fonts/DIGITALDREAM.ttf',
        'color'=>imagecolorallocate($image, 255, 255, 255),
    );

            $image = imagecreatefrompng('/home/dev3/public_html/themes/admin/images/countdown.png');
            // Open the first source image and add the text.
            $text = $interval->format('%a:%H:%I:%S');
            if(preg_match('/^[0-9]\:/', $text)){
                $text = '0'.$text;
            }
            $text,$font['color']);
            putenv('GDFONTPATH=' . realpath('.'));
            imagettftext ($image , $font['size'] , $font['angle'] , $font['x-offset'] , $font['y-offset'] , $font['color'],$font['file'] ,$text);

            ob_start();
            imagegif($image);
            ob_end_clean();

    $gif = new AnimatedGif($frames,$delays,$loops);
    $gif->display();
Bharata
  • 13,509
  • 6
  • 36
  • 50
  • Try to use the full path. Or look at the output of `echo getcwd();`. Is it the same as where you .ttf file is? – Cheery Oct 08 '14 at 22:23
  • No the .ttf file is in the same directory as my php. I've put a copy of the .ttf file in several places, including the public_html folder with the correct path an it still is giving me problems. I am able to open a url to the path, and it prompts me to download the file. I even tried using the URL on a different server and it worked fine. I'm thinking the issue has something to do with imagettftext() because the imageString function works just fine, I just want to use a .ttf file – inlandquarter Oct 10 '14 at 17:17

1 Answers1

1

GD / FreeType font loading code is restricted to the local filesystem. Your code is trying to read the font from an HTTP URL:

$font = array(...
   'file'=>'https://dev3.successengineapps.com/fonts/DIGITALDREAM.ttf'
...);

The font-loading code in GD doesn't know how to make an HTTP request.

Here is an example of the minimal set of your code required to get some kind of output; that is, I didn't attempt a serious rewrite in any way of your code, but also stripped out anything that was not obviously directly related to the problem:

<?php
header('Content-Type: image/gif');
$image = imagecreatefrompng('https://dev3.successengineapps.com/themes/admin/images/countdown.png');
    $font = array(
        'size'=>40,
        'angle'=>0,
        'x-offset'=>10,
        'y-offset'=>70,
        'file'=>'./DIGITALDREAM.ttf',
        'color'=>imagecolorallocate($image, 255, 255, 255),
    );
$text = "Hello, world.";
if (imagettftext ($image , $font['size'] , $font['angle'] , $font['x-offset'] , $font['y-offset'] , $font['color'],$font['file'] ,$text)) {
        imagegif($image);
} else {
        var_dump($php_errormsg);
}

The output of this code is visible here:

GD code

My recommendation would be to start with this, and see if you can get working output, and then slowly add additional code back in until you either have a working solution, or find what is breaking it.

TML
  • 12,813
  • 3
  • 38
  • 45
  • Array ( [GD Version] => bundled (2.0.34 compatible) [FreeType Support] => 1 [FreeType Linkage] => with freetype [T1Lib Support] => [GIF Read Support] => 1 [GIF Create Support] => 1 [JPEG Support] => 1 [PNG Support] => 1 [WBMP Support] => 1 [XPM Support] => 1 [XBM Support] => 1 [JIS-mapped Japanese Font Support] => ) – inlandquarter Oct 10 '14 at 17:20
  • I know so - the code for opening/loading TTF files does not use PHP's `streams`, it uses FreeType, which doesn't support URLs, only local filesystem. – TML Oct 10 '14 at 17:21
  • I currently am trying a path and that isn't working either(/home/dev3/public_html/fonts/DIGITALDREAM.ttf) – inlandquarter Oct 10 '14 at 17:22
  • You should refine your question, which asks "Does anyone know why imagettftext would have a problem with opening a file using a URL?" – TML Oct 10 '14 at 17:23
  • In a different server, I have almost identical code with the file being referenced simply by adding the GDfontpath, and using just the name of the file(no path), which leads me to this issue. I have a copy in the same directory as my .php file, but that doesn't work either... – inlandquarter Oct 10 '14 at 17:24
  • Show your PHP code, and remove the last sentence of the question. It would really help us help you. – TML Oct 10 '14 at 17:27
  • Put the PHP code right into the question, mark it as code using the curly-braces icon above the edit box. – TML Oct 10 '14 at 17:30
  • All set, fair warning it is a mess. – inlandquarter Oct 10 '14 at 17:31
  • This code is still trying to load it from an HTTP request; show the code trying to load it from the local filesystem. – TML Oct 10 '14 at 17:35
  • Maybe a file permission or file owner problem? – cjs1978 Oct 10 '14 at 17:38
  • Alright this should be the applicable code to the question. It shouldn't be a file owner problem, I am able to access the file in several different locations – inlandquarter Oct 10 '14 at 17:42
  • Is there an excessive comma up there? – cjs1978 Oct 10 '14 at 17:44
  • 'file'=>'/home/dev3/public_html/fonts/DIGITALDREAM.ttf',', – cjs1978 Oct 10 '14 at 17:45
  • That fixed it, the comment wasn't the issue, I accidentally added that. For some reason that path worked though. Thank you guys! – inlandquarter Oct 10 '14 at 17:57