PHP GD库无法显示图片 - 饼图

时间:2016-02-25 11:46:46

标签: php gd

我有以下代码:

function show_pie_graph() {
    include 'config.php';

    $values = $GLOBALS['value_array'];
    $image_array = array();
    $full_width         = $PIE_GRAPH_CONFIG['width']; 
    $full_height        = $PIE_GRAPH_CONFIG['height']; 
    $size               = $PIE_GRAPH_CONFIG['size'];        


    for($j=0; $j<1;$j++) {
        $years = $values[$j];

        $column_values = array('A'  => $years['result_A'],
                               'B'  => $years['result_B'],
                               'C'  => $years['result_C'],
                               'D'  => $years['result_D'],
                               'E'  => $years['result_E'],
                               'FX' => $years['result_FX'],
                               'FN' => $years['result_FN']);

        $num=count($column_values);

        $percents;
        $angles;
        $angles[0]=0;
        $percents[0]=NULL;
        $sum_values = $column_values['A'];
        $sum_values += $column_values['B'];
        $sum_values += $column_values['C'];
        $sum_values += $column_values['D'];
        $sum_values += $column_values['E'];
        $sum_values += $column_values['FX'];
        $sum_values += $column_values['FN'];

        $percents[1] = (int)round($column_values['A']/$sum_values*100);
        $percents[2] = $percents[1]+(int)round($column_values['B']/$sum_values*100);
        $percents[3] = $percents[2]+(int)round($column_values['C']/$sum_values*100);
        $percents[4] = $percents[3]+(int)round($column_values['D']/$sum_values*100);
        $percents[5] = $percents[4]+(int)round($column_values['E']/$sum_values*100);
        $percents[6] = $percents[5]+(int)round($column_values['FX']/$sum_values*100);
        $percents[7] = $percents[6]+(int)round($column_values['FN']/$sum_values*100);


        //load percents array.  First value is NULL
        for ($c=1;$c<=$num;$c++)
        {
            $angles[$c]=(int)round((($percents[$c])/100)*360);
        }

        $angles[$num]=360;

        //create image
        $half=round($size/2);
        $image = imagecreatetruecolor($size, $size);

        // colors
        $colorR=array(0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x74, 0x33, 0xFF, 0x33, 0x66);
        $colorG=array(0x00, 0x80, 0x00, 0xA5, 0xFF, 0xFF, 0x99, 0x00, 0x33, 0xFF);
        $colorB=array(0xFF, 0x00, 0x00, 0x00, 0x00, 0xD4, 0xFF, 0x66, 0x99, 0x33);

        $white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
        $black = imagecolorallocate($image, 0x00, 0x00, 0x00);

        for($i=1;$i<count($angles);$i++) {
            imagefilledarc($image, 50, 50, 100, 50,  $angles[$i-1],  $angles[$i], imagecolorallocate($image,$colorR[$i],$colorG[$i],$colorB[$i]), IMG_ARC_PIE);
        }
    }
    // flush image
    header('Content-type: image/png');
    imagepng($image);
    imagedestroy($image);
}

百分比是权利(35,54,77,89,98,98,100)和角度(126,194,277,320,353,353,360),但是当我运行脚本时,我看不到任何东西。 PHP脚本不返回任何错误。哪里可以成为问题?

1 个答案:

答案 0 :(得分:1)

问题在于你的百分比是353两倍: - )

将第二个更改为354,然后重试。如果您使用这样的代码:

#!/usr/local/bin/php -f
<?php
        $size=100;
        $image = imagecreatetruecolor($size, $size);

        // colors
        $colorR=array(0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x74, 0x33, 0xFF, 0x33, 0x66);
        $colorG=array(0x00, 0x80, 0x00, 0xA5, 0xFF, 0xFF, 0x99, 0x00, 0x33, 0xFF);
        $colorB=array(0xFF, 0x00, 0x00, 0x00, 0x00, 0xD4, 0xFF, 0x66, 0x99, 0x33);

        $white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
        $black = imagecolorallocate($image, 0x00, 0x00, 0x00);
        $angles[0]=0;
        $angles[1]=126;
        $angles[2]=194;
        $angles[3]=277;
        $angles[4]=320;
        $angles[5]=353;
        $angles[6]=354;
        $angles[7]=360;

        for($i=1;$i<count($angles);$i++) {
            $r=imagefilledarc($image, 50, 50, 100, 100,  $angles[$i-1],  $angles[$i], imagecolorallocate($image,$colorR[$i-1],$colorG[$i-1],$colorB[$i-1]), IMG_ARC_PIE);
        }

    imagepng($image,"result.png");
    imagedestroy($image);
?>

你会得到这个:

enter image description here