简体   繁体   English

使用php从给定的Google图表api URL下载图像文件

[英]download image file from given google chart api url using 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. 我想使用<a href="">Download</a>类的链接<a href="">Download</a>此URL返回的图像,并且在单击此链接时应出现下载框,以便用户可以将图像保存到他/她的系统。 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 ? 我不想将图像保存到服务器吗?

Original Question 原始问题

You can stream or proxy the file to your users by setting up a simple PHP download script on your server. 您可以通过在服务器上设置简单的PHP下载脚本,将文件流式传输或代理给用户。 When user hits the download.php script below it will set the correct headers so that their browsers asks them to save a download. 当用户点击下面的download.php脚本时,它将设置正确的标题,以便他们的浏览器要求他们保存下载。 It will then stream the chart image from google to the users browser. 然后它将图表图像从谷歌流到用户浏览器。

In your HTML: 在您的HTML中:

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

In download.php: 在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 传递动态生成的图表API URL

In your HTML: 在您的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: 在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;

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; 由于该图像是在chart.apis.google.com上生成的,并且您无法控制该服务器,因此无法使其随该图像一起发送Content-Disposition标头。 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. 从技术上讲可以做的事情(但我不确定Google的ToS是否允许,更好地检查),是链接到您的服务器,该服务器将代理下载并添加Content-Disposition: attachment标头。

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; 最接近的事情是使用PHP动态获取图像数据,然后将其提供给标头Content-Disposition: filename=qr.png 文件名= 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. 希望它是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM