yoha/qr

This package is made to make qr generation easy

Maintainers

Details

github.com/yohaqr/yohaqr

Source

Issues

Fund package maintenance!
yohatechtop

v1.2.0 2025-02-11 17:22 UTC

This package is auto-updated.

Last update: 2025-06-14 06:26:50 UTC


README

By yohaqr

If you appreciate my work, please consider supporting me by visiting the sponsor page or buying me a coffee ☕.

Latest Stable Version Build Status Total Downloads Monthly Downloads License

YohaQR

Overview

YohaQR is a powerful PHP Composer package that offers a simple, fluent API for generating dynamic QR codes using the latest Endroid QR Code library. With YohaQR you can easily customize your QR codes by:

  • Custom Data Encoding: Embed any text, URL, or custom data.
  • Adjustable Size and Margin: Control the overall dimensions and the surrounding margin.
  • Error Correction & Encoding: Select suitable error correction levels and encoding options.
  • Logo Integration: Seamlessly add a custom logo with options for resizing and background removal.
  • Labeling: Optionally append a label with custom fonts and alignment.

Warning

"Install GD or Imagepk extension for PHP."

We'll install either the GD or ImageMagick (imagick) extension based on your system.

🖥️ 1. On Linux (Ubuntu/Debian)

Install GD Extension

sudo apt update
sudo apt install php-gd -y
sudo systemctl restart apache2  # or php-fpm, if you're using it

(Alternative) Install ImageMagick

sudo apt install php-imagick -y
sudo systemctl restart apache2

🖥️ 2. On Fedora / CentOS / RHEL

Install GD Extension

sudo dnf install php-gd -y
sudo systemctl restart httpd

(Alternative) Install ImageMagick

sudo dnf install php-pecl-imagick -y
sudo systemctl restart httpd

🖥️ 3. On Windows (XAMPP/WAMP)

Enable GD Extension in php.ini

  1. Open the file: php.ini

    • In XAMPP: located at C:\xampp\php\php.ini
    • In WAMP: C:\wamp64\bin\php\php.ini
  2. Find and uncomment the line:

    ;extension=gd

    Change it to:

    extension=gd
  3. Restart Apache via the XAMPP/WAMP control panel.

✅ 4. Verify Installation

Using terminal/command:

php -m | grep -E "gd|imagick"

Or create a file phpinfo.php:

<?php phpinfo(); ?>

Installation

Install YohaQR via Composer:

composer require yoha/qr

Note: YohaQR requires PHP ^8.1. Ensure you have the latest version of PHP and Composer installed.

Supported Output Types

YohaQR supports multiple output formats for your QR codes:

  1. PNG (Default)
  2. PDF
  3. WEBP
  4. SVG
  5. More formats coming soon...

You can change the output format using the setWriterType() method.

Changing the Writer Type

By default, YohaQR generates PNG images. To change the writer type (e.g., to PDF, WEBP, or SVG), call the setWriterType() method with the desired format:

<?php 
use Yoha\Qr\Core\QrBuilder;

$qrBuilder = new QrBuilder();
$qrBuilder->setWriterType('pdf') // Change output type to PDF
          ->setData('http://yohacodes.pro.et')
          ->generate();

Usage Examples

Below are several advanced examples demonstrating how to generate and display QR codes using YohaQR.

Example 1: Basic QR Code Generation (PNG)

Generate a basic QR code and display it as a PNG image using the getUri() method.

<?php

use Yoha\Qr\Core\QrBuilder;

// Include the Composer autoloader if you're using YohaQR in a custom PHP project
require __DIR__ . '/../vendor/autoload.php';

$qrBuilder = new QrBuilder();
$build     = $qrBuilder
             ->setdata('data')
             ->generate();

// Generate the QR code and obtain the Data URI
$result = $qrBuilder->getUri();
?>

<!-- Display the generated QR code as an image -->
<img src="<?= $result ?>" alt="Generated QR Code (PNG)">

Example 2: Advanced QR Code with Logo and Custom Settings

This example demonstrates how to create a fully customized QR code with a logo, resized dimensions, and custom data.

<?php

use Yoha\Qr\QrBuilder;

$qrBuilder = new QrBuilder();

// Configure QR code with advanced settings
$result = $qrBuilder->setWriterType('png')                 // Change type if needed: 'pdf', 'webp', or 'svg'
                    ->setData('https://example.com')         // Data to encode
                    ->setLogoPath(__DIR__ . '/assets/logo.png')// Set the path to your logo image
                    ->setLogoResizeToWidth(50)                 // Resize logo width (in pixels)
                    ->setLogoPunchoutBackground(true)          // Optionally remove logo background
                    ->setMargin(2)                             // Set the QR code margin
                    ->generate();

// Retrieve the Data URI
$uri = $result->getDataUri();
?>

<!-- Display the QR code with a logo -->
<img src="<?= $uri ?>" alt="Advanced QR Code with Logo">

Example 3: Generating and Saving a PDF QR Code

Generate a QR code in PDF format and save it to the server, then display it using an embed element.

<?php

use Yoha\Qr\QrBuilder;

$qrBuilder = new QrBuilder();

// Generate a PDF QR code and save the file
$result = $qrBuilder->setMargin(2)
                    ->setWriterType('pdf')
                    ->setData('Testing SaveFile')
                    ->saveToFile(name: 'testing_pdf', path: __DIR__ . '/../storage/files/');

// Retrieve the Data URI for the saved file
$uri = $result->getDataUri();
?>

<!-- Embed the saved PDF in the browser -->
<embed src="<?= $uri ?>" type="application/pdf" width="600" height="800">

Example 4: Dynamically Rendering QR Codes Based on MIME Type

YohaQR provides a helper method (readFile()) that checks the MIME type of the generated QR code and renders the appropriate HTML element automatically.

<?php

use Yoha\Qr\QrBuilder;

$qrBuilder = new QrBuilder();

// Generate a QR code (change the writer type as needed: 'png', 'pdf', or 'svg')
$result = $qrBuilder->setMargin(2)
                    ->setWriterType('svg')
                    ->setData('Testing SaveFile')
                    ->generate();

// Render the QR code appropriately based on its MIME type
echo $qrBuilder->readFile($result);

Easy example

<?php

    $qr = new QrBuilder();
    $return = $qr->setData('data')
                 ->generate();

    echo "<img src='. $return->getDataUri() .'  alt='Scan Me' />";


?>

Logo Integration

The logo feature in YohaQR allows you to easily add a custom logo to your QR code. Just provide the logo’s file path with setLogoPath(). You can also:

  • Resize the logo using setLogoResizeToWidth() or setLogoResizeToHight()
  • Remove the logo's background with setLogoPunchoutBackground()
<?php

use Yoha\Qr\QrBuilder;

$qrBuilder = new QrBuilder();

$result = $qrBuilder->setData('https://example.com')
                    ->setLogoPath(__DIR__ . '/assets/logo.png')
                    ->setLogoResizeToWidth(60)
                    ->setLogoPunchoutBackground(true)
                    ->generate();

echo '<img src="' . $result->getDataUri() . '" alt="QR Code with Logo">';

after installing the package for easy use you can just call _qr("your data here')

Example

<?php

$uri = _qr('MyTestData');

<img src="<?= $uri ?>" alt="alter Txt" />

?>

Contributing

Contributions are welcome! If you find any bugs or have suggestions for improvements, please open an issue or submit a pull request on GitHub. For major changes, please open an issue first to discuss what you would like to change.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Security

If you discover any security vulnerabilities, please report them via email at johnpro3269@gmail.com.