(PHP 4, PHP 5, PHP 7)
Imagettftext-writes text to an image with a TrueType font
Description
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.
<?php
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 ';
}
Workaround:
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
Or
$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.