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
https://forums.phpfreaks.com/topic/38341-solved-cannot-modify-header-information/
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...

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);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.