Php 在Symfony 2中输出JPGraph
我正试图让symfony2控制器从JPGraph生成一个图形。我遇到的问题是让Symfony和JPGraph可靠地协同工作 我制作了我的控制器,并且我确保我进入了函数,但是我无法将图形输出到浏览器。我在使用image/jpeg标题时尝试了$graph->Stroke(),但结果是一个空白页面。我还尝试使用Twig并将我的graph对象传递给模板,并调用graph.Stroke,但Twig似乎没有正确解析它,因为图像没有出现(我在img src上使用了base 64编码,结果仍然没有图像) 最后,我试过了Php 在Symfony 2中输出JPGraph,php,symfony,jpgraph,Php,Symfony,Jpgraph,我正试图让symfony2控制器从JPGraph生成一个图形。我遇到的问题是让Symfony和JPGraph可靠地协同工作 我制作了我的控制器,并且我确保我进入了函数,但是我无法将图形输出到浏览器。我在使用image/jpeg标题时尝试了$graph->Stroke(),但结果是一个空白页面。我还尝试使用Twig并将我的graph对象传递给模板,并调用graph.Stroke,但Twig似乎没有正确解析它,因为图像没有出现(我在img src上使用了base 64编码,结果仍然没有图像) 最后,
return $graph->Stroke()
及
但这两种方法都只会产生一个空白页。我会在早上回来工作时提供任何人认为需要的信息源,我只是希望没有这些信息源,有人可以指导我如何让Symfony和JPGraph以我想要的方式进行交互
更新:
这是我试图运行的源代码,作为一个演示/学习练习,以使两者协同工作
<?php //
namespace Bundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
$JPGraphSrc = 'JPGraph/src';
require_once ($JPGraphSrc.'/jpgraph.php');
require_once ($JPGraphSrc.'/jpgraph_line.php');
require_once ($JPGraphSrc.'/jpgraph_bar.php');
require_once ($JPGraphSrc.'/jpgraph_date.php');
class GraphingController extends Controller
{
public function createGraphAction(Request $request) {
$this->getResponse()->setContent('image/jpeg');
// Some data
$ydata = array(11,3,8,12,5,1,9,13,5,7);
// Create the graph. These two calls are always required
$graph = new Graph(350,250);
$graph->SetScale('textlin');
// Create the linear plot
$lineplot=new LinePlot($ydata);
$lineplot->SetColor('blue');
// Add the plot to the graph
$graph->Add($lineplot);
// Display the graph
$graph->Stroke();
return sfView::NONE;
}
}
在处理过程中,我设法找到了解决方案。它相当简单,允许对正在发生的事情进行大量控制,并将所有内容都保留在Symfony框架内
首先,它应该是
new \Graph(350, 250);
及
没有\,Symfony认为它是其框架的一部分,而不是像我一样的包含库
为了让图像真正显示出来,除了修复上述问题外,我还必须在控制器中执行以下操作:
// Display the graph
$gdImgHandler = $graph->Stroke(_IMG_HANDLER);
//Start buffering
ob_start();
//Print the data stream to the buffer
$graph->img->Stream();
//Get the conents of the buffer
$image_data = ob_get_contents();
//Stop the buffer/clear it.
ob_end_clean();
//Set the variable equal to the base 64 encoded value of the stream.
//This gets passed to the browser and displayed.
$image = base64_encode($image_data);
$redirect = $this->render('Bundle:Folder:file.html.twig', array(
'EncodedImage' => $image,
));
return $redirect;
然后在树枝里面:
<img src="data:image/png;base64, {{ EncodedImage }}" />
// Display the graph
$gdImgHandler = $graph->Stroke(_IMG_HANDLER);
//Start buffering
ob_start();
//Print the data stream to the buffer
$graph->img->Stream();
//Get the conents of the buffer
$image_data = ob_get_contents();
//Stop the buffer/clear it.
ob_end_clean();
//Set the variable equal to the base 64 encoded value of the stream.
//This gets passed to the browser and displayed.
$image = base64_encode($image_data);
$redirect = $this->render('Bundle:Folder:file.html.twig', array(
'EncodedImage' => $image,
));
return $redirect;
<img src="data:image/png;base64, {{ EncodedImage }}" />