7

我正在使用这个脚本,它是 jpgraph 本身提供的示例之一。当我将它单独放在网页(空白)上时,它正在绘制图表。但是当我将代码嵌入到已经存在的网页中(带有一些内容)时,它并没有绘制图表。

GD 已根据 phpinfo() 启用。我正在使用 jpgraph 3.5.0b1。

4

6 回答 6

11

问题是您将 HTML/文本输出与图像输出混合在一起。

任何时候你有一个 PHP 脚本生成图形内容,你必须以不同于普通 HTML 或文本的方式处理输出。

有几条路线,我将在这里简要介绍它们。

将输出保存到文件并在 HTML 中使用该文件名

//replace this line:
// Display the graph
//$graph->Stroke();

// with these lines:

    // Default is PNG so use ".png" as suffix
    $fileName = "/tmp/imagefile.png";
    $graph->img->Stream($fileName);

..然后$filename在图像标签中使用,像这样(例如):

print '<img src="'.$filename.'" />';

创建一个将输出图形的独立 PHP 脚本

You can use the example script as-is, alone in a file called graph_render_script.php. Then, in your HTML, you use that script as a source:

<img src="graph_render_script.php" />

Output base-64 encoded data

Another route is to use base-64 encoded image data. This is relatively simple to do:

print '<img src="data:image/png;base64,'.base64_encode($graph->Stroke()).'" />';

As always, the documentation should be your guide!

Documentation

于 2011-09-06T17:51:16.923 回答
4

This worked for me:

putting the php code that generates the image in a file...Then on my html page I do this:

<img src="graph.php" > 
于 2013-02-28T14:19:53.910 回答
3

embedding the graph inline is indeed possible. You'll have to use output buffering to capture the image data, then base64 encode that data, then use that base64-encoded string as the source in an <img>.

Try something like this:

$img = $graph->Stroke(_IMG_HANDLER);

ob_start();
imagepng($img);
$imageData = ob_get_contents();
ob_end_clean();

?><html>
<head>
    <title>JpGraph Inline Image Example</title>
</head>
<body>
    <h1>JpGraph Inline Image Example</h1>
    <img src="data:image/png;base64,<?php echo(base64_encode($imageData)); ?>" />
</body>
</html>

ceejayoz made an excellent point in that this method is almost never what you want. I do not recommend embedding the image data like this unless you have a good reason to do so and understand the downsides, i.e. older browsers lack support for it, and the page size is dramatically increased (not only from the image data but the fact the image data is base64 encoded, which balloons the length). I've used this method in the field myself on a project last year, but it was only because the client refused to make a second request for the image.

于 2013-04-14T23:08:24.843 回答
1

但是当我将代码嵌入到已经存在的网页中(带有一些内容)时,它并没有绘制图表。

你不能这样做——你不能在页面中将图像输出为原始二进制数据。

您需要将生成图形的代码放在单独的文件中,并使用图像标签。

<img src="path/to/jpgraph/chart.php" />
于 2011-09-06T17:49:07.377 回答
1

图表需要在它自己的页面上,你不能嵌入它。它输出原始 JPG,您不需要发送其他内容,并且需要有正确的标题来告诉浏览器它是 JPG。例如,要嵌入图表,您将创建一个名为 stats.php 的不同页面,并在该页面上创建一个指向独立图表的图像标签。

<img src=graph.php>
于 2011-09-06T17:50:30.790 回答
0

I've had this problem many times, I've noticed it happens when you have require() or include() in your Chart's script and those scripts have Data Base connections or special configurations.

I've solved this problem separating the data retrieving and the Chart drawing, passing parameters to the script or using SESSIONS to get the values.

Example of Embed image Chart in your PHP or HTML file:

<img src="linear_graph_customer.php?values=1,2,3,4|1,2,3,4|1,2,3,4&title=CHART&width=500&height=300" width="500" height="300" class="img" />

Regards.

于 2015-12-17T18:53:37.677 回答