Skip to content

Instantly share code, notes, and snippets.

@calpo
Created December 1, 2011 08:11
Show Gist options
  • Save calpo/1414886 to your computer and use it in GitHub Desktop.
Save calpo/1414886 to your computer and use it in GitHub Desktop.
文字列を画像で出力
<?php
// コンテントタイプを設定します
header('Content-type: image/png');
// 画像を生成します
$im = imagecreatetruecolor(600, 600);
// いくつかの色を生成します
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 599, 599, $white);
// 描画する文字列
$text = "あなた見える景色見たい
あなたまるで錦みたい
あなた見てた景色見たい 空は何色?
あなた目には見えぬ物の形 知ってた
今は過去に 過去は未来 未来が今で
忘れられた人の意志は忘れたくはない";
// フォント自身のパスでパスを置き換えます
$fonts = array(
'/home/username/tmp/aquafont.ttf',
'/usr/share/fonts/japanese/TrueType/sazanami-gothic.ttf',
'/usr/share/fonts/japanese/TrueType/sazanami-mincho.ttf'
);
$font = $fonts[((int)$_REQUEST['font'])];
// テキストに影を付けます
//imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// テキストを追加します
imagettftext($im, 14, 0, 10, 20, $black, $font, $text);
// imagepng() を使用して imagejpeg() よりもクリアなテキストにします
imagepng($im);
imagedestroy($im);
/*
//出力文字の取得
$setstring = 'これはてすとです';
//画像の作成
$img = imagecreate(200, 100);
//色の作成(背景色)
$backcol = imagecolorallocate($img, 160, 160, 160);
//背景色を塗る
imagefilledrectangle($img, 0, 0, 200, 80, $backcol);
//色の作成(文字)
$col = imagecolorallocate($img, 255, 255, 255);
//文字を書く
imagestring($img, 2, 10, 4, $setstring, $col);
//画像出力
header("Content-type: image/png");
header("Cache-control: no-cache");
imagepng($img);
//画像の消去(メモリの解放)
imagedestroy($img);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment