Short Question
I want to pass GDFONTPATH environment variable from Apache vhost.conf to PHP. How can I do this?
I am running: Debian 12, Apache 2.4, PHP 8.2
Long question
I am using GD image functions (e.g. imagettftext) in PHP and want to use specific fonts that may not be available system wide. This can be done by setting the environment variable GDFONTPATH and the PHP function putenv is good for this purpose.
However, putenv is also a security concern, and as such on my production servers, I have it listed on disable_functions in my php.ini and as a result, I can't set this environment var within my PHP script. Instead, setting it using SetEnv in my Apache vhost .conf file seems like a good idea (this is a cut-down example):
<Directory "/srv/www/example.com/public">
AllowOverride None
Require all granted
SetEnv GDFONTPATH "/srv/www/example.com/private/fonts"
</Directory>
The snag is, this doesn't work as [I] expected! It does set GDFONTPATH, but not locally, so I get the following results:
getenv('GDFONTPATH'); # Returns '/srv/www/example.com/private/fonts'
getenv('GDFONTPATH', true); # Returns ''
The libgd functions ignore this; they only look at the local environment variable that I only seem to be able to set using the putenv function. But enabling putenv on my production servers is not practical for security reasons, hence my really wanting to use SetEnv in my Apache config!
FWIW: I've coded a "pragmatic" work-around that will use PHP's getenv to read the path from Apache's SetEnv, and then pass a fully-qualified font-filename to gdlib. This works, but is less than ideal! I'll likely post this code for future reference, but I really would like a solution where I can set the environment from Apache. This post might offer a potential solution, but it looks like it would apply to all vhosts, so again, not ideal.
I'm a bit sketchy on the difference between environment variables and "local" environment variables.
References: libgd source regarding GDFONTPATH