Monday, February 6, 2017

How to use Graphviz with PHP on Ubuntu

On the previous post, i show you how to install and use graphviz on ubuntu, let's take it to another level, this article i will show you cool tricks using graphviz, php and apache web server.

For this tutorial we are going to need php and apache web server, if you haven't got those two, you can read my other post about installing apache web server and installing php on ubuntu.

Once you have all the ingredients, we can begin the tutorial.

Using graphviz with php on ubuntu
  • step 1, create a new folder on apache web server root directory
  • cd /var/www/html/
    sudo mkdir grap
  • step 2, give full permission to that new folder
  • sudo chmod 777 grap
  • step 3, create a php file inside the folder
  • gedit index.php
  • step 4, copy paste code below into the php file
  • <?php 
    $input = <<<DATA
    digraph G {
    main [label=""];
    main -> open -> read;
    main -> comment;
    main -> subscribe;
    blog -> awesome;
    blog -> nice;
    nice -> good;
    awesome -> good;
    good -> job;
    main -> nice;
    file_put_contents('', $input);
    exec('dot -Tpng -o graph.png'); 
    echo ('<img src="graph.png" />');

  • step 5, open browser and run the php file

  • http://localhost/grap/index.php

Pretty cool, huh?

No comments:

Post a Comment