3

i want to download image returned by this url using a link like <a href="">Download</a> and on click of this link download box should appear so user can save image to his/her system. here is the url that return image

http://chart.apis.google.com/chart?chs=300x300&cht=qr&chld=L|0&chl=http%253A%252F%252Fnetcane.com%252Fprojects%252Fyourl%252F3

i don't want to save the image to server is it possible ?

Yasir
  • 3,817
  • 10
  • 35
  • 41

3 Answers3

6

Original Question

You can stream or proxy the file to your users by setting up a simple PHP download script on your server. When user hits the download.php script below it will set the correct headers so that their browsers asks them to save a download. It will then stream the chart image from google to the users browser.

In your HTML:

<a href="download.php">Download</a>

In download.php:

header('Content-Type: image/png');
header('Content-Disposition: attachment; filename="chart.png"');
$image = file_get_contents('http://chart.apis.google.com/chart?chs=300x300&cht=qr&chld=L|0&chl=http%253A%252F%252Fnetcane.com%252Fprojects%252Fyourl%252F3');
header('Content-Length: ' . strlen($image));
echo $image;

Passing in dynamically generated chart API URLs

In your HTML:

<?php
$url = 'http://chart.apis.google.com/chart?my-generated-chart-api-url';
<a href="download.php?url=<?php echo urlencode($url); ?>">Download</a>

In download.php:

$url = '';
if(array_key_exists('url', $_GET)
   and filter_var($_GET['url'], FILTER_VALIDATE_URL)) {
     $url = $_GET['url'];
}
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename="chart.png"');
$image = file_get_contents($url);
header('Content-Length: ' . strlen($image));
echo $image;
Treffynnon
  • 21,365
  • 6
  • 65
  • 98
  • Unlikely that the OP is in control of Google Charts server. – Piskvor left the building Apr 13 '11 at 13:22
  • @Piskvor control is not required. This is all hosted on the OPs hosting and merely stream the contents of googles output to the user. – Treffynnon Apr 13 '11 at 13:23
  • aha, now I get your point. +1 (although the matter of ToS still applies - I'm pretty sure this is explicitly forbidden with GMaps, not sure about GCharts) – Piskvor left the building Apr 13 '11 at 13:27
  • great!!! :) it works for static url thank you so much...i don't know why i'm not getting full url when i send ur in querysting it only return chs paramater can you help me in this why its not returning full url? download.php?url=generated-api-url – Yasir Apr 13 '11 at 13:29
  • I think you might need to use `urlencode()` on the `generated-api-url` parameter in your URL (`download.php?url=generated-api-url`). See http://php.net/urlencode – Treffynnon Apr 13 '11 at 13:31
0

No, not really. Since the image is generated at chart.apis.google.com, and you don't have control over that server, you can't make it send the Content-Disposition header with that image; therefore, browsers will display that image.

What you technically could do (but I'm not sure if Google's ToS allows it, better check), is to link to your server, which will proxy the download and add the Content-Disposition: attachment header.

Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
0

I believe you will not be able to do that. The closest thing would be to dynamically get the image data using PHP and then serving it with the header Content-Disposition: attachment; filename=qr.png

<?php

$img_data = file_get_contents("http://chart.apis.google.com/chart?chs=300x300&cht=qr&chld=L|0&chl=http%253A%252F%252Fnetcane.com%252Fprojects%252Fyourl%252F3");
header("Content-Type: image/png");
header("Content-Length: " . strlen($img_data));
header("Content-Disposition: attachment; filename=qr.png");
print $img_data;

?>

Code is untested, but I think you get the gist of it. Hope its what you're looking for.

Igor Serko
  • 549
  • 2
  • 12