Rendering a .dot graph to a Web Page using PHP in Windows


I'm new to Graphviz (2.38) and newer to PHP (5.5.13).

I have dot scripts files that I use to produce graphs via the command line in Windows.

I'd like to deliver this ability via a web-site for my colleagues to access. I've gotten as far as storing my dot script into a PHP array variable.

I'm not sure what comes next to get PHP to execute "dot.exe" and return the results to the web page. Does anyone have any advice or a simple example?

Many many thanks in advance.


(I hope this wasn't a dual posting. The first attempt hasn't shown up; this is my second attempt.)

This PHP script displays an SVG


    $graph = <<<DOT
strict digraph demo
    start    [color = red];
    finish    [color = green];

    start -> finish;
    $file_name = 'temp/demo.gv';
    $fp        = fopen($file_name, 'w');

    fwrite($fp, $graph);

    $command = "dot -Tsvg $file_name";
    $output  = array();
    $result  = 0;
    $svg     = exec($command, $output, $result);

    # Nope:
    #echo print_r($output);

    foreach ($output as $key => $value)
        echo "$value\n";

The way i do it is: 1- PHP

The way i do it is:

1- PHP runs graphviz with command line parameters that cause it to generate a file (PNG, SVG etc etc).

2- Reference that graph file in the html part of your code:

<img src="<?php echo $imagename; ?>" alt="blah blah" />

Where imagename is defined as a variable in PHP: $imagename =

Maybe not the most elegant but it's low tech, works and is easy to troubleshoot.


Recent comments