Jump to content

[SOLVED] cannot modify header information


mishuk

Recommended Posts

Hi.  I found this pie chart script on here and am hoping to use it as the basis for what i need to achieve.  However i tried to apply it to my stylesheet to make sure it worked and i have this error

 

Warning: Cannot modify header information - headers already sent by (output started at...

can anyone suggest how to rectify the problem

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Careers Tracking System - Year 9 Reports</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
@import url("style.css");
-->
</style>
</head>
<body>
<div id ="container">
</div>
<div id ="menu">
</div>
<div id ="content">
<?php
$values = array(1, 1);    // values from your db

$im = imagecreate(450,250);
$bg = imagecolorallocate($im, 0, 0, 0);
$white = ImageColorAllocate ($im, 255, 255, 255);
$colors = array(
    imagecolorallocate($im, 0xFF, 0, 0),     //red
    imagecolorallocate($im, 0, 0xFF, 0),     //green
    imagecolorallocate($im, 0, 0, 0xFF)      //blue
);

$s_angle = 0;                          // pie slice start angle
$e_angle = 0;                          // pie slice end angle
$total = array_sum($values);
$k = count($values);
$cx = 225;
$cy = 125;                       // centre of the pie chart
for ($i=0; $i<$k; $i++)  {
    $a = $values[$i] / $total * 360;   // angle for this slice
    $e_angle += $a;
    imagefilledarc($im, $cx, $cy, 180, 180, $s_angle, $e_angle, $colors[$i], IMG_ARC_PIE);
    $s_angle += $a;                    // start angle for next slice
}
ImageString($im, 4, 30, 50, 'Male', $white);
ImageString($im, 4, 30, 70, 'Female', $white);
header ('content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
</div>
</body>
</html>

 

Cheers

Link to comment
Share on other sites

Headers are automatically sent the moment you start outputting anything to the browser.

 

Almost at the bottom of your code you have a statement trying to modify the header:

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

 

Now look at your error message:

Warning: Cannot modify header information - headers already sent

 

Putting 2 and 2 together gives...

Link to comment
Share on other sites

You cannot create a image and echo html at the same time. You have to have the image creation code in an external file and then use the img HTML tag to grab the generated image. Like so:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Careers Tracking System - Year 9 Reports</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
@import url("style.css");
-->
</style>
</head>
<body>
<div id ="container">
</div>
<div id ="menu">
</div>
<div id ="content">
<img src="image.php">
</div>
</body>
</html>

 

image.php

<?php
$values = array(1, 1);    // values from your db

$im = imagecreate(450,250);
$bg = imagecolorallocate($im, 0, 0, 0);
$white = ImageColorAllocate ($im, 255, 255, 255);
$colors = array(
    imagecolorallocate($im, 0xFF, 0, 0),     //red
    imagecolorallocate($im, 0, 0xFF, 0),     //green
    imagecolorallocate($im, 0, 0, 0xFF)      //blue
);

$s_angle = 0;                          // pie slice start angle
$e_angle = 0;                          // pie slice end angle
$total = array_sum($values);
$k = count($values);
$cx = 225;
$cy = 125;                       // centre of the pie chart
for ($i=0; $i<$k; $i++)  {
    $a = $values[$i] / $total * 360;   // angle for this slice
    $e_angle += $a;
    imagefilledarc($im, $cx, $cy, 180, 180, $s_angle, $e_angle, $colors[$i], IMG_ARC_PIE);
    $s_angle += $a;                    // start angle for next slice
}
ImageString($im, 4, 30, 50, 'Male', $white);
ImageString($im, 4, 30, 70, 'Female', $white);
header ('content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.