How to Generate a QR code using PHP?

13,945

Try this:

<?php
header('Content-Type: image/png');

include( '../phpqrcode/qrlib.php' );
QRcode::png( 'PHP QR Code : )' );
?>

When outputting an image, you don't need HTML tags. Alternatively, you could use img tag with data:... source. For an example, see here.

Share:
13,945
seamus
Author by

seamus

Updated on June 04, 2022

Comments

  • seamus
    seamus almost 2 years

    I am attempting to turn some data into a QR on a webpage. To do this I am using 'php qrcode'.

    Sourceforge Home Page of library: http://phpqrcode.sourceforge.net/

    I am unable to generate the QR code using the library. Am I missing a PHP extension.

    Thinking I was missing the GD2 extension, I did:

    brew install gd
    

    Current Code:

      include( '../phpqrcode/qrlib.php' );
      QRcode::png( 'PHP QR Code : )' );
    

    Current Output:

    �PNG IHDRWWKK/PLTE���U��~�IDAT8��ұ � PG.�N�t�tـ,V��H,pe�����4Ĵq�k�� ��j���'^l�����p�h�Sn�8D>3T-�%�����:\��z���4{oL��!C�-����_o�r���r��m��x+��,}��ő��|i����ʚt{oZ6\,i�{�5몏{���Ϫ����/MIEND�B`�

    Edit: I have tried changing the html header to image/png. Same results.

       <meta http-equiv="content-type" content="image/png" charset="UTF-8">
    

    Edit: Full Sample Code, Same results, no QR code.

    <?php header('Content-type: image/png'); ?>
    
    <!DOCTYPE html>
    <html lang="en" dir="ltr">
    <head>
    <meta http-equiv="content-type" content="image/png" charset="utf-8">
    <title></title>
    </head>
    <body>
    
    <?php
    
    include( '../phpqrcode/qrlib.php' );
    QRcode::png( 'PHP QR Code : )' );
    
    ?>
    
    </body>
    </html>
    

    Edit: The following shows same results

    <?php
    header('Content-type: image/png');
    
    include( '../phpqrcode/qrlib.php' );
    QRcode::png( 'PHP QR Code : )' );
    ?>
    

    Edit: Using laravel framework

    • Franz Gleichmann
      Franz Gleichmann about 6 years
      you have to set the content-type header so the browser knows it's an image, not text
    • Andreas Storvik Strauman
      Andreas Storvik Strauman about 6 years
      Try adding header('Content-type: image/png'); to the top of your file!
    • Ken Lee
      Ken Lee over 2 years
      Try (1) putting this code as test.php (<?php include( '../phpqrcode/qrlib.php' );QRcode::png( 'PHP QR Code : )' );?> , then (2) put <img src='test.php'> in your laravel view.
  • houcros
    houcros over 4 years
    OP says he's already using that library. I think he needs more detailed help on how to generate the QR code.