Search code examples
javascriptphphtmlqr-code

Download image in a URL


I have a QR in a URL

I want to click a button and download the image of the QR in the URL

URL: https://quickchart.io/qr?text=http://tapo.app&dark=000&light=fff&ecLevel=H&margin=1&size=500¢erImageSizeRatio=0.4¢erImageUrl=https://app.tapo.app/Logo-Big.png

is it possible using PHP or Javascript?

I havent figured it out.


Solution

  • You can use fetch to get the content as a blob, then create an <a> element with the download attribute. Clicking on the anchor will download the image.

    fetch('https://quickchart.io/qr?text=http://tapo.app&dark=000&light=fff&ecLevel=H&margin=1&size=500%C2%A2erImageSizeRatio=0.4%C2%A2erImageUrl=https://app.tapo.app/Logo-Big.png').then(res => res.blob()).then(blob => {
        let url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = 'Image.png';
        a.click();
        URL.revokeObjectURL(url);
    });