dqrm8199 2011-04-13 13:11
浏览 54
已采纳

使用php从给定的谷歌图表api url下载图像文件

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 ?

  • 写回答

3条回答 默认 最新

  • doucan4873 2011-04-13 13:19
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码