0

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 = 'https://chart.googleapis.com/chart?chid=' . 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.

4
Contributors
9
Replies
10
Views
5 Years
Discussion Span
Last Post by kevindougans
0

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);
0

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

0

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

0

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);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.