用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
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.