龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php 画出一个简单的流程图

时间:2014-09-02 12:01来源:网络整理 作者:网络 点击:
分享到:
画出一个简单的流程图 [代码片段(75行)]
<?php
$shuZhu=array(array("经理1","经理2"),array("主管1","主管2","经理3"),array("组长","老大"));
$fontPosition='simhei.ttf';

class process
{
    var $arr;//流程确定的数组
    var $promoters;//流程发起人
    var $black;//所画节点颜色
    var $imgWidth;//图片宽度
    var $imgHeight;//图片长度
    var $font;//字体存放位置
    var $fontSize;
    function __construct($shuZhu,$fontP,$width=400,$height=300,$fontSize=10,$promoters="")
    {
        $this->arr=$shuZhu;
        $this->promoters=$promoters;
        $this->font = $fontP;
        $this->imgWidth=$width;
        $this->imgHeight=$height;
        $this->fontSize=$fontSize;
    }
    //point(x1,x2)是画图的起始点,w是横线的长,h是竖线的长,r是节点圆点的半径
    function show( $x,$y,$w,$h,$r)
    {
        //创建画布
        $image=imagecreatetruecolor($this->imgWidth,$this->imgHeight);
        //设置图像中所需的颜色
        $gray=imagecolorallocate($image,0xc0,0xC0,0xC0);
        $red=imagecolorallocate($image,0xFF,0x00,0x00);
        $black=imagecolorallocate($image,0x00,0x00,0x00);
        imagefill($image,0,0,$gray);

        imageline($image,$x,$y,$x+$w,$y,$red);//画第一条横线线
        imagefilledellipse($image, $x,$y, $r,$r, $black);//画出发起人的节点
        imagettftext($image,$this->fontSize, 0, $x-$w/4,$y+$w/2, $black, $this->font,$this->promoters);

        for($i=0;$i<count($this->arr);$i++)
        {
            $num=count($this->arr[$i]);//该级接点上有多少人
            if($num>1)//节点上的人数多于一人,则先画一条竖线
            {
                imageline($image,$x+$w*($i+1),$y-$h/2,$x+$w*($i+1),$y+$h/2,$red);   
            }
            for($j=0;$j<count($this->arr[$i]);$j++)
            {
                if($num>1)
                {
                    $m=$h/($num-1);//一个节点上有count[$arr[i]]个人,则$m代表截取竖线的长度
                    imageline($image,$x+$w*($i+1),$y-$h/2+$m*$j,$x+$w*($i+2),$y,$red);
                    imagefilledellipse($image, $x+$w*($i+1),$y-$h/2+$m*$j, $r,$r, $black);
                    //imagestring($image,2,$x+$w*($i+1)-10,$y-$h/2+$m*$j+5,$this->arr[$i][$j],$black);//直接输出接点上的人员名称
                    imagettftext($image, $this->fontSize, 0, $x+$w*($i+1)-$w/4,$y-$h/2+$m*$j+$w/2, $black, $this->font, $this->arr[$i][$j]);
                }
                else
                {
                    imageline($image,$x+$w*($i+1),$y,$x+$w*($i+2),$y,$red);//画一条横线
                    imagefilledellipse($image, $x+$w*($i+1),$y, $r,$r, $black);
                    //imagestring($image,2,$x+$w*($i+1),$y,$this->arr[$i][0],$black);//直接输出接点上的人员名称
                    imagettftext($image,$this->fontSize, 0, $x+$w*($i+1)-$w/4,$y+$w/2, $black, $this->font,$this->arr[$i][0]);
                }
            }

        }

        header('Content-type:image/png');
        imagepng($image);
        imagedestroy($image);
    }
}
$img=new process($shuZhu,$fontPosition,600,300,10,"董事长");
$img->show(10,150,50,200,8);
?>
//该片段来自于http://outofmemory.cn
精彩图集

赞助商链接