Вопрос

I'm attempting to have a Symfony 2 Controller generate a graph from JPGraph. The issue that I'm having is getting Symfony and JPGraph to work together reliably.

I made my controller, and I have made sure that I'm getting inside the function, but I'm unable to have the graph output to the browser. I've tried $graph->Stroke() while using the image/jpeg header, but it results in a blank page. I've also tried to use Twig and pass my graph object to the template, and call graph.Stroke, but it appears Twig doesn't parse this properly as the image doesn't appear (I used the base 64 encoding on my img src, and it still resulted in no image.)

Lastly, I've tried

return $graph->Stroke()

and

return new Response($graph->Stroke());

But both also just resulted in a blank page as well. I'll provide whatever source anyone deems required in the morning when I'm back at work, I simply was hoping that without the source, someone may be able to direct me to how to have Symfony and JPGraph both interact in the way I desire.

Update:

Here is the source that I'm attempting to get running as a demo/learning exercise to get the two working together.

<?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;
    }
}
Это было полезно?

Решение

While working on it, I managed to find the solution. It's fairly simple, and allows for a lot of control over what is going on and keeping everything inside the Symfony framework.

Firstly, it should have been

new \Graph(350, 250);

and

new \LinePlot();

Without the \, Symfony was thinking it was part of its framework and not an included library like I had.

To get the image to actually display, I had to do the following in the controller beyond fixing the above:

// 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;

And then inside Twig:

<img src="data:image/png;base64, {{ EncodedImage }}" />
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top