如何为QR码创建下载链接?

问题描述 投票:2回答:2

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

我正在创建QR代码:

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
2个回答
3
投票

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

请记住,在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');

1
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.