首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony 2中的输出JPGraph

Symfony 2中的输出JPGraph
EN

Stack Overflow用户
提问于 2013-10-07 23:34:48
回答 1查看 2K关注 0票数 2

我试图让一个Symfony 2控制器从JPGraph生成一个图。我遇到的问题是如何让Symfony和JPGraph可靠地协同工作。

我做了我的控制器,我已经确定我进入了这个函数,但是我无法将图形输出到浏览器。我在使用图像/jpeg头时尝试了$graph->Stroke(),但结果是出现了一个空白页。我还尝试使用Twig并将图形对象传递给模板,并调用graph.Stroke,但由于图像没有出现,Twig似乎没有正确解析它(我在img上使用了基本64编码,但仍然没有生成图像)。

最后,我试过了

代码语言:javascript
复制
return $graph->Stroke()

代码语言:javascript
复制
return new Response($graph->Stroke());

但这两种情况也只是导致了一页空白。当我回到工作岗位的时候,我会提供任何人认为早上需要的任何信息,我只是希望没有消息来源,有人可以指导我如何让Symfony和JPGraph以我想要的方式进行交互。

更新:

这里是我试图让运行作为一个演示/学习练习,使两者一起工作的来源。

代码语言:javascript
复制
<?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;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-08 14:21:13

在工作期间,我设法找到了解决方案。它相当简单,允许对正在发生的事情进行大量控制,并将一切保持在Symfony框架内。

首先,它应该是

代码语言:javascript
复制
new \Graph(350, 250);

代码语言:javascript
复制
new \LinePlot();

没有这个\,Symfony认为它是其框架的一部分,而不是像我一样包含的库。

为了实际显示图像,除了修复上面的内容之外,我还必须在控制器中执行以下操作:

代码语言:javascript
复制
// 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;

然后在Twig里面:

代码语言:javascript
复制
<img src="data:image/png;base64, {{ EncodedImage }}" />
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19236592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档