###前提・実現したいこと
windows7でxamppをインストールしました。PHPのバージョンはPHP7です。
###発生している問題・エラーメッセージ
imagettftextを使い画像をブラウザに出力しようと思ってマニュアルの例1にある下のプログラムを実行しましたが、**画像ファイルhttp://localhost/hoge.phpは壊れているため、表示できませんでした。**と表示されてマニュアルの出力例のようにならないです。arial.ttfはphpファイルと同じ階層におきました。いったい何が違うのでしょうか?
PHP
1<?php 2// コンテントタイプを設定します 3header('Content-Type: image/png'); 4 5// 画像を生成します 6$im = imagecreatetruecolor(400, 30); 7 8// いくつかの色を生成します 9$white = imagecolorallocate($im, 255, 255, 255); 10$grey = imagecolorallocate($im, 128, 128, 128); 11$black = imagecolorallocate($im, 0, 0, 0); 12imagefilledrectangle($im, 0, 0, 399, 29, $white); 13 14// 描画する文字列 15$text = 'Testing...'; 16// フォント自身のパスでパスを置き換えます 17$font = 'arial.ttf'; 18 19// テキストに影を付けます 20imagettftext($im, 20, 0, 11, 21, $grey, $font, $text); 21 22// テキストを追加します 23imagettftext($im, 20, 0, 10, 20, $black, $font, $text); 24 25// imagepng() を使用して imagejpeg() よりもクリアなテキストにします 26imagepng($im); 27imagedestroy($im); 28?>
###ためしたこと
- headerの位置をimagepngの直前にする。
- headerの位置をimagedestroyの直後にする。
- headerの位置をプログラムの先頭にする。
- $fontのパスを絶対パス(
C:\Windows\Fonts\ARIALNB.TTF
とC:\xampp\htdocs\arial.ttf
)を指定する。 - $fontを
dirname(__FILE__) . 'arial.ttf'
もしくは__DIR__ . 'arial.ttf'
とする。 error_reporting(E_ALL)
でエラーが表示されないか確認する。phpinfo()
とgd_info()
でGDライブラリがインストールされているか確認する。
####phpinfo()の結果
####gd_info()の結果
PHP
1Array 2( 3 [GD Version] => bundled (2.1.0 compatible) 4 [FreeType Support] => 1 5 [FreeType Linkage] => with freetype 6 [GIF Read Support] => 1 7 [GIF Create Support] => 1 8 [JPEG Support] => 1 9 [PNG Support] => 1 10 [WBMP Support] => 1 11 [XPM Support] => 1 12 [XBM Support] => 1 13 [WebP Support] => 1 14 [JIS-mapped Japanese Font Support] => 15) 16
- filesize($font)の実行。
####filesize($font)の実行
以下のようにプログラムを編集して、filesize($font)を表示しました。
$fontのパスはC:\Windows\Fonts\ARIALNB.TTF
とC:\xampp\htdocs\arial.ttf
の両方を試しました。
実行するとどちらのパスを指定した場合も367112という文字が表示されました。
PHP
1<?php 2// コンテントタイプを設定します 3//header('Content-Type: image/png');//headerをコメントアウト 4 5// 画像を生成します 6$im = imagecreatetruecolor(400, 30); 7 8// いくつかの色を生成します 9$white = imagecolorallocate($im, 255, 255, 255); 10$grey = imagecolorallocate($im, 128, 128, 128); 11$black = imagecolorallocate($im, 0, 0, 0); 12imagefilledrectangle($im, 0, 0, 399, 29, $white); 13 14// 描画する文字列 15$text = 'Testing...'; 16// フォント自身のパスでパスを置き換えます 17$font = 'arial.ttf';//`C:\Windows\Fonts\ARIALNB.TTFとC:\xampp\htdocs\arial.ttfで試す。 18 19// テキストに影を付けます 20imagettftext($im, 20, 0, 11, 21, $grey, $font, $text); 21 22// テキストを追加します 23imagettftext($im, 20, 0, 10, 20, $black, $font, $text); 24 25echo filesize($font);//ファイルサイズ表示 26 27// imagepng() を使用して imagejpeg() よりもクリアなテキストにします 28//imagepng($im);//文字化けした文字が一緒に表示されてしまうのでコメントアウト。 29//imagedestroy($im); 30?> 31
- pngをファイルに保存する。
pngをファイルに保存するときは、以下のプログラムを使いました。
このプログラムを実行すると、phpファイルと同じ階層に120*20pxのtest.pngが作成されました。
PHP
1<?php 2$im = @imagecreatetruecolor(120, 20); 3imagepng($im, "test.png");
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/27 08:57
2016/12/27 09:54
2016/12/27 10:15
2016/12/27 10:23
2016/12/27 10:27
2016/12/27 10:41 編集
2016/12/27 10:51
2016/12/27 10:58
2016/12/27 11:06
2016/12/27 11:20
2016/12/27 11:41
2016/12/27 11:53 編集
2016/12/27 12:08
2016/12/27 12:41