7

php.netの例を使用すると、警告が表示され、画像が正しくレンダリングされません。次のように .ttf ファイルへのフル パスを指定します。/var/www/public/myfont.ttf

PHP Warning:  imagettftext() [<a href='function.imagettftext'>function.imagettftext</a>]: Could not find/open font in <phpfile>

ここにあるカスタム .ttf フォントを使用しています。有効なフォント ファイルとして Ubuntu でファイルを開くことができます。他のフォントも試しましたが、結果は同じでした。

apache2、php5、freetype6、およびphp5-gdがインストールされたUbuntu 10.04 LTS 32ビットを使用しています。また、777 ファイルとフォルダーを ttf ファイルで chmod しようとしましたが、同じ結果になりました。

カスタム ttf フォント ファイルを使用してサンプルを動作させるにはどうすればよいですか?

*編集:私が使用しているコード:

<?php
// File is: /var/www/public/test.php
// Apart from $font variable, it's copy-pasted from php.net

// 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 = '/var/www/public/UnmaskedBB.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);
?>

phpinfo() からの出力;

[gd]
GD Support  enabled
GD Version  2.0
FreeType Support    enabled
FreeType Linkage    with freetype
FreeType Version    2.3.11
T1Lib Support   enabled
GIF Read Support    enabled
GIF Create Support  enabled
JPEG Support    enabled
libJPEG Version     6b
PNG Support     enabled
libPNG Version  1.2.42
WBMP Support    enabled 

テストis_fileis_readable:

$font = realpath('./').'/UnmaskedBB.ttf';
echo "Font: ".$font; // /var/www/public/UnmaskedBB.ttf
echo "Is file? ".is_file($font); // 1
echo "Is readable? ".is_readable($font); // 1
4

4 に答える 4

8

あなたは挿入しようとすることができます:

putenv('GDFONTPATH=' . realpath('.'));

最初の の前に、imagettftextパスの問題がないことを確認するだけです。

アップデート

のようなフォント キャッシュを使用している場合は、次のようfc-cacheに更新することを忘れないでください。

sudo fc-cache -f -v
于 2011-05-22T17:57:04.940 に答える
0

ttf ファイルを php ファイルと同じディレクトリに移動して、$fontを次のように変更してみてください。

$font = 'UnmaskedBB.ttf';
于 2011-05-22T17:38:11.880 に答える
0

すべてのphpパッケージを含むディストリビューションをアップグレードすると、問題が解決しました

于 2011-06-24T08:28:12.087 に答える