Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

download image file from given google chart api url using php

Tags:

php

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 ?

like image 201
Yasir Avatar asked Jul 15 '26 05:07

Yasir


1 Answers

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;
like image 74
Treffynnon Avatar answered Jul 17 '26 18:07

Treffynnon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!