Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Generate a QR code using PHP?

Tags:

php

qr-code

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

like image 499
seamus Avatar asked Nov 16 '25 11:11

seamus


1 Answers

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.

like image 171
Kamil Jarosz Avatar answered Nov 19 '25 02:11

Kamil Jarosz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!