using truetype fonts in gd

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
blaineca
-


Joined: 05 Dec 2018
Posts: 14

PostPosted: Fri Feb 19, 2021 6:42 am    Post subject: using truetype fonts in gd Reply with quote

it seems I am unable to display truetype fonts.

Using all samples and also other scripts people have written. I can not display them. Is there some additional I need to do to the prepackaged PHP?

I see there is a new version of GD. Is there a way to upgrade it?
Back to top View user's profile Send private message
admin
Site Admin


Joined: 03 Mar 2002
Posts: 1295

PostPosted: Sat Feb 20, 2021 8:58 pm    Post subject: Re: using truetype fonts in gd Reply with quote

blaineca,

Without any context, it is difficult to give you a detailed answer.

Anyway, GD functions related to TrueType fonts mention that there is a need for specifying the path where fonts reside. This could be done using a global or local definition of the environment variable GDFONTPATH:

https://www.php.net/manual/en/function.imagettftext.php
_________________
Follow @abyssws on Twitter
Subscribe to our newsletter
_________________
Forum Administrator
Aprelium - https://aprelium.com
Back to top View user's profile Send private message
blaineca
-


Joined: 05 Dec 2018
Posts: 14

PostPosted: Sun Feb 21, 2021 6:04 pm    Post subject: Reply with quote

here is a test script I am trying to get working.

Code:
<?php
// Set the content-type
header('Content-Type: image/png');

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
Back to top View user's profile Send private message
admin
Site Admin


Joined: 03 Mar 2002
Posts: 1295

PostPosted: Mon Feb 22, 2021 10:10 pm    Post subject: Reply with quote

The script reports errors because the function imagettftext() is unable to locate the font. Here are the reported errors:

Code:
<b>Warning</b>:  imagettftext(): Could not find/open font in
<b>/Users/test/testphp/fonttest.php</b> on line <b>20</b><br /> <br />
<b>Warning</b>:  imagettftext(): Could not find/open font in
<b>/Users/test/testphp/fonttest.php</b> on line <b>23</b><br />


A quick solution is to copy arial.ttf inside the local working directory (usually it is the directory where the file fonttest.php is located.)

A better solution, as suggested in the PHP documentation we cited, is to declare a GDFONTPATH environment variable containing the full path of a directory containing the TTF fonts (such as c:\windows\fonts).

This can be done by declaring the environment variable in Windows.

A better solution would be declaring that same environment variable in the console of Abyss Web Server: in the host, select "Scripting Parameter", and add GDFONTPATH in "Custom Environment Variables".
_________________
Follow @abyssws on Twitter
Subscribe to our newsletter
_________________
Forum Administrator
Aprelium - https://aprelium.com
Back to top View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group