vladpak1/too-simple-qr

A wrapper for the chillerlan/php-qrcode library that allows you to generate a variety of QR codes with ease.

1.0.1 2023-09-16 05:30 UTC

This package is auto-updated.

Last update: 2024-04-16 06:57:33 UTC


README

TooSimpleQR is a wrapper over the wonderful chillerlan/php-qrcode, which simplifies QR code generation, paying more attention to customizations and ease of interface.

This small pet project is far from perfect, as I'm still a beginner developer learning the ropes. Please keep in mind my limited experience when using it, and proceed at your own risk.

If you want to generate QR codes in a serious project, it's better to use chillerlan/php-qrcode.

Examples of QR codes created with this library:

Requirements

  • PHP 8.0+
  • Composer
  • GD or Imagick

Installation

{
  "require": {
    "vladpak1/too-simple-qr": "*"
  },
  "minimum-stability": "dev",
  "prefer-stable": true
}

There are plans to move to a stable version, so soon this will not be necessary.

Or if you already have minimum-stability set to dev, you can simply run:

composer require vladpak1/too-simple-qr

Usage Example

use vladpak1\TooSimpleQR\QRFactory;
use vladpak1\TooSimpleQR\OutputConstants;
use vladpak1\TooSimpleQR\Image\Logo;

// Load composer
require_once __DIR__ . '/vendor/autoload.php';

$qr = \vladpak1\TooSimpleQR\QRFactory::QRCode();

$settings = \vladpak1\TooSimpleQR\QRFactory::QRSettings();

$logo = new Logo();
$logo->setByPath(Logo::TWITTER_LOGO);

$settings
    ->setOutput(OutputConstants::OUTPUT_PNG)
    ->setSize(500)                              // Size in pixels.
    ->setBackgroundColor('#ffffff')             // Background color in HEX format.
    ->setMargin(50)                             // Margin in pixels.
    ->setFinderColor('#1d9bf0')                 // Finder color in HEX format.
    ->setDataColor('#3284bc')                   // Data color in HEX format.
    ->setCorrectionLevel(OutputConstants::CORRECTION_LEVEL_H) // Correction level.
    ->setCircularModules(true)                  // Round modules.
    ->setCircleRadius(0.75)                     // Circle radius.
    ->setLogo($logo)                            // Logo.
    ->setCustomFinderShape(OutputConstants::SHAPE_CIRCLE); // Custom finder shape.

$qrCodeImage = $qr
    ->setData('Hello World!')
    ->setSettings($settings)
    ->render();
    
// Now we can save or directly output (as HTML) the QR code.
$qrCodeImage->echo();

As a result, we get:

We can also distort the QR code by adding, for example, a gradient:

->setBackgroundGradient('#FFD8D8', '#FFF9F9', Gradient::DIRECTION_BOTTOM_TO_TOP);

Need to quickly generate a QR code?

Consider using presets — predefined settings for generating QR codes.

You can easily create your own presets by extending the vladpak1\TooSimpleQR\Preset\AbstractPreset class.

Example of using a preset:

$preset = new \vladpak1\TooSimpleQR\Preset\Presets\InstagramPreset('Hello World!');

// You can also set the driver and output interface for the preset.
$preset
    ->setDriver(\vladpak1\TooSimpleQR\OutputConstants::DRIVER_GD)
    ->setOutput(\vladpak1\TooSimpleQR\OutputConstants::OUTPUT_PNG);
    
// We get a branded Instagram QR code.
$preset->render()->save('path/to/qr.png');

GD or Imagick?

This library will work with either GD or Imagick, but some settings are only supported by Imagick. Probably in the future, I will make everything more consistent and the difference in drivers will not be so noticeable. However, at the moment, Imagick is recommended.

The library will automatically select the driver to use, based on what is installed on your server, giving preference to Imagick.

But you can also forcibly specify the driver:

$qr->setDriver(OutputConstants::DRIVER_GD);

Testing

TooSimpleQR uses PHPUnit for testing. Currently, there are very few tests, mainly due to issues with the package architecture.

To run tests, use the following command:

composer test

License

TooSimpleQR is licensed under the MIT license.