Paul
Paul

Reputation: 11746

How do I create a download link for QR code?

I'm using the QR google api to create QR codes but would like the ability to download the image in PHP. I've looked online but can't seem to find anything helpful. Any suggestions?

I'm creating the QR code like so:

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')); 

Upvotes: 2

Views: 5136

Answers (2)

goat
goat

Reputation: 31813

if you want to download the file onto your webserver(and save it), just use copy()

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

This will not prompt a visitors web browser to save the file.

Upvotes: 1

Guilherme Viebig
Guilherme Viebig

Reputation: 6932

You can use a any binary safe function to retrieve and output the image with the right headers.

Remeber that allow_fopen_url must be On in PHP configuration.

Something like:

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');

Upvotes: 4

Related Questions