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_file
とis_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