Generating QR Codes in PHP with Composer (endroid/qr-code) and phpqrcode

This article walks through two practical methods for generating QR codes in PHP—using Composer to install the endroid/qr-code library and directly integrating the phpqrcode library—detailing setup steps, code examples, and parameter explanations to help developers quickly implement URL‑based QR code generation.

php Courses
php Courses
php Courses
Generating QR Codes in PHP with Composer (endroid/qr-code) and phpqrcode

Because the team needed a PHP action that generates a QR code from a given URL, the author documents the process of researching QR‑code generation, discovering existing libraries, and summarizing the implementation steps.

Initially the author searched for QR‑code generation mechanisms online, realized that building one from scratch was unnecessary, and decided to use an existing PHP library.

Method 1: Composer with endroid/qr-code – The author explains that Composer is the standard PHP package manager, shows how to install the library with composer require endroid/qr-code, and describes issues encountered on Windows (locating php.exe in XAMPP). After successful installation, the library can be referenced in the project.

Method 2: phpqrcode library – The author downloads the phpqrcode package from SourceForge, places it in the extends directory (required for ThinkPHP), and provides a complete function example:

function qrcode($level = 'L', $size = 4) {
    // Import Phpqrcode plugin
    require_once EXTEND_PATH.'phpqrcode/phpqrcode.php';
    $url1 = 'https://www.baidu.com/s?wd=666...';
    $errorCorrectionLevel = $level;
    $matrixPointSize = intval($size);
    $object = new QRcode();
    ob_end_clean();
    $object->png($url1, false, $errorCorrectionLevel, $matrixPointSize, 2);
}

The author then lists the meaning of the function parameters ($text, $outfile, $level, $size, $margin, $saveabdprint, $back_color, $fore_color) and notes that colors must be provided as hexadecimal values without the "#" (e.g., #FFFFFF0xFFFFFF).

In summary, the article emphasizes that most needed functionalities already have open‑source implementations; developers should first research existing solutions, consult documentation, and then integrate the appropriate library to achieve the desired feature efficiently.

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 codephpqrcodeendroid/qr-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.