관리 메뉴

nkdk의 세상

php에서 그래프를 그릴 때 사용하는 함수 본문

My Programing/PHP

php에서 그래프를 그릴 때 사용하는 함수

nkdk 2009. 7. 24. 13:59

php에서 그래프나 혹은 그림을 그릴 때 사용하는 간단한 함수들을 보려고 한다.
정말 간단하게만 볼려고 하는 것이기 때문에 자세한 것에 대해서는 인터넷을 참조하길 바란다.
그냥 이런 함수가 있다라는 것을 일단 확인하는 차원에서 하는 것이다.

graph_php1.php
<?php
$img = imagecreate(300,200);
$color1 = imagecolorallocate($img, 44, 4, 100);
$color2 = imagecolorallocate($img, 255, 255, 255);
$title = mb_convert_encoding("nkdk,nkdk", "UTF-8", "SJIS");
$font = "c:/windows/fonts/HGRPP1.TTC";

imagettftext($img, 22, 20, 80, 100, $color2, $font, $title);

header("Content-type: image/png");
imagepng($img);
imagedestory($img);

$img = imagecreate(300, 300);
$color1 = imagecolorallocate($img, 0, 0, 255);
$color2 = imagecolorallocate($img, 4, 255, 255);

imageline($img, 50, 250, 270, 250, $color2);
imageline($img, 50, 250, 50, 30, $color2);

imagefilledrectangle($img, 70, 250, 100, 50, $color2);
imagefilledrectangle($img, 120, 250, 100, 50, $color2);
imagefilledrectangle($img, 170, 250, 100, 50, $color2);
imagefilledrectangle($img, 220, 250, 100, 50, $color2);

header("Content-type: image/png");
imagepng($img);
imagedestory($img);
?>

graph_php2.php
<?php

$img = imagecreate(600, 300);
$color1 = imagecolorallocate($img, 0, 0, 255);
$color2 = imagecolorallocate($img, 4, 255, 255);

imageline($img, 50, 250, 270, 250, $color2);
imageline($img, 50, 250, 50, 30, $color2);

imagefilledrectangle($img, 70, 250, 100, 50, $color2);
imagefilledrectangle($img, 120, 250, 150, 80, $color2);
imagefilledrectangle($img, 170, 250, 200, 120, $color2);
imagefilledrectangle($img, 220, 250, 250, 220, $color2);

header("Content-type: image/png");
imagepng($img);
imagedestory($img);
?>

설명은 따로 하지 않고 각 함수들에 대해서는 인터넷을 참고하길 바란다. 필자의 경우는 간단한 방식은 이게 좋을수도 있다고 생각하지만, 그림 그리는 용도로는 다른 콤포넌트를 쓰는 것이 좋기에 많이 추천하지는 않는다.

다음으로는 include시키는 방법과 간단히 로그를 남기는 방법에 대해서 볼까 합니다.