Heim >Backend-Entwicklung >PHP-Tutorial > 用php调用graphviz轻便画拓扑图
用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