1

I'm using PHP QR Code library to generate QR codes.

I included the library and after fetching user information from database, I am trying to create qrcode. And then return the path of the generated qrcode to the front end so that I can pass it to an image tag for showing it to users.

I am fetching name, id,email and user image path from database. I want to include user image to the qrcode, so I get the contents and encode it as string.

I'm not getting errors. I checked the folder, qrcode is not being saved.

require_once 'externalLibraries/qrcode/qrlib.php';
 // how to build raw content - QRCode with Business Card (VCard) + photo         
 $tempDir = QRCODE_PATH; //saves temporary directory path

 // we building raw data 
 $codeContents  = 'BEGIN:VCARD'."\n"; 
 $codeContents .= 'FN:'.$name."\n"; 
 $codeContents .= 'ID:'.$id."\n"; 
 $codeContents .= 'EMAIL:'.$email."\n"; 
 $codeContents .= 'PHOTO;JPEG;ENCODING=BASE64:'.base64_encode(file_get_contents('../'.$userAvatar))."\n"; 
 $codeContents .= 'END:VCARD'; 

 // generating 
 QRcode::png($codeContents, $tempDir.$clientid.'.png', 4, 3); 

 // displaying 
 return QRCODE_PATH.$clientid.'.png'; 

Is this the way to generate qrcodes?

version 2
  • 1,049
  • 3
  • 15
  • 36
  • What size is your image and where are you returning this to? – RST Sep 19 '16 at 09:25
  • I'm returning this to my angular service. Image is user profile picture. I guess encoding it results in a long string. @RST – version 2 Sep 19 '16 at 09:28
  • 1
    http://phpqrcode.sourceforge.net/examples/index.php?example=027 this example which you seem to follow, uses the outcome in `img`-tag and has a warning about picture size – RST Sep 19 '16 at 09:37
  • Is there any other method to include an image in a qrcode? @RST – version 2 Sep 19 '16 at 09:39
  • Since there is a size limit a QR-Code can handle, you can use the methods described in my answer below (resize image, link to image). – Philipp Palmtag Sep 21 '16 at 08:52

1 Answers1

0

Your code is working for me. The image is saved at the pointed location. I used placeholder for your variables though. To display the image you can use:

$imgpath = QRCODE_PATH.$clientid.'.png';
$src = 'data: '.mime_content_type($imgpath).';base64,'.base64_encode(file_get_contents($imgpath));
echo '<img src="'.$src.'">';

Update:

As mentioned by RST in the comments and stated in these answer a QR-Code can only have a limited size. The image you are using might simply be too large. Try using your generation without the image and see if it works. To answer your question in the comment, you can either reseize the image to be smaller, but no other method will help you hence the QR-Code size is limited. Maybe you think about putting a link to the image into the QR-Code.

Community
  • 1
  • 1
Philipp Palmtag
  • 1,310
  • 2
  • 16
  • 18