画像に文字(テキスト)を書き込む

//ヘッダーでimg画像であることを宣言
header("Content-Type: image/png");
//新規イメージオブジェクトの生成
$img = imagecreate(300, 300);
//カラー情報を付与
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
//表示するテキストを用意
$text = mb_convert_encoding('Hello world!','UTF-8','auto');
//ttfファイルへのリンク(要ttfファイル)
$font = 'ipagp.ttf';
//リソース、フォントサイズ、アングル(回転)、x座標、y座標(フォントペースライン)、フォント、テキスト
imagettftext($img,24,0,5,30,$black,$font,$text);
//書き出し
imagepng($img);

※一番最初にコールしたimagecolorallocateが自動で背景色にセットされる

画像の回転

//ヘッダーでimg画像であることを宣言
header("Content-Type: image/png");
//外部から画像の読み込み
$img = imagecreatefrompng('sample.png');
//背景色を指定
$color = imagecolorallocate($img, 0, 0, 0);
//画像を回転(リソース、角度、背景色)
$newimg = imagerotate($img, 45, $color);
//書き出し
imagepng($newimg);