php 如何在网页中嵌入图形(jpgraph)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7323976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to embed a graph (jpgraph) in a web-page
提问by Nitin Venkatesh
I am using thisscript which is one of the examples provided by jpgraph itself. When I put this on a web-page (blank) by itself, it's drawing the graph. But when I embed the code in already existing web-page (with some content), it ain't drawing a graph.
我正在使用这个脚本,它是 jpgraph 本身提供的示例之一。当我将其单独放在网页(空白)上时,它正在绘制图形。但是当我将代码嵌入到已经存在的网页(带有一些内容)中时,它并没有绘制图形。
GD is already enabled according to phpinfo(). Iam using jpgraph 3.5.0b1.
根据 phpinfo() 已经启用了 GD。我正在使用 jpgraph 3.5.0b1。
回答by Chris Baker
The problem is that you are mixing HTML/text output with image output.
问题是您将 HTML/文本输出与图像输出混合在一起。
Any time you have a PHP script generate graphical content you have to handle the output differently than normal HTML or text.
任何时候您让 PHP 脚本生成图形内容时,您都必须以不同于普通 HTML 或文本的方式处理输出。
There are a few routes, I'll cover them briefly here.
有几条路线,我将在这里简要介绍。
Save the output to a file and use that filename in your 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);
.. then use $filename
in an image tag, like this (for example):
.. 然后$filename
在图像标签中使用,像这样(例如):
print '<img src="'.$filename.'" />';
print '<img src="'.$filename.'" />';
Create a standalone PHP script that will output the graphic
创建将输出图形的独立 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:
您可以按原样使用示例脚本,单独在名为graph_render_script.php
. 然后,在您的 HTML 中,您使用该脚本作为源:
<img src="graph_render_script.php" />
Output base-64 encoded data
输出 base-64 编码数据
Another route is to use base-64 encoded image data. This is relatively simple to do:
另一种方法是使用 base-64 编码的图像数据。这样做相对简单:
print '<img src="data:image/png;base64,'.base64_encode($graph->Stroke()).'" />';
print '<img src="data:image/png;base64,'.base64_encode($graph->Stroke()).'" />';
As always, the documentation should be your guide!
与往常一样,文档应该是您的指南!
Documentation
文档
回答by gio
This worked for me:
这对我有用:
putting the php code that generates the image in a file...Then on my html page I do this:
将生成图像的 php 代码放在一个文件中......然后在我的 html 页面上我这样做:
<img src="graph.php" >
回答by The Awnry Bear
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>
.
嵌入图内联确实是可能的。您必须使用输出缓冲来捕获图像数据,然后对该数据进行 base64 编码,然后使用该 base64 编码的字符串作为<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.
ceejayoz 提出了一个很好的观点,因为这种方法几乎从来都不是你想要的。我不建议像这样嵌入图像数据,除非您有充分的理由这样做并了解其缺点,即旧浏览器缺乏对它的支持,并且页面大小急剧增加(不仅来自图像数据,而且事实上图像数据采用 base64 编码,从而增加了长度)。去年我在一个项目中自己在现场使用了这种方法,但这只是因为客户拒绝对图像进行第二次请求。
回答by ceejayoz
But when I embed the code in already existing web-page (with some content), it ain't drawing a graph.
但是当我将代码嵌入到已经存在的网页(带有一些内容)中时,它并没有绘制图形。
You can't do that - you can't output an image as raw binary data within a page.
您不能这样做 - 您不能在页面内将图像输出为原始二进制数据。
You need to put the code that generates the graph in a separate file, and use an image tag.
您需要将生成图形的代码放在单独的文件中,并使用图像标记。
<img src="path/to/jpgraph/chart.php" />
回答by profitphp
The graph needs to be on its own page, you can't embed it. It outputs a raw JPG and you need to have no other content sent and have the proper headers to tell the browser it's a JPG. To embed the graph you'd make a different page called stats.php for example, and on that page you'd make an image tag pointing to the stand alone graph.
图表需要在它自己的页面上,你不能嵌入它。它输出一个原始 JPG,你不需要发送其他内容,并有正确的标题告诉浏览器它是一个 JPG。例如,要嵌入图形,您需要创建一个名为 stats.php 的不同页面,并在该页面上创建一个指向独立图形的图像标记。
<img src=graph.php>
回答by RGA
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.
我已经多次遇到这个问题,我注意到它发生在您拥有require()
或include()
在您的图表脚本中并且这些脚本具有数据库连接或特殊配置时。
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.
我已经解决了这个问题,将数据检索和图表绘制分开,将参数传递给脚本或使用 SESSIONS 获取值。
Example of Embed image Chart in your PHP or HTML file:
在 PHP 或 HTML 文件中嵌入图像图表的示例:
<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.
问候。