Invalid font file name (imagettfbox)

This question was asked again and again, but I could not find the right answer to my problem ... As a small note, all the code worked fine before we moved the class file from / application / lib / class to / library / class ...

I tried to play with GDFONTPATH, relative, absolute paths with and without a file extension to no avail. Here are some of the lines we have tried so far:

putenv('GDFONTPATH=' . realpath(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'fonts')); /*1*/ $FontName = dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'fonts'.DIRECTORY_SEPARATOR.basename($FontName,'.ttf'); /*2*/ $FontName = '\pChart\fonts\\'.basename($FontName); /*3*/ $FontName =basename($FontName); $coords = imagettfbbox($FontSize, 0, $FontName, $Text); 

Several combinations of these attempts have also been used to no avail. I am really unhappy with this problem since # 1, when echo'ed gives the full path that opens the correct font file if it is copied / pasted into win explorer.

This can help to find out the absolute path to the file that receives the error and the path to the font name ...

 C:\wamp\www\application_bundle\Library\pChart\class\pImage.class.php C:\wamp\www\application_bundle\Library\pChart\fonts\arialuni.ttf 

We face this problem on all development platforms (Win, Mac, and Linux). PHP 5.3.13

Thank you for your help.

Edit: It seems that the file was not found / the server is not looking at the right folder ... If someone can help fix the problem, specifying how to determine which path GD tries to open is really help.

+4
source share
4 answers

We figured out how to make it work.

In short, we included a class file, and then called methods to write text. We did something like this:

 $classPath = 'pChart/'; include($classPath.'/class/pImage.class.php'); //... inside the pImage.class we passed font like this: $FontName = $classPath.'/fonts/arialuni.ttf'; imagettfbbox($FontSize, 0, $FontName, $Text) 

This did not work, what we did before or after ... Until we changed $ classPath to

 $classpath = '../library/pChart/'; 

Please note that they (or should) point to exactly the same folder as the code executed from the file in the root of the library.

We tried to find why the absolute paths did not work, but could not reproduce the error in an isolated environment, so there is something suspicious in our architecture.

Thank you for your time.

+2
source

I know that this was answered and accepted, but I did not see anyone thinking about why this solution worked and what was wrong in the first place.

Brief Description of the Problem

The current working directory is set at the php point that processes the request, and all relative paths are resolved based on the current working directory, not the directory of the file to which the path is pointed.

Long problem description

Relative paths in php are resolved based on the current working directory.

For testing purposes, you can always see what the current working directory is by calling getcwd

This value is initially sent to the http request as a directory containing the file that the web server originally passed to the request in php.

So, for example, if you go to http://www.mydomain.com/index.php , the current working directory will be the same as the document root ( $_SERVER["DOCUMENT_ROOT"] )

For a CLI request, cwd is the directory you are in when you execute the command. Therefore, if I am in /home/orangepill and I run /usr/bin/php /path/to/file.php , cwd will be /home/orangepill .

This causes a problem for relative file references inside the included files.

Let's look at this example.

  • The client will go to the site www.mydomain.com

  • Apache has index.php installed in the DirectoryIndex directive, and apache finds the index.php file in the document root directory. The current working directory is set to the root of the document.

  • /index.php contains the line include "library/myclass.php"; $ _SERVER ["DOCUMENT_ROOT"]. "/Library/myclass.php" exists and everything is fine

  • myclass.php contains the line include("myclass_helper.php"); which permits $ _SERVER ["DOCUMENT_ROOT"]. "/myclass_helper.php". (remember that relative links are resolved relative to the current working directory)

  • $_SERVER["DOCUMENT_ROOT"]."/myclass_helper.php" does not actually exist in $_SERVER["DOCUMENT_ROOT"]."/library/myclass_helper.php"

You are probably, but wait ... I experienced different behaviors in my scripts by including it in the include. The reason for this is that include and require language constructs (along with several other file system commands) try to include relative paths from each of the paths specified in the include php directive. So, in the above example, if the library directory with the document root existed inside the included paths, then everything will work as expected.

A bulk solution for the required files relative to the current file is to structure your include paths using the __DIR__ context constant. So you would use include __DIR__."/myclass_helper.php"; ( include dirname(__FILE__)."/myclass_helper.php in pre PHP 5.3 environments), and at run time it would actually turn your relative path into an absolute path based on the location of the file making the include.

For the usual included directories, I’m used to setting some frequently used places for use with relative file system links. eg

 define ("APPLICATION_PATH", realpath($_SERVER["DOCUMENT_ROOT"]."/application"); define ("LIBRARY_PATH", realpath($_SERVER["DOCUMENT_ROOT"]."/library"); define ("CONFIG_PATH", APPLICATION_PATH."/etc/"; 

This gives you a lot of reference points for including paths relatively.

+2
source

Have you dropped $FontName before this piece of code to debug its value?

In any case, if you already install GDFONTPATH , you do not need to use any path on $FontName , in this case you can just use the font name (arialuni.ttf or even just arialuni) or in most basename() function as in the example # 3

Your call to putenv() might just be putenv('GDFONTPATH=' . realpath('../fonts')) .

Try to see if it works:

 putenv('GDFONTPATH=' . realpath('../fonts')); $FontName = 'arialuni.ttf'; // note that I'm using font name directly $coords = imagettfbbox($FontSize, 0, $FontName, $Text); 

Update 1

Please reset your GDFONTPATH and tell me what it prints. Add that after your call to putenv() it will cause your code to raise the error with the GDFONTPATH that it was looking for:

  trigger_error(sprint('GDFONTHPATH = %s', getenv('GDFONTPATH')), E_USER_ERROR); 
0
source

You can also just try to provide a link regarding the root directory. I had the same problem ("Invalid file font name", function "...") and fixed as in the main file where pChart is initialized:

 $myPicture->setFontProperties(array("FontName"=>$_SERVER['DOCUMENT_ROOT']."/files/lib/pChart/fonts/verdana.ttf")); 

Solved the problem for me. This line is used like this:

 $myPicture->setFontProperties(array("FontName"=>"fonts/verdana.ttf")); 

which - I don’t know why - an error occurred today.


Please note that this solution is not very elegant, as it depends on the fact that your pChart folder is actually located on something like

 http://myHomepage.com/files/lib 

that is, it does not work with relative directories.

0
source

Source: https://habr.com/ru/post/1489902/


All Articles