【问题标题】:How do I create a download link for QR code?如何创建二维码的下载链接?
【发布时间】:2012-03-09 05:18:01
【问题描述】:

我正在使用 QR google api 创建 QR 码,但希望能够下载 PHP 中的图像。我在网上看过,但似乎找不到任何有用的东西。有什么建议吗?

我正在像这样创建二维码:

function generateQR($url, $width = 150, $height = 150) {
    $url    = urlencode($url);
    $image  = '<img src="http://chart.apis.google.com/chart?chs='.$width.'x'.$height.'&cht=qr&chl='.$url.'" alt="QR code" width="'.$width.'" height="'.$height.'"/>';
    return $image;
}

echo(generateQR('http://google.com')); 

【问题讨论】:

标签: php qr-code


【解决方案1】:

如果您想将文件下载到您的网络服务器(并保存),只需使用copy()

copy($url, 'myfile.png');

这不会提示访问者网络浏览器保存文件。

【讨论】:

    【解决方案2】:

    您可以使用任何二进制安全函数来检索和输出具有正确标题的图像。

    请记住,在 PHP 配置中 allow_fopen_url 必须为 On。

    类似:

    function forceDownloadQR($url, $width = 150, $height = 150) {
        $url    = urlencode($url);
        $image  = 'http://chart.apis.google.com/chart?chs='.$width.'x'.$height.'&cht=qr&chl='.$url;
        $file = file_get_contents($image);
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=qrcode.png");
        header("Cache-Control: public");
        header("Content-length: " . strlen($file)); // tells file size
        header("Pragma: no-cache");
        echo $file;
        die;
    }
    
    forceDownloadQR('http://google.com');
    

    【讨论】:

    • 当然,这需要在任何其他输出之前发送,或者必须打开输出缓冲
    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多