On 4/4/2011 11:19 PM, jwcarlton wrote:
> I'm still working with imagettfbbox(). The problem I'm having now is
> that I can't seem to refer to the TTF file unless it's in the actual
> directory as the index.php file; if I give a relative or absolute
> path, I'm getting an error:
>
> Warning: imagettfbbox() [function.imagettfbbox]: Could not find/open
> font in /home/accountname/public_html/variables.php on line 152
>
> For this system, I have a variables.php file, located at the /
> public_html/ directory. This is the file that uses imagettfbbox(),
> like so:
>
> $home = "http://www.example.com";
>
> define("F_SIZE", 9);
>
> // This is the problem line, # 152 in the error
> define("F_FONT", "arialbd.ttf");
>
> function get_bbox($text) {
> return imagettfbbox(F_SIZE, 0, F_FONT, $text);
> }
>
>
> Now, if I'm loading index.php that's in the same directory as
> variables.php, then this works fine (obviously). In other directories,
> though, it (obviously) can't find the arialbd.ttf file.
>
> In all files, I'm including variables.php like so:
>
> include "../variables.php";
>
> I've tried the following variations, but still get the same errors:
>
> // Found this recommendation somewhere
> define("F_FONT", "./arialbd.ttf");
>
> // URLs are supposed to be OK
> define("F_FONT", "$home/arialbd.ttf");
>
> // Using no variables, just type the URL straight in
> define("F_FONT", "http://www.example.com/arialbd.ttf");
>
> // Thought that a relative path would at least work
> // on the second-level directories
> define("F_FONT", "../arialbd.ttf");
>
>
> Finally, I tried removing define(), and switching to global variables,
> but had the same error:
>
> $f_size = "9";
> $f_font = "$home/arialbd.ttf";
>
> function get_bbox($text) {
> global $f_size;
> global $f_font;
>
> return imagettfbbox($f_size, 0, $f_font, $text);
> }
>
>
> Can you guys tell me the correct way to define the path to arialbd.ttf
> here?
>
> TIA,
>
> Jason
Jason,
While theoretically it will work to access a font file this way, it will
only do so if the font file is accessible from the web; you won't be
able to place it in a directory outside of your web site (which is
really a better idea anyway). Additionally, when such an operation does
work, it requires an extra request to the web server, unnecessarily
increasing server load.
A much better way to access files on your server (and not just .ttf
files - it works for other files you wish to include, also) is to make
it relative to the root directory of your web site *on the file system*.
You could hard code this, but then you have to change it every time you
move the site to a new directory, change hosting companies, go from your
development machine to your production one, etc. Fortunately, there is
a value in the $_SERVER superglobal array which helps here.
Use $home=$_SERVER['DOCUMENT_ROOT'];
This will set $home to the root directory of your web server on the
disk. You can now access your file with:
$f_font = "$home/arialbd.ttf";
Or, even better, since this would not normally be accessed from the web
and if your web hosting company allows you to place files below the
websites root directory, you can place the file 1 level down, i.e. if
your web sites's home directory is /var/www/example/html, you could
place the file in /var/www/example and access it with
$f_font = "$home/../arialbd.ttf";
Or even in a subdirectory of it with
$f_font = "$home/../fonts/arialbd.ttf";
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|