LoginSignup
12
12

More than 5 years have passed since last update.

続:ブロック図生成ツール blockdiagを導入してみました

Last updated at Posted at 2014-10-08

■はじめに

前回、CentOS6.5にブロック図生成ツールblockdiagを導入しました。

今回は、webサーバ経由で利用した際のTipsを記載します。

■前提

※前提1: webサーバはapache
※前提2: apacheよりphpが使える状態となっていること

■概要

・スクリプト上にblockdiagコマンドを定義した場合、「apache」ユーザ/グループで実行されることを考慮

・ブロック図生成先にapacheユーザ or グループの書き込み権限を付与する必要あり

・blockdiagコマンド実行時、apacheユーザで実行されるため明示的にフォントを指定する必要あり

■出力先ディレクトリ

デフォルトの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 = "起"];
   B [label = "承"];
   C [label = "転"];
   D [label = "結"];

   A -> B -> C -> D;

   // Use M17N text directly (need to quote).
   春 -> 夏 -> 秋 -> 冬;

   // Use M17N text including symbol characters (need to quote).
   "春は 曙" -> "夏 = 夜" -> "秋.夕暮れ" -> "冬 & つとめて";
}
</textarea>
<hr />
<input type='submit' value='変換' />
</form>

<?php
  if (isset($_POST['text'])){
    // パスなどを定義
    $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'];

    // block図を作成
    file_put_contents($fd,$text);

    // blockdiagコマンド実行
    $command = sprintf("blockdiag -f {$font} -Tsvg %s",$ft);
    $Err = `$command`;
    printf("command: %s<br />",$command);
    print "<hr />";

    if (file_exists($ff)) {
      echo "$ff が存在します<br />";
      printf("ff: %s<br />",$ff);
      print "<img src=\"{$ff}\" />";
    } else {
      echo "<span style='color:#ff0000;'>$ff は存在しません。生成に失敗しているようです<br />";
      echo "成功すると以下のようなSVGファイルが生成されます</span><br />";
      print "<img src=\"/cp_root/nippon.svg\" />";
    }
  }
?>
</body>
</html>

実行権限付与

# chmod 755 /var/www/html/index.php

■ブラウザより実行

20141008_blockdiag_sample01.png


▽変換結果

20141008_blockdiag_sample02.png

■生成ファイルを確認

# ls -l /var/www/html/data/svg5038.svg
-rw-r--r-- 1 apache apache 7465 10月  8 20:04 2014 /var/www/html/data/svg5038.svg

少しは実際の利用用途に役立つ内容になったかと思います。

以上になります。

12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12