【PHP】endroid/qr-code를 사용하여 QR 코드 생성

7382 단어 PHPQRcodeQR코드
환경은 Windows 10입니다.
웹 서버는 Apache(XAMPP)입니다.
GitHub: https://github.com/endroid/qr-code
Packagist: https://packagist.org/packages/endroid/qr-code

설치하다.


Compooser를 사용하여 설치합니다.
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"

실행 테스트


GiitHub에 실린 샘플 코드를 실행해 보십시오.
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();

아이폰 카메라로 잘 인식해.

다방면으로 시도하다


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>

좋은 웹페이지 즐겨찾기