Home » Php » php – How to Save Google Charts to the Server

php – How to Save Google Charts to the Server

Posted by: admin July 12, 2020 Leave a comment

Questions:

Background:
We’re using Google Charts to create graphs of some data generated by our web app. The user creates a report and then emails that report. Trouble is, once the user opens the report in Microsoft Word, that program exhibits some odd behavior regarding the dynamically generated images.

So, using PHP, we want to save our dynamically generated charts on the server, because Word can handle simple linked images without any problems.

I’m kind of at a loss on how to proceed. Ideas?

How to&Answers:
<?
$imageData = file_get_contents('http://chart.apis.google.com/chart... etc');

// Attach image data as attachment to an email
//OR: 

file_put_contents('/path/to/save/image.png',$imageData);
?>

Answer:

The simplest way is probably to use something like curl to retrieve the image from Google and write it out to a file on your server. You could also just use fopen and related functions, if you turn on the allow_url_fopen option.

Answer:

you can use curl to fetch image and save it on the server:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://url.to.chart/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FILE, '/path/to/file');
curl_exec($ch);
curl_close($ch);

Answer:

Smart solution,
See here! Very very very simple
http://www.battlehorse.net/page/topics/charts/save_google_charts_as_image.html

Answer:

Just fetch the image using the URL your webfrontend is generating, then adjust the code which is generating the reports to include the images stored locally instead of the Google Chart URLs.

Any other problem?