Backend Development 5 min read

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., #FFFFFF → 0xFFFFFF ).

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.

Tutorialqr codeComposerphpqrcodeendroid/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

login 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.