JpGraph Error: HTTP headers have already been sent

点点圈 提交于 2019-12-02 05:17:12

问题


The problem is that the JpGraph is not displayed correctly on my web-page. The strange thing is that if I run the above code in isolation, then it works. But if I insert it in my main code, it fails producing the above-shown message. P.S. I'm using 'ob_start();', but it does not solve the problem.

// A new graph with automatic size
$graph = new GanttGraph (0,0, "auto");

//  A new activity on row '0'
$activity = new GanttBar (0,"Project", "2001-12-21", "2002-02-20");
$graph->Add( $activity);

// Display the Gantt chart
$graph->Stroke();
?> 
</div>

JpGraph Error: HTTP headers have already been sent.
Caused by output from file index.php at line 85.
Explanation:
HTTP headers have already been sent back to the browser indicating the data as text before the library got a chance to send it's image HTTP header to this browser. This makes it impossible for the library to send back image data to the browser (since that would be interpretated as text by the browser and show up as junk text).

Most likely you have some text in your script before the call to Graph::Stroke(). If this texts gets sent back to the browser the browser will assume that all data is plain text. Look for any text, even spaces and newlines, that might have been sent back to the browser.

For example it is a common mistake to leave a blank line before the opening "<?php".

回答1:


JpGraphs can't exist in files with html. They have to be in a pure php file. To get around this, I created a seperate file that generates the graph, and made the whole thing a function. At the end, change

$graph->Stroke();

to

$graph->Stroke(".<filepaht>.jpg");

Then, in your index.php page, reference the image file.

So, what it looks like you need is,

createjpgraph.php:

<?php 
function GenGraph (<input variables>) {

    // A new graph with automatic size        
    $graph = new GanttGraph (0,0, "auto");        

    //  A new activity on row '0'        
    $activity = new GanttBar (0,"Project", "2001-12-21", "2002-02-20");        
    $graph->Add( $activity);        

    // Display the Gantt chart        
    $graph->Stroke("./foler/file.jpg");
}
?> 

index.php:

...
<div>
...
<?php
include 'createjpgraph.php';
GenerateGraph(<variables>);
?>
<img src=\"./folder/file.jpg\" />
</div>

Hope this works for you.




回答2:


You can even do it in the same html document - first writing the graph to a file, then displaying it ...

//Code generating your graph here ...

// Finally output the image to a file
$graph->Stroke("/var/www/html/tmp/out.jpg");

//end of php code
?>

<!-- now include the newly generated image in the HTML -->
<img src="/tmp/out.jpg">

</body>
</html>



回答3:


It is seem that the jpeg file that is being created by the function cannot be over written in my case...

to do overwrite it.. I changed my JPGraph file gd_image.inc.php .. you have to comment out the line that says JpGraphError::RaiseL(25111,$aStrokeFileName)



来源:https://stackoverflow.com/questions/10487796/jpgraph-error-http-headers-have-already-been-sent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!