ปกติแล้ว PHP เองก็สามารถวาดภาพได้ด้วย GD Library

What is gd?
gd is a graphics library. It allows your code to quickly draw images complete with lines, arcs, text, multiple colors, cut and paste from other images, and flood fills, and write out the result as a PNG or JPEG file. This is particularly useful in World Wide Web applications, where PNG and JPEG are two of the formats accepted for inline images by most browsers.

gd is not a paint program. If you are looking for a paint program, you are looking in the wrong place. If you are not a programmer, you are looking in the wrong place, unless you are installing a required library in order to run an application.

gd does not provide for every possible desirable graphics operation. It is not necessary or desirable for gd to become a kitchen-sink graphics package, but version 2.0 does include most frequently requested features, including both truecolor and palette images, resampling (smooth resizing of truecolor images) and so forth.


ซึ่ง GD Library เอง ได้รับความนิยมมาก โดยเฉพาะกลุ่ม opensource เราสามารถใช้ชุดคำสั่งในภาษาต่างๆ ซึ่งส่วนใหญ่จะมีให้ใช้แล้ว (build in) ดังนี้ PHP, PERL, OCaml, Tcl, Pascal, REXX และภาษาอื่นๆ อีก

จริงๆ เราสามารถใช้ GD ได้กับทุกๆ ภาษา โดยวิธีส่งไปให้โปรแกรมสร้างภาพ โดยผ่าน command line ก็ได้ ซึ่งโปรแกรมลักษณะนี้ ก็มีโปรแกรม fly ที่คุณ Martin Gleeson ได้พัฒนาขึ้นมา


การจะทำ Graph ด้วย PHP นั้น ก็ทำได้ไม่ยาก แค่วาดภาพกราฟฟิก ผู้พัฒนาอาจต้องมีความรู้เรื่องการคำนวณ การวาดภาพ เป็นต้น

ปัจจุบันมี script library ที่ช่วยในการวาดภาพหรือทำกราฟมากมาย ทั้งแบบ opensource และ commercial สำหรับ opensource เรามาดูกันว่า มีตัวไหนน่าสนใจบ้าง



JpGraph
user posted image
ผมคิดว่า JpGraph เป็น graph script ที่ดีที่สุด เท่าที่เห็นมา
user posted image

JpGraph Architecture
user posted image

ตัวอย่างการใช้งาน

$graph = new graph(150, 150, 'fig1.png', 0, false);
$graph->img->SetMargin(30, 30, 30, 30);
$graph->SetScale('textlin');
$line1 = new LinePlot($data);
$graph->Add($line1);
$graph->Stroke();


จะได้
user posted image


Introduction to JPGraph - Part I, Part II
Zend tutorial on how to use JpGraph

 



Libchart
user posted image
user posted image
user posted image
user posted image
ตัวอย่างการใช้งาน

 

include "libchart/libchart.php";

header("Content-type: image/png");

$chart = new VerticalChart(500,250);

$chart->addPoint(new Point("Jan 2005", 273));
$chart->addPoint(new Point("Feb 2005", 321));
$chart->addPoint(new Point("March 2005", 442));
$chart->addPoint(new Point("April 2005", 711));

$chart->setTitle("Monthly usage for www.example.com");
$chart->render();
?>





GDGraph 1.1
ว่าจะไม่เอามาลงอยู่แล้ว ไม่ได้มีจุดเด่นอะไรเท่าไหร่ สามารถสร้าง Bar Graph, Pie Graph และ Line Graph แต่ดูแล้วสู้ตัวอื่นไม่ได้
user posted image
user posted image
user posted image
user posted image


Links
GD Library
Libchart
jpgraph ออกภาษาไทยได้ไหม, คนไม่เคยใช้
อยากทราบวิธีทำ report ด้วย PHP ครับ
ลง PHP + GD Library บน Linux, ยังทำไม่สำเร็จ
วาดรูปให้เป็นแบบ dynamic