GithubHelp home page GithubHelp logo

chillerlan / php-qrcode Goto Github PK

View Code? Open in Web Editor NEW
1.8K 45.0 266.0 18.13 MB

A PHP QR Code generator and reader with a user-friendly API.

Home Page: https://smiley.codes/qrcode/

License: Apache License 2.0

PHP 92.20% PostScript 7.75% Twig 0.06%
php7 qrcode-generator qrcode qr-code qr-generator svg phpqrcode php php-library drupal-module

php-qrcode's Introduction

chillerlan/php-qrcode

A PHP QR Code generator based on the implementation by Kazuhiko Arase, namespaced, cleaned up, improved and other stuff.
It also features a QR Code reader based on a PHP port of the ZXing library.

Attention: there is now also a javascript port: chillerlan/js-qrcode.

PHP Version Support Packagist version Continuous Integration CodeCov Codacy Packagist downloads Documentation

Overview

Features

  • Creation of Model 2 QR Codes, Version 1 to 40
  • ECC Levels L/M/Q/H supported
  • Mixed mode support (encoding modes can be combined within a QR symbol). Supported modes:
  • Flexible, easily extensible output modules, built-in support for the following output formats:
  • QR Code reader (via GD and ImageMagick)

Requirements

For the QRCode reader, either ext-gd or ext-imagick is required!

Documentation

Important: Please use the examples from the branch that matches your installed php-qrcode version ( v4.x, v5.x, dev-main )!

Installation with composer

See the installation guide for more info!

Terminal

composer require chillerlan/php-qrcode

composer.json

{
	"require": {
		"php": "^^8.2",
		"chillerlan/php-qrcode": "dev-main#<commit_hash>"
	}
}

Note: replace dev-main with a version constraint, e.g. ^5.0 - see releases for valid versions.

Quickstart

We want to encode this URI for a mobile authenticator into a QRcode image:

$data = 'otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net';

// quick and simple:
echo '<img src="'.(new QRCode)->render($data).'" alt="QR Code" />';

Wait, what was that? Please again, slower! See Advanced usage in the manual. Also, have a look in the examples folder for some more usage examples.

QR codes are awesome!

Reading QR Codes

Using the built-in QR Code reader is pretty straight-forward:

// it's generally a good idea to wrap the reader in a try/catch block because it WILL throw eventually
try{
	$result = (new QRCode)->readFromFile('path/to/file.png'); // -> DecoderResult

	// you can now use the result instance...
	$content = $result->data;
	$matrix  = $result->getMatrix(); // -> QRMatrix

	// ...or simply cast it to string to get the content:
	$content = (string)$result;
}
catch(Throwable $e){
	// oopsies!
}

Shameless advertising

Hi, please check out some of my other projects that are way cooler than qrcodes!

Disclaimer!

I don't take responsibility for molten CPUs, misled applications, failed log-ins etc.. Use at your own risk!

License notice

Trademark Notice

The word "QR Code" is a registered trademark of DENSO WAVE INCORPORATED
https://www.qrcode.com/en/faq.html#patentH2Title

php-qrcode's People

Contributors

codemasher avatar darthmaim avatar dereuromark avatar joshstoik1 avatar jschaedl avatar kmvan avatar livingroot avatar maximiliankresse avatar trismegiste avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-qrcode's Issues

About php5 branch

First off, thank you very much for this library!

I'm working with the Drupal 8 GA Login module and we're hoping to use this library. We have the 2.x branch of this library working well, but had a user run into a problem with the minimum php version being 7.x.

Since Drupal 8 still officially supports PHP 5.6 I think it's in the GA_Login module's best interest to do the same, so I'm curious what you can tell me about the last version of this library that supported php5.6.

It looks like to me that we could use version 1.1.0 from packagist and support php5.6. Does that sound right to you?

If 1.1.0 branch would work for us, do you think there is any reason why we (GA_Login module) shouldn't use it? Is it significantly behind in features we might need, or insecure in some way?

Thanks again for dropping by the ga_login issue queue and for the great library!

PHP 8.0 Support

