Linux hint Warning:imagettftext (): Could not find/open font error

Source: Internet
Author: User
Tags rand

(PHP 4, PHP 5, PHP 7)

Imagettftext-writes text to an image with a TrueType font


Array Imagettftext (Resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, Stri Ng $text)

Writes the specified text to the image using a TrueType font.

In many cases, fonts are placed in the same directory as the script. The following tips can mitigate the problems involved.

Set the enviroment variable for GD
Putenv (' gdfontpath= '. Realpath ('. '));

Name the font to is used (note the lack of the the. ttf extension)
$font = ' Somefont ';

A recent project does not show up in the Ubuntn Apache server, and is displayed correctly in the CentOS Apache server. Think is the problem of PHP configuration, but the opening of the GD library, the relevant services are normal, the display of the method of verification code alone to test, found the error

Warning:imagettftext (): Could not find/open font

Intercept some of the relevant code:

Class Checkcode {
Set the address of the font
Private $font;

function __construct () {
$rand = rand (0, 1);
if ($rand = = 0) {
$this->font = ' Elephant.ttf ';
} else {
$this->font = ' Vineta.ttf ';

* Generate text
Private Function Creat_font () {
$x = $this->width/$this->code_len;
for ($i = 0; $i < $this->code_len; $i + +) {
Imagettftext ($this->img, $this->font_size, Rand ( -30), $x * $i + rand (0, 5), $this->height/1.4, $this->fo Nt_color, $this->font, $this->code[$i]);
if ($i = = 0)
$this->x_start = $x * $i + 5;

is Imagettftext () This function error, can not find/can not open the font. There should be an error specifying the font file in the __construct () function.

if ($rand = = 0) {
$this->font = ' Elephant.ttf ';
} else {
$this->font = ' Vineta.ttf ';


Although the above code indicates that the font file is in the current directory, the UBUNTN Apache server still assigns a specific path to the font file.

$this->font = './elephant.ttf '; Relative path
$this->font = '/var/www/html/project/elephant.ttf '; Absolute path

So the verification code can be displayed properly.

From the above functional usage is not open font, that is, a very simple path problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.