效果:
<?php
/**
* graphviz api
*
* $Id api.php [email]i@annhe.net[/email] 2015-6-8 $
**/
$host = $_SERVER['HTTP_HOST'];
$siteurl = "http://" . $host . "/gv/";
$cht = "dot";
$chl = "graph {fontname=\"SimSun\";node[shape=box];a[label=\"nothing to do~\"];}";
$error = "error.png";
$engine = array("dot", "neato", "fdp", "sfdp", "twopi", "circo");
$imgtype = "png";
$imgtype_arr = array("png", "gif", "jpeg");
if(isset($_GET['cht'])) {
$cht = $_GET['cht'];
$arr = explode(':', $cht);
if(!array_key_exists("1", $arr)) {
$cht = "dot";
} else {
$cht = $arr['1'];
}
if(!in_array($cht, $engine)) {
$cht = "dot";
}
}
if(isset($_GET['chl'])) {
$chl = $_GET['chl'];
}
if(isset($_GET['chof'])) {
$imgtype = $_GET['chof'];
if(!in_array($imgtype, $imgtype_arr)) {
$imgtype = "png";
}
}
$gvname = md5($chl) . $cht;
$gvpath = "./gv/" . $gvname . ".gv";
$imgpath = "img/" . $gvname . "." . $imgtype;
$file = fopen("$gvpath", "w");
$encode = mb_detect_encoding($chl, array("ASCII","UTF-8","GB2312", "GBK", "EUC-CN"));
if($encode != "UTF-8") {
$chl = iconv("$encode", "UTF-8", $chl);
}
$chl = str_replace(">", ">", $chl);
$chl = str_replace("<", "<", $chl);
$chl = str_replace(""", "\"", $chl);
fwrite($file, "$chl");
fclose($file);
if(!file_exists($imgpath)) {
exec("$cht -T$imgtype $gvpath -o $imgpath",$out, $ret);
if($ret != 0) {
$imgpath = $error;
$imgtype = "png";
}
}
$imgstrout = "image$imgtype(imagecreatefrom$imgtype('$imgpath'));";
header("Content-Type: image/$imgtype; charset=UTF-8");
eval($imgstrout);
?>
|