With the official release of php 8, will we have support for it?

Currently adding it using composer fails:

Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

Problem 1
- Installation request for chillerlan/php-qrcode 3.3 -> satisfiable by chillerlan/php-qrcode[3.3.0].
- chillerlan/php-qrcode 3.3.0 requires php ^7.2 -> your PHP version (8.0.0) does not satisfy that requirement.

Regards,

SVG output lines between paths

Hi,

I've noticed that outputting the QR code as SVG generates SVG with lines that look like borders on some squares. I haven't managed to figure out what exactly those lines are, stroke, outline or just misalignment of paths, it seems like they are transparent. Here is a screenshot of SVG versus the PNG output so its easier to notice what I'm talking about.

SVG QR code

PNG QR code

My QR options are:
_
'version' => 6,
'eccLevel' => QRCode::ECC_L,
'outputType' => QRCode::OUTPUT_MARKUP_SVG,
'imageBase64' => false
_

Can you try recreating the issue and looking into it please, maybe I'm doing something wrong. Thank you ahead!

QR codes not scanning

QR codes generated with the same parameters in v.1.0.3 and up do not seem to scan correctly. Using v.1.0.2 the code scans perfectly. This is using examples/image.php.

QR breaks on long strings

Great project!

However it seems the generator fails on long strings like this:

eyJjdCI6IjVPSExXNzZQZUg1NitEUUdtcFwvY2FBPT0iLCJpdiI6ImY0ZTI4MGMyYjc2NDExZmJjMmUzMjc5NzA0MTc3YmI4IiwicyI6ImZjZTZlMTY3YjNjMTQwMDUifQ%3D%3D

index

QRCodeException - code length overflow

Hi,
I have problem with generating SVG QRCode. No matter which size I'm gettting QRCodeException Here is my content. It is valid QRCode content for Czech standart for QR Invoice (http://qr-faktura.cz/).

Please could you increase MAX_BITS or provide option to bypase?

SPD*1.0*ACC:CZ8620100000002000948394*AM:3025.00*CC:CZK*X-VS:1181273*X-SS:0*X-KS:308*MSG:1181273*DT:20190318*X-INV:SID%2A1.0%2ATB0%3A2500.00%2AT0%3A525.00%2AID%3A1181273%2ADD%3A20190304%2AINI%3A25520270%2AVII%3ACZ25520270%2AINR%3A24791334%2AVIR%3ACZ24791334%2ADUZP%3A20190304%2AAM%3A3025.00%2AVS%3A1181273%2ADT%3A20190318%2ACC%3ACZK%2AX-SW%3Acpilot.cz*

PHP 7.1

I understand that you want to drop support for PHP 5.6 and even PHP7.

However, there's no technical reason for dropping 7.1 support. It should work perfectly fine on 7.1. There's a big difference between EOL and end of active support.

Otherwise this is a great library.

where is the autoload.php ๏ผŸ

$ phpunit
PHPUnit 6.4.3 by Sebastian Bergmann and contributors.

Cannot open file "/Users/luxin88/Documents/github/php-qrcode/vendor/autoload.php".

PHP 8 support

The latest release is restricted to ^7.4. Is there anything stopping this from being bumped?

Get version of generated qr-code

first: thank you for this excellent script!

is there a function to get the qr-version of the generated qr-code when in options "version" is set to QRCode::VERSION_AUTO ?

bug

Parse error: syntax error, unexpected 'SettingsContainerInterface' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in C:\laragon\www\testeqrcode_php\vendor\chillerlan\php-qrcode\src\QRCode.php on line 153

Render to variable

I haven't found a way to store the image to a variable so I can base64encode it and store it at a database without writing it to filesystem...

PDF output PR welcome or should we keep it separated?

We think about creating a PDF output module for this library but I'm not sure if we should keep it as an external repository/module or if you are willed to accept a PR for a default implementation?

We would use FPDF under the hood and the QR codes will be created as simple vector based PDF files. I'm also not sure if (in case for a default implementation) the dependency to FPDF should be hard (in the composer.json) or optional.

