PHP code that generates qr code

Here is a simple PHP script that generates a QR code:

Copy code<?php
// Required library
require_once 'vendor/autoload.php';

// Use the library namespace
use Endroid\QrCode\QrCode;

// Create a new QR code
$qrCode = new QrCode('https://www.example.com');

// Set additional options
$qrCode->setSize(300);

// Directly output the QR code
header('Content-Type: '.$qrCode->getContentType());
echo $qrCode->writeString();

This script uses the endroid/qr-code library to generate the QR code. Make sure to install this library by running composer require endroid/qr-code in the terminal.

You can customize the QR code by setting additional options, such as the size, error correction level, and foreground and background colors. For a full list of available options, see the documentation for the endroid/qr-code library: https://github.com/endroid/qr-code

You can also save the QR code to an image file by calling the writeFile() method instead of writeString().

Leave a Comment