Hello,
in process.php, I am trying to display google chart by requesting it using streams, the follwoing code is for requesting the chart:

header('content-type: image/png');
  $url = '' . md5(uniqid(rand(), true));
  $chd = 't:';
  for ($i = 0; $i < 150; ++$i) {
    $data = rand(0, 100000);
    $chd .= $data . ',';
  }
  $chd = substr($chd, 0, -1);

  // Add data, chart type, chart size, and scale to params.
  $chart = array(
    'cht' => 'lc',
    'chs' => '600x200',
    'chds' => '0,100000',
    'chd' => $chd);

  // Send the request, and print out the returned bytes.
$context = stream_context_create(
    array('http' => array(
      'method' => 'POST',
      'content' => http_build_query($chart))));
$fp=fopen($url, 'r', false, $context);

How can I save the chart as .png inside a file called images then pass the name of the file back to index.php.

Thanks.

Dani AI

Generated

A short diagnosis and a simple, reliable fix that builds on the thread

opened the chart stream with a context and ended up calling file_put_contents('images/imgs.png', $fp) — that often produces a corrupt file because $fp is a stream resource, not raw bytes. file_put_contents() can accept a stream resource and will copy the remaining buffer, but it is safer and clearer to read the stream explicitly (for example with stream_get_contents() or stream_copy_to_stream()) and to make sure the images folder exists and is writable. (php.net)

A more robust approach (and memory-friendly) is to let cURL stream the response directly to disk instead of loading it into a PHP string (this also avoids the fwrite/fclose dance shown earlier by ). Example pattern:

$url = 'https://chart.googleapis.com/chart?cht=lc&chs=600x200&chd=t:...';
$fp = fopen(__DIR__ . '/images/chart.png', 'wb');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

Remember to check curl_errno() / curl_error() and ensure filesystem permissions. Streaming with CURLOPT_FILE is the recommended pattern for downloading images. (php.net)

A final note about the source: the Google Image Charts endpoint was deprecated (April 20, 2012) and can be unreliable long-term; consider migrating to a maintained image-chart service such as QuickChart or render charts client-side and capture them server-side if you need stable, production-ready images. Common ways to hand the filename back to index.php are: redirect with a query parameter, return JSON (AJAX), or store the generated filename in session or a short-lived DB row. (bpcharts-na.morningstar.com)

Recommended Answers

All 9 Replies

Hmm.. Are you trying to save random charts? Why?

$url = '' . md5(uniqid(rand(), true));

Well I am just testing trying to undersatnd how to get a google chart and save it as .png inside a folder, I tried this but I got invalid image saved, I run my code on wamp

file_put_contents('images/imgs.png',$fp);

First you need a valid chart id, otherwise, even if you match one, it could be private and you won't be able to save it.

Then check this thread: http://www.daniweb.com/web-development/php/threads/256243
The solutions proposed there may save time for you, bye.

still not working :(

any ideas how to get the image from the file pointer and save it?

if not with stream, dose any one have a n idea of saving an image using curl?

still not working

no

try:

function save_image($img,$fullpath){
    $ch = curl_init ($img);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $rawdata=curl_exec($ch);
    curl_close ($ch);
    if(file_exists($fullpath)){
        unlink($fullpath);
    }
    $fp = fopen($fullpath,'x');
    fwrite($fp, $rawdata);
    fclose($fp);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.