16
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【PHP】endroid/qr-codeを使ってQRコードを生成してみる

Last updated at Posted at 2019-04-08

環境はWindows10。
WebサーバーはApache(XAMPP)です。

GitHub:https://github.com/endroid/qr-code
Packagist:https://packagist.org/packages/endroid/qr-code

インストール

Composerを使ってインストールします。

C:\xampp\htdocs\qrcode>composer require endroid/qr-code
Using version ^3.5 for endroid/qr-code
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 10 installs, 0 updates, 0 removals
  - Installing endroid/installer (1.1.5): Downloading (100%)
  - Installing symfony/polyfill-ctype (v1.11.0): Loading from cache
  - Installing symfony/inflector (v4.2.5): Downloading (100%)
  - Installing symfony/property-access (v4.2.5): Downloading (100%)
  - Installing symfony/options-resolver (v4.2.5): Downloading (100%)
  - Installing myclabs/php-enum (1.6.6): Downloading (100%)
  - Installing khanamiryan/qrcode-detector-decoder (1.0.2): Downloading (100%)
  - Installing dasprid/enum (1.0.0): Downloading (100%)
  - Installing bacon/bacon-qr-code (2.0.0): Downloading (100%)
  - Installing endroid/qr-code (3.5.8): Downloading (100%)
symfony/property-access suggests installing psr/cache-implementation (To cache access methods.)
bacon/bacon-qr-code suggests installing ext-imagick (to generate QR code images)
endroid/qr-code suggests installing symfony/http-foundation (Install if you want to use QrCodeResponse)
Writing lock file
Generating autoload files
Endroid Installer detected project type "all"

テスト実行

GitHubに載っているサンプルコードを実行してみます。

index.php
<?php
require_once __DIR__ . '/vendor/autoload.php';

use Endroid\QrCode\QrCode;

$qrCode = new QrCode('Life is too short to be generating QR codes');

header('Content-Type: '.$qrCode->getContentType());
echo $qrCode->writeString();

image.png

ちゃんとiPhoneのカメラで認識します。

IMG_2435.jpeg

色々試す

index.php
<?php
require_once __DIR__ . '/vendor/autoload.php';

use Endroid\QrCode\QrCode;

// QRコードオブジェクトの生成
// 引数にQRコードにしたい情報の文字列を渡す。
$qrCode = new QrCode('https://qiita.com/danishi/items/fd8038ee373c04b94bf6');

// サイズ設定(ピクセル)
$qrCode->setSize(400);

// 余白の設定(ピクセル)
$qrCode->setMargin(10);

// 前景色の設定(RGBA)
$qrCode->setForegroundColor(['r' => 0, 'g' => 128, 'b' => 0, 'a' => 0]);

// 背景色の設定(RGBA)
$qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);

// ロゴを設定
$qrCode->setLogoPath('qiita.png');

// ロゴのサイズ設定
$qrCode->setLogoSize(50, 50);

// ファイルに出力
$qrCode->writeFile(__DIR__.'/qrcode.png');

// ブラウザにQRコードを返す。
//header('Content-Type: '.$qrCode->getContentType());
//echo $qrCode->writeString();

// imgタグに表示
$img = base64_encode($qrCode->writeString());

?>
<html>
	<body>
		<img src="data:image/gif;base64,<?= $img ?>">
	</body>
</html>

image.png

16
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?