2012-02-17 43 views
1

我正在使用QR google api创建QR码,但希望能够使用PHP下载图像。我在网上查找,但似乎无法找到有用的东西。有什么建议么?如何创建QR码的下载链接?

我创建的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')); 
+1

file_get_contents oO – dynamic 2012-02-17 19:54:22

+0

可能是相关的:http://stackoverflow.com/questions/900207/return-a-php-page-as-an-image – cspray 2012-02-17 19:57:05

+0

不知道你是什么意思“下载图像在PHP中”。不过,你必须用urlencode编码url。 – satoshi 2012-02-17 19:57:37

回答

3

您可以使用任何二进制安全的功能,用正确的标题检索和输出的图像。

记住allow_fopen_url必须在PHP配置中为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'); 
+0

当然,这需要在任何其他输出之前发送,或者输出缓冲必须在 – 2012-02-17 20:11:33

1

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

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

这不会促使访问者的网络浏览器保存文件。

+0

的作品中,如果在php.ini中使用'allow_url_fopen = 1' – 2017-10-25 03:58:48