C:blanc
Published
July 8 2022
Blog PHP

Generate QRCode in PHP

  • このエントリーをはてなブックマークに追加

library

Generating a QR code is easy with the PHP library chillerlan/php-qrcode.

install

Install php-qrcode with Composer.

composer install
composer.json
{
    "require": {
        "php": "^8.0",
        "chillerlan/php-qrcode": "dev-main"
    }
}

PHP Code

The eighth line specifies the image type to be output.
In this case, we chose SVG.
Other options are here
PHP
<?php
require_once __DIR__ . '/vendor/autoload.php';
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
use chillerlan\QRCode\Common\EccLevel;

$options = new QROptions([
    'outputType' => QRCode::OUTPUT_MARKUP_SVG,
    'eccLevel' => EccLevel::L,
]);

$url = 'https://blog.c-blanc.com/';
$img = (new QRCode($options))->render($url);
?>

<div><img src="<?= $img ?>" width="300" height="300" /></div>

DEMO

Save -&gt; Download

I saved it on the server and made it available for download.
PHP
<?php
require_once __DIR__ . '/vendor/autoload.php';
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
use chillerlan\QRCode\Common\EccLevel;

$options = new QROptions([
    'outputType' => QRCode::OUTPUT_MARKUP_SVG,
    'eccLevel' => EccLevel::L,
]);

$url = 'https://blog.c-blanc.com/';
$file = 'data/qrcode.svg';
$path = __DIR__ . '/' . $file;

(new QRCode($options))->render($url, $path);
?>

<div><img src="<?= $file ?>" width="300" height="300" /></div>
<div><a href="<?= $file ?>" download>ダウンロード</a></div>

At the end.

It seems that more complicated QR Codes can be created, but I think this is enough.
Also, if you download it, you can use it as vector data in Adobe Illustrator or other applications.

PROFILE

C:blanc
I am a freelance programmer.
  • COMMENT

    Submitted. Comments will appear after approval.
  • あと150more characters