2012-02-17 255 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