Last time, I introduced the block diagram generation tool blockdiag to CentOS6.5.
This time, I will describe the tips when using it via a web server.
-If the blockdiag command is defined in the script, consider that it will be executed by the "apache" user / group.
-It is necessary to grant write permission for the apache user or group to the block diagram generation destination.
-When executing the blockdiag command, it is necessary to explicitly specify the font because it is executed by the apache user.
From the default documentroot
# mkdir /var/www/html/data
# chown .apache /var/www/html/data
# vi /var/www/html/index.php
--------------------
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>BlockDiag Online</h1>
<form action='index.php' method='post'>
<textarea name='text' cols='120' rows='20'>
blockdiag admin {
   // Set M17N text using label property.
   A [label = "Ki"];
   B [label = "Accepted"];
   C [label = "Turn"];
   D [label = "Conclusion"];
   A -> B -> C -> D;
   // Use M17N text directly (need to quote).
spring->summer->autumn->winter;
   // Use M17N text including symbol characters (need to quote).
   "Spring is dawn" -> "summer=Night" -> "autumn.evening" -> "winter&be making an effort";
}
</textarea>
<hr />
<input type='submit' value='conversion' />
</form>
<?php
  if (isset($_POST['text'])){
    //Define path etc.
    $dir = "data/";
    $font = "/usr/share/fonts/ipa-gothic/ipag.ttf";
    $filename = sprintf("svg%04d",mt_rand(0,9999));
    $fd = sprintf("./{$dir}%s.diag",$filename);
    $ff = sprintf("./{$dir}%s.svg" ,$filename);
    $text = $_POST['text'];
    
    //Create block diagram
    file_put_contents($fd,$text);
    
    //blockdiag command execution
    $command = sprintf("blockdiag -f {$font} -Tsvg %s",$ft);
    $Err = `$command`;
    printf("command: %s<br />",$command);
    print "<hr />";
    
    if (file_exists($ff)) {
      echo "$ff exists<br />";
      printf("ff: %s<br />",$ff);
      print "<img src=\"{$ff}\" />";
    } else {
      echo "<span style='color:#ff0000;'>$ff does not exist. Seems to have failed to generate<br />";
      echo "If successful, the following SVG file will be generated</span><br />";
      print "<img src=\"/cp_root/nippon.svg\" />";
    }
  }
?>
</body>
</html>
# chmod 755 /var/www/html/index.php
http://example.com/index.php


# ls -l /var/www/html/data/svg5038.svg
-rw-r--r--1 apache apache 7465 October 8 20:04 2014 /var/www/html/data/svg5038.svg
I think that the content has become a little useful for actual usage.
That's it.
Recommended Posts