PHP グラフ作成 画像処理 (GD)  その1

GDライブラリを使ってグラフ作成
<?php


 
//キャンバスサイズ
$width   = 820;
$height  = 620;
 

 
$image = imagecreatetruecolor($width, $height);
 
//線の色
$color = imagecolorallocate( $image, 255, 255, 255 );
$color1 = imagecolorallocate( $image, 128, 0, 255 );
imagesetthickness($image, 2);
imageline($image,10,10,800,10,$color);
imageline($image,10,10,800,10,$color);
imageline($image,10,10,10,600,$color);
imageline($image,10,600,800,600,$color);
imageline($image,800,10,800,600,$color);

imagesetthickness($image, 10);
imageline($image,800,10,10,600,$color1);

$font = "C:\Windows\Fonts\meiryo.ttc";
$text = 'test';
imagettftext($image, 10, 0, 80, 70, $color, $font, $text);
$text = 'test2';
imagettftext($image, 10, 0, 80, 100, $color, $font, $text);
 
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
 
?>

imagecreatetruecolor ピクセル単位で表示画像サイズ
imagecolorallocate 線の色
imagesetthickness($image, 2) 線のサイズ
imageline($image,X1,Y1,x2,Y2,$color);

テキストの表示
$font = “C:\Windows\Fonts\meiryo.ttc”; 使用フォント
imagettftext($image, 10, 0, 80, 70, $color, $font, $text);
        サイズ、回転、x、y

header(‘Content-Type: image/jpeg’);  出力タイプ
imagejpeg($image); JPG指定
imagedestroy($image); 画像破壊

次はHTMLに動的に組み込む場合

htmlに組み込み
<?php


 
//キャンバスサイズ
$width   = 820;
$height  = 620;
 
//キャンバスの中心を原点とする


 
$image = imagecreatetruecolor($width, $height);
 
//線の色
$color = imagecolorallocate( $image, 255, 255, 255 );
$color1 = imagecolorallocate( $image, 128, 0, 255 );
imagesetthickness($image, 2);
imageline($image,10,10,800,10,$color);
imageline($image,10,10,800,10,$color);
imageline($image,10,10,10,600,$color);
imageline($image,10,600,800,600,$color);
imageline($image,800,10,800,600,$color);

imagesetthickness($image, 10);
imageline($image,800,10,10,600,$color1);

$font = "C:\Windows\Fonts\meiryo.ttc";
$text = 'test';
imagettftext($image, 10, 0, 80, 70, $color, $font, $text);
$text = 'test2';
imagettftext($image, 10, 0, 80, 100, $color, $font, $text);
 
//header('Content-Type: image/jpeg');
imagejpeg($image,"msg.jpg");
imagedestroy($image);
 
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <img src="msg.jpg">
	<form action="upload.php" method="post" enctype="multipart/form-data">
  CSVファイル:<br />
  <input type="file" name="csvfile" size="30" /><br />
  <input type="submit" value="アップロード" />
</form>
  </body>
</html>

imagejpeg($image,”msg.jpg”); msg.jpgで出力
<img src=’msg.jpg’

カテゴリー php