in Code

[CODE/PHP] JpGraph: How to output your graph as a base64 encoded image

Some times you just want to output the image created by your $graph object without having to create a separate .php script that would need to receive a bunch of parameters.

Here’s a function you can pass your $graph object right before the $graph->Stroke(); call

function graphInSrc($graph, $width, $height) {
  $img = $graph->Stroke(_IMG_HANDLER);
  ob_start();
  imagepng($img);
  $img_data = ob_get_contents();
  ob_end_clean();

  echo '<img width="'.$width.'" height="'.$height.'" src="data:image/png;base64,'.base64_encode($img_data).'"/>';
}

Write a Comment

Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.