GD饼图图像不会显示

时间:2012-10-30 10:55:32

标签: php image image-processing gd

这些是我的代码:

index.php文件

<script>
        $(document).ready(function() {  
            $('#num_fields').change(function(){
               var num = $('#num_fields').val();                  

                var i = 0,j=0; 
                var html = ''; 
                for (i=1;i<=num;i++) {

                        html += 'Name: <input type="text" name="field-' + i + '" maxlength="25"/> Rating: <select name="rating-'+ i +'"><option value=50>1</option><option value=80>2</option><option value=110>3</option><option value=140>4</option><option value=170>5</option><option value=200>6</option><option value=230>7</option><option value=260>8</option><option value=290>9</option><option value=320>10</option></select><br/>'; 
                }
                $('#numFields').html(html);
            });
        }); 
</script>

<form name="input" method="post" action="generate.php">
            Select number of fields:      
                <select id="num_fields" name="num_fields">
                    <option selected>     SELECT   </option>
                    <?php for($i=6;$i<=10;$i++)
                        echo "<option value=$i>$i</option>";
                    ?>
                </select>
             <div id="numFields"></div><br/>
            <input type="submit" name="submit" value="Generate Wheel"/>
</form>

generate.php文件

<body>
    <div style="margin-top: 0px;">
        <a href="code here">Print This Image</a>
        <img src="image.php" alt="" />
    </div>
</body>

image.php文件

$num_fields = $_POST['num_fields'];
// create image
$image = imagecreate(500, 500);

// allocate some colors
$white    = imagecolorallocate($image, 255, 255, 255);
$pink     = imagecolorallocate($image, 255, 105, 180);
$red = imagecolorallocate($image, 255, 000, 000);
$green     = imagecolorallocate($image, 034, 139, 034);
$brown = imagecolorallocate($image, 139, 069, 019);
$yellow      = imagecolorallocate($image, 255, 255, 000);
$orange  = imagecolorallocate($image, 255, 140, 000);
$blue  = imagecolorallocate($image, 100, 149, 237);
$purple  = imagecolorallocate($image, 218, 112, 214);
$gray  = imagecolorallocate($image, 205, 205, 193);
$black    = imagecolorallocate($image, 000, 000, 000);
$cyan    = imagecolorallocate($image, 000, 255, 255);


//bg color//
imagefilledrectangle($image,0,0,500,500,$white);

$font = 'arial.ttf'; 

if($num_fields =='6')
{ 
// for each slices
imagefilledarc($image, 250, 250, $_POST['rating-1'], $_POST['rating-1'], 0, 60, $pink, IMG_ARC_EDGED);
imagefilledarc($image, 250, 250, $_POST['rating-2'], $_POST['rating-2'], 60, 120 , $green, IMG_ARC_EDGED);
imagefilledarc($image, 250, 250, $_POST['rating-3'], $_POST['rating-3'], 120, 180 , $red, IMG_ARC_EDGED);
imagefilledarc($image, 250, 250, $_POST['rating-4'], $_POST['rating-4'], 180, 240 , $gray, IMG_ARC_EDGED);
imagefilledarc($image, 250, 250, $_POST['rating-5'], $_POST['rating-5'], 240, 300 , $orange, IMG_ARC_EDGED);
imagefilledarc($image, 250, 250, $_POST['rating-6'], $_POST['rating-6'], 300, 360 , $blue, IMG_ARC_EDGED);


//outline
imagefilledarc($image, 250, 250, 400, 400, 0,360, $black, IMG_ARC_NOFILL); //outer circle
imagefilledarc($image, 250, 250, 320, 320, 0,360, $black, IMG_ARC_NOFILL); //inner circle
imageline($image, 250, 250, 450, 250, $black ); //line bet blue and pink 0 deg
imageline($image, 250, 250, 349, 78, $black ); //line bet orange and blue
imageline($image, 250, 250, 150, 78, $black ); //line bet orange and gray 
imageline($image, 250, 250, 50, 250, $black ); //line bet red and gray 
imageline($image, 250, 250, 151, 422, $black ); //line bet red and green 
imageline($image, 250, 250, 350, 422, $black ); //line bet pink and green 


//text legends 2
imagettftext($image, 9, 243, 425, 280, $black, $font, $_POST['field-1']);
imagettftext($image, 9, 183, 315, 415, $black, $font, $_POST['field-2']);
imagettftext($image, 9, 120, 130, 380, $black, $font, $_POST['field-3']);
imagettftext($image, 9, 60, 75, 210, $black, $font, $_POST['field-4']);
imagettftext($image, 9,0, 200, 80, $black, $font, $_POST['field-5']);
imagettftext($image, 9, 300, 378, 128, $black, $font, $_POST['field-6']);


// flush image
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);   
}

我得到的只是一张破碎的图像。请帮忙! :(我想要的是在渲染的饼图图像中有一个“打印此图像”选项,这是我知道将html代码放入图像的唯一方法。如果您有其他解决方案,请通过建议帮助我。谢谢!

编辑:我只想在生成图像的页面上有一个“打印”按钮。我搜索过它,人们说我不能在 header()之前放置任何HTML代码甚至是echo代码。如果我在 image.php 文件中的所有php代码之后放置html代码,它会显示图像图表,但html代码中的任何其他文本都不会显示。 :(

1 个答案:

答案 0 :(得分:0)

简单..由于以下原因,您无法看到PHP错误:

header('Content-type: image/png');

当您的帖子字段包含有效数字

时,您的代码可以正常工作

简单示例

$num_fields = 6;
for($i = 0; $i <= $num_fields; $i ++) {
    $_POST['rating-' . $i] = mt_rand(50, 200);
    $_POST['field-' . $i] = "Field $i";
}
// create image
$image = imagecreate(500, 500);

// allocate some colors
$white = imagecolorallocate($image, 255, 255, 255);
$pink = imagecolorallocate($image, 255, 105, 180);
$red = imagecolorallocate($image, 255, 000, 000);
$green = imagecolorallocate($image, 034, 139, 034);
$brown = imagecolorallocate($image, 139, 069, 019);
$yellow = imagecolorallocate($image, 255, 255, 000);
$orange = imagecolorallocate($image, 255, 140, 000);
$blue = imagecolorallocate($image, 100, 149, 237);
$purple = imagecolorallocate($image, 218, 112, 214);
$gray = imagecolorallocate($image, 205, 205, 193);
$black = imagecolorallocate($image, 000, 000, 000);
$cyan = imagecolorallocate($image, 000, 255, 255);

// bg color//
imagefilledrectangle($image, 0, 0, 500, 500, $white);

$font = 'verdana.ttf';

if ($num_fields == '6') {
    // for each slices
    imagefilledarc($image, 250, 250, $_POST['rating-1'], $_POST['rating-1'], 0, 60, $pink, IMG_ARC_EDGED);
    imagefilledarc($image, 250, 250, $_POST['rating-2'], $_POST['rating-2'], 60, 120, $green, IMG_ARC_EDGED);
    imagefilledarc($image, 250, 250, $_POST['rating-3'], $_POST['rating-3'], 120, 180, $red, IMG_ARC_EDGED);
    imagefilledarc($image, 250, 250, $_POST['rating-4'], $_POST['rating-4'], 180, 240, $gray, IMG_ARC_EDGED);
    imagefilledarc($image, 250, 250, $_POST['rating-5'], $_POST['rating-5'], 240, 300, $orange, IMG_ARC_EDGED);
    imagefilledarc($image, 250, 250, $_POST['rating-6'], $_POST['rating-6'], 300, 360, $blue, IMG_ARC_EDGED);

    // outline
    imagefilledarc($image, 250, 250, 400, 400, 0, 360, $black, IMG_ARC_NOFILL); // outer
                                                                                // circle
    imagefilledarc($image, 250, 250, 320, 320, 0, 360, $black, IMG_ARC_NOFILL); // inner
                                                                                // circle
    imageline($image, 250, 250, 450, 250, $black); // line bet blue and pink 0
                                                   // deg
    imageline($image, 250, 250, 349, 78, $black); // line bet orange and blue
    imageline($image, 250, 250, 150, 78, $black); // line bet orange and gray
    imageline($image, 250, 250, 50, 250, $black); // line bet red and gray
    imageline($image, 250, 250, 151, 422, $black); // line bet red and green
    imageline($image, 250, 250, 350, 422, $black); // line bet pink and green

    // text legends 2
    imagettftext($image, 9, 243, 425, 280, $black, $font, $_POST['field-1']);
    imagettftext($image, 9, 183, 315, 415, $black, $font, $_POST['field-2']);
    imagettftext($image, 9, 120, 130, 380, $black, $font, $_POST['field-3']);
    imagettftext($image, 9, 60, 75, 210, $black, $font, $_POST['field-4']);
    imagettftext($image, 9, 0, 200, 80, $black, $font, $_POST['field-5']);
    imagettftext($image, 9, 300, 378, 128, $black, $font, $_POST['field-6']);

    // flush image
    header('Content-type: image/png');
    imagepng($image);
    imagedestroy($image);
}

输出

enter image description here