Issue base64_encode with JPGraph

266 views
Skip to first unread message

Binh GIIK

unread,
Jan 24, 2019, 4:24:50 PM1/24/19
to JpGraph
Hi all,
I've used JPGraph for 8 years with php5 -7. Have you run into this issue with JPGraph + PHP buffer before, please help me out. Any help is appreciated.

<?php
$graph = new Graph( 500,500, 'auto' );
... all graph settings here
$graph->Add( $curveLine );

@ob_clean();
ob_start();

$graph->Stroke(); //show .png image on browser, good

$chart_image = base64_encode( ob_get_contents() );
ob_end_clean();
echo $chart_image; //show error "cannot be displayed because it contains errors" , doesn't echo a base64 string to browser, why???

------
JPGraph 4.0.2
PHP 7.0.28

David Martin

unread,
Jan 25, 2019, 6:05:04 AM1/25/19
to JpGraph
Have those last 3 lines been working all along and just stopped working? If not what was changed?

Claude Deslandes

unread,
Jan 28, 2019, 11:10:30 PM1/28/19
to JpGraph
Good day Binh,

   You have my full attention on your code.  I have been using Jpgraph since version 1.2 (14 years) and I have been searching to include an image generated by jpgraph in an email.

   Waiting for the answer.

David Martin

unread,
Jan 29, 2019, 7:37:01 AM1/29/19
to JpGraph
Binh, to send a base64 encoded image to the browser you have to let the browser know what is coming using the proper headers. Using what you posted the following code should work as you expect it to and display your graph.

@ob_clean();
ob_start
();
$graph
->Stroke();
$chart_image
= base64_encode(ob_get_contents());
ob_end_clean
();
header
("Content-type: image/png");
echo base64_decode
($chart_image);

Claude, to be able to send a graph as an e-mail attachment you need to save it as a temporary file while you build your mail then add it as an attachment and finally delete it. If you can instead send a PDF as an attachment with the graph the JPGraph manual has an example of including a graph directly into a PDF. Regardless of how you do it I'd highly recommend using something like PHPMailer which sending mail and attachments super easy.

Reply all
Reply to author
Forward
0 new messages