The class name for a default implementation would be chillerlan\QRCode\Output\QRFpdf.

If the created QR codes needs to be reused in dynamic created PDFs they can be reimported by FPDI.

What do you think? Separate repository/module or default implementation? :-)

Support for logo?

Hello,

Thank you for the well done QR Code generation!

I see that there is a configuration option that supports a logo in the center of the QR Code, which has not yet been implemented. Is there a target time frame for this, or is there insufficient user interest?

Best Regards,

Edward

Works locally but not on server

Hi,

Thanks for the great lib :)

Everything works fine locally but doesn't seem to work on my server (Ubuntu 18.4 on Digital Ocean).

I have tried checking and installing the php7.3 extension one by one but still no success.

Can you help with a lead?

Wkhtml2pdf -> Does not render SVG Correctly [With Solution]

Using WKHTML2PDF does not render the SVG at all. It comes up blank.

This is due to the extra % in the svgHeader string:

QRMarkup.php Line 30

    protected string $svgHeader = '<svg xmlns="http://www.w3.org/2000/svg" class="qr-svg %1$s" '.
                                  'style="width: 100%**%**; height: auto;" viewBox="0 0 %2$d %2$d">';

Support for logo in the middle

First of all: awesome package, well done. ๐Ÿ‘
I saw a little mention of logo support in the documentation, but that it wasn't implemented yet. Any idea when this will be available?

Change color

Hello,

maybe I am to stupid to find it out, but how can I change to qrcode from black to orange?
Thanks

[Suggestion] Non square logos are stretching (not really a bug)

Hello,

In regards to #35 (where you did an EXCELLENT job - as in the full library as a whole), I have noticed that using non-square logos it's a bit hard to determine the number of squares that should be reserved in the matrix for a particular image using logoWidth and logoHeight in a way it doesn't stretch.

Example given: I have a logo of 972x324px (3:1 width:height scale). Setting logoWidth = 30 and logoHeight = 10 (following the 3:1 WxH ratio of the logo), the resulting QR Code is "stretched" horizontally... ๐Ÿ˜น

Reason: there's 1 extra "block" reserved by your lib at all edges (top, bottom, left, right) and the actual space the logo will occupy in the matrix is 28x8 (3.5:1).

For it to work as expected, I had to set logoWidth = 32 (2 blocks extra at top and bottom = 30 for the logo image) and logoHeight = 12 (2 blocks extra at left and right = 10 for the logo), and boom! The logo is now perfectly fit and the ratio 3:1 is kept.

My suggestion is logoWidth and logoHeight had 2 "automatically added" internally in the lib, so the definition of these values would be much more intuitive (W 30 H 10 = ratio kept).

I just want to save the file

I do not want to display or output anything.
I am sure there is a way, but I cannot easily see it in the documentation or the examples.

-FT

code length overflow

Hello,
Based on the example taken on php-qrcode/examples/image.php
If i take the exact same example you wrote : working.
If i just remove the content form moduleValues and put an eccLevel to ECC_H : working.
But i get a code length overflow with the following string of data :
$data = 'otpauth://totp/flupflup.com:marco?secret=II6SXEVSTAQV4SNJ&issuer=flupflup.com';
$options = new QROptions([
'version' => 7,
'outputType' => QRCode::OUTPUT_IMAGE_PNG,
'eccLevel' => QRCode::ECC_H,
'scale' => 5,
'imageBase64' => false,
'moduleValues' => [],
]);
header('Content-type: image/png');
echo (new QRCode($options))->render($data);

It looks like it happen only with ECC_H and the other are working well.
I don't really know if my error is relevant or if I did something wrong. But it might be useful to share.
Here the stacktrace :
Stack trace:

#0 /public_html/vendor/chillerlan/php-qrcode/src/Data/QRDataAbstract.php(118): chillerlan\QRCode\Data\QRDataAbstract->writeBitBuffer('otpauth://totp/...')
#1 /public_html/vendor/chillerlan/php-qrcode/src/Data/QRDataAbstract.php(95): chillerlan\QRCode\Data\QRDataAbstract->setData('otpauth://totp/...')
#2 /public_html/vendor/chillerlan/php-qrcode/src/QRCode.php(205): chillerlan\QRCode\Data\QRDataAbstract->__construct(Object(chillerlan\QRCode\QROptions), 'otpauth://totp/...')
#3 /public_html/vendor/chillerlan/php-qrcode/src/QRCode.php(157): chillerlan\QRCode\QRCode->initDataInterface('otpauth://totp/...')
#4 /public_html/vendor/chillerlan/php-qrcode/src/QRCode.php(230): chillerlan\QRCode\QRCode->getMatrix('otpauth://totp/...')
#5 /public_html/vendor/chillerlan/php-qrcode/src/Data/QRDataAbstract.php on line 230

Anyway, thank's for the tools, you've made a neat job.

Generate QR from raw binary data

I'm looking to generate QR codes from raw binary data rather than text, as it makes for much more compact codes - for example a timestamp value (from time()) as text is at least 10 bytes, but as raw data is only 4 (pack('L', time())), but this QR code renderer dies when given raw data like that. It it possible to disable character set semantics and accept raw binary input with this package?

Examples not working

require_once '../vendor/autoload.php';

The file ../vendor/autoload.php is missing.

Use viewBox instead of width&height

When you have variable data length and want same size of inline svg image then it is better to have just viewbox defined. The only quick solution I have come up is ugly hack like this:

...
$output = $qr->render($data);
$output = preg_replace('~width="(\d+)px" height="(\d+)px"~', 'style="height: 100%; width: 100%" viewBox="0 0 $1 $2"', $output);
return $output;

Basic (novice) question: running examples

I have done:
step 1: get composer
step 2: composer install (created all dirs etc)

What do I do now to run the example(s) in the tests directory?

Could someone provide a simple index.php that would use the package, and is located in the root dir of my server? The package is installed in php-qrcode-master directory, under the root dir.

Thanks very much.

How to save the QR to the server?

Hi! This is probably a very noob question to ask but how do I save the QR code to my server folder? I've been using the QR library which this was based on but there you would call QRcode::png($data, $filePath . $fileName, QR_ECLEVEL_H, 6, 1); and it would be saved.

PDF output with text

We've been using the example class QRImageWithText to add a text below the QR code. Because we will need to print the QR codes, we will probably need to switch from PNG to PDF so that it's in vectors. Is it possible to add a text in PDF just like we do it in PNG? If so, would it be possible to add some sample class?

Thanks for your great work!

Help // How can i load the class in Laravel ?

In the implement of this class in a controller of laravel i have this:
app/Http/Controllers/TestCodeQRController.php

<?php

// namespace App\Http\Controllers;
namespace chillerlan\QRCode;

use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;

class TestCodeQRController extends Controller
{

    function index():string
    {
        echo 'Testing code QR()';


        $data = 'otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net';
        echo '<img src="'.(new QRCode)->render($data).'" />';



        return '';
    }

}

and the error is:
Class 'chillerlan\QRCode\Controller' not found

Can you help me ? Thanks a lot!

PostgresSQL Connect Bug

When trying to save the QRCode image in the database (POSTGRES) it generates the error:
Fatal error: Uncaught Error: Class 'chillerlan \ QRCodeExamples \ Bank' not found in C: \ xampp \ htdocs \ testeQRcode \ cadEvento.php: 29 Stack trace: # 0 {main} thrown in C: \ xampp \ htdocs \ testQRcode \ cadEvento.php on line 29

I wonder if the library supports PostgresSQL

Mask pattern & penalty calculation still incorrect

mask patterns

Unlike the announcement in the recent releases, the bug with the mask pattern generation was only partially fixed. What's worse is that several documentations, including ISO standards documents and Wikipedia, are divided about this specific topic. It turns out that the code was correct all the time and i fell for the (incorrect) ISO doc (p51), that has 2 parameters (i & j) swapped - i as rows instead of columns and vice versa for j. The repo where this library was forked from uses $col for the rows, which wasn't much less confusing... Now swapping these 2 does only matter for the mask patterns 001, 010 and 100 which would end up rotated by 90ยฐ and therefore result in (mostly) unreadable codes.

Mask pattern 100 flipped Mask pattern 100 expected
The wrong rotated mask pattern 100 (left) and the expectation (right) and data "test" masked with the correct pattern 100 (below) [Version 10, ECC: L]
image

This has now (hopefully) (finally) been fixed and i promise i won't touch it again.

mask pattern testing / penalty score

Another bug that seems to have existed ever since i've forked and refactored this repo is in the penalty calculation for the best mask pattern selection. It ranges from completely wrong results to off-by-one errors and needs further investigation (comparison to the original code, which i cannot confirm as correct due to the lack of tests).

rectangles to squares

Is there easy way to convert rectangles or squares to circles, or can you point me how can I do that?

QRCode image with logo and colors not working

It return the image black and white and not with colors as shown in examples

	$options = new QRCodeLogoOptions;

	$options->version          = 5;
	$options->eccLevel         = QRCode::ECC_H;
	$options->imageBase64      = false;
	$options->logoSpaceWidth   = 13;
	$options->logoSpaceHeight  = 13;
	$options->scale            = 5;
	$options->imageTransparent = false;
	
	$options2 = new QROptions([
		'version'      => 7,
		'outputType'   => QRCode::OUTPUT_IMAGICK,
		'eccLevel'     => QRCode::ECC_H,
		'scale'        => 5,
		'moduleValues' => [
			// finder
			1536 => '#A71111', // dark (true)
			6    => '#FFBFBF', // light (false)
			// alignment
			2560 => '#A70364',
			10   => '#FFC9C9',
			// timing
			3072 => '#98005D',
			12   => '#FFB8E9',
			// format
			3584 => '#003804',
			14   => '#00FB12',
			// version
			4096 => '#650098',
			16   => '#E0B8FF',
			// data
			1024 => '#4A6000',
			4    => '#ECF9BE',
			// darkmodule
			512  => '#080063',
			// separator
			8    => '#DDDDDD',
			// quietzone
			18   => '#DDDDDD',
		],
	]);
	header('Content-type: image/png');
	// print_r((new QRCode($options2))->render($data));die();
	$qrOutputInterface = new QRCodeLogo($options, (new QRCode($options2))->getMatrix($data));
	
	echo $qrOutputInterface->dump(null, FCPATH.'/image.png');
	die();

Specify output image dimensions

Hello,

is it possible to generate output image with fixed width & height no matter how long data is?

Thanks for the work.

Unable to install on PHP v7.2.33 (even when using ^3.3)

Unable to install on my php v7.2.3 server due to php-settings-container dependency which seems to be locked at 2.0.0. Documentation claimed that this was usable with ^3.3 for PHP 7.2.x but this doesn't seem to be the case.

Exact error:

$ composer require chillerlan/php-qrcode:^3.3

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - chillerlan/php-settings-container 2.0.0 requires php ^7.4 -> your PHP version (7.2.33) does not satisfy that requirement.
    - chillerlan/php-settings-container 2.0.0 requires php ^7.4 -> your PHP version (7.2.33) does not satisfy that requirement.
    - Installation request for chillerlan/php-settings-container (locked at 2.0.0) -> satisfiable by chillerlan/php-settings-container[2.0.0].

BC break 3.3.0 => 3.4.0 SVG base64

Hey there,

after upgrading to 3.4.0, the QR Code for SVG changed by default to base64.
Not sure if thats a BC break in 4.x aswell.

Would love if this is being fixed somehow.

Thanks in advance.

background color

Hey,

is it possible to generate a QR-Code with a white background?

At the moment all have transparent background and when display in an email with enabled dark-mode, it is almost impossible to even see the qr-code (Outlook for Android).

Thank you, Louis

Where's Wally?

Whats the reason for monopolizing the implementation with the composer? 'vendor', wheres Wally?

Error

Parse error: syntax error, unexpected 'SettingsContainerInterface' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in C:\laragon\www\testeqrcode_php\vendor\chillerlan\php-qrcode\src\QRCode.php on line 153

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.