How to Install and Use the Endroid QR Code Extension in PHP

This guide explains the required environment, shows how to install the Endroid QR‑code library via Composer, and provides complete PHP code for generating standard QR codes and QR codes with embedded logos, including handling of file paths and optional domain URLs.

php Courses
php Courses
php Courses
How to Install and Use the Endroid QR Code Extension in PHP

The article begins by listing the development environment: Windows 10 x64, PHPStudy as the integrated PHP environment, and Composer as the dependency manager.

It then instructs to install the QR‑code extension using Composer with the command: composer require endroid/qr-code Two PHP service methods are presented. The first method make_qrcode creates a plain QR code image, handling directory creation, file naming, and optional domain prefix, and returns the image path.

<?php
namespace app\common\service;

use Jrk\Random;
use Endroid\QrCode\QrCode;
use think\Exception;

class QrcodeSrvice {
    public static function make_qrcode($text, $size = 105, $domain = false) {
        $img_name = Random::alnum(15);
        $n = date("Ym");
        $dir = app()->getRootPath() . "public/qrcode/code/" . $n;
        if (!is_dir($dir)) {
            mkdir($dir, 0777, true);
        }
        $pathname = $dir . "/" . $img_name . '.png';
        $qrCode = new QrCode();
        $qrCode->setText($text)
            ->setSize($size)
            ->setPadding(15)
            ->setErrorCorrection('high')
            ->setForegroundColor(['r'=>0,'g'=>0,'b'=>0,'a'=>0])
            ->setBackgroundColor(['r'=>255,'g'=>255,'b'=>255,'a'=>0])
            ->setImageType(QrCode::IMAGE_TYPE_PNG);
        try {
            $qrCode->save($pathname);
            $str = "/qrcode/code/" . $n . "/" . $img_name . '.png';
            if ($domain) {
                return request()->domain() . $str;
            } else {
                return $str;
            }
        } catch (\Endroid\QrCode\Exceptions\ImageTypeInvalidException $exception) {
            return "";
        }
    }
}

The second method QrCodeWithLogo generates a QR code with a logo, performing similar steps but also checking the logo file, setting logo size, and returning the appropriate path.

<?php
// ... (same namespace and use statements as above)
class QrcodeSrvice {
    // ... previous method omitted for brevity
    public static function QrCodeWithLogo($text, $logo, $domain = false) {
        if (!file_exists($logo)) {
            exception("logo地址不存在");
        }
        $img_name = Random::alnum(15);
        $n = date("Ym");
        $dir = app()->getRootPath() . "public/qrcode/" . $n;
        if (!is_dir($dir)) {
            mkdir($dir, 0777, true);
        }
        $pathname = $dir . "/" . $img_name . '.png';
        $qrCode = new QrCode();
        $qrCode->setText($text)
            ->setSize(300)
            ->setLogo($logo)
            ->setLogoSize(60)
            ->setErrorCorrection('high')
            ->setForegroundColor(['r'=>0,'g'=>0,'b'=>0,'a'=>0])
            ->setBackgroundColor(['r'=>255,'g'=>255,'b'=>255,'a'=>0])
            ->setImageType(QrCode::IMAGE_TYPE_PNG);
        $qrCode->save($pathname);
        $str = "/qrcode/" . $n . "/" . $img_name . '.png';
        if ($domain) {
            return request()->domain() . $str;
        } else {
            return $str;
        }
    }
}

Overall, the guide provides a ready‑to‑use solution for generating QR codes in PHP projects, covering installation, configuration, and code examples for both plain and logo‑enhanced QR codes.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Tutorialqr-code
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.