用php調用graphviz輕便畫拓撲圖
來源:互聯網
上載者:User
用php調用graphviz輕鬆畫拓撲圖
graphviz是一款古老的畫拓撲圖的工具,非常強大,能夠按照你在文字檔裡定義的格式轉換為拓撲圖,很多大公司都是用graphviz來畫拓撲圖,它的最主要的功能是用程式產生文本,然後調用graphviz來把文本轉化為拓撲圖。
1.安裝graphviz
到http://www.graphviz.org/Download..php裡下載對應的平台的安裝包;
我用的是centos因此用yum安裝最方便
wget http://www.graphviz.org/graphviz-rhel.repo /etc/yum.repos.d/graphviz-rhel.repo
yum install graphviz
2.安裝完畢後進行測試
echo “digraph G {Hello->World}” | dot -Tpng >hello.png
3.php 調用graphviz
首先用程式產生/tmp/domain.txt,然後用dot命令產生圖片
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header ("Pragma: no-cache"); // HTTP/1.0
header ("Content-type: image/gif");
$filename = '/tmp/domain.txt';
$somecontent = "digraph G {a->b->c->a}";
if (!$handle = fopen($filename, 'w')) {
echo "cannot open $filename";
exit;
}
if (fwrite($handle, $somecontent) === FALSE) {
echo "cannot write to $filename";
exit;
}
fclose($handle);
passthru("dot -Tpng $filename");
// passthru("cat $filename | dot -Tpng");
?>
end