18
19

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.

laravelでQRコードを作ってbase64でエンコードする

Last updated at Posted at 2018-11-09

最初はwerneckbh/laravel-qr-codeを入れたけど、
どうやら画像生成に特化しているようでQRコードを文字列で出力できなかった。
simplesoftwareio/simple-qrcodeだと上手くいったので共有。

インストール方法

  • composer.jsonのrequireにパッケージを追加
composer.json
"require": {
    "simplesoftwareio/simple-qrcode": "~2"
}
  • laravelのディレクトリに移動してcomposer updateを実行
  • サービスプロバイダーとエイリアスを追加
config/app.php
'providers' => [
    ...
    /*
     * Package Service Providers...
     */
     // 追加!!!
    SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class,
],

'aliases' => [
    ...
    // 追加!!!
    'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class,
],

使い方

  • フォーマット指定->生成->base64化でOK
SampleController.php
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class SampleController extends Controller
{
    ...
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $src = base64_encode(QrCode::format('png')->size(100)->generate('https://qiita.com/nobuhiro-kobayashi'));
        
        return response('<img src="data:image/png;base64, ' . $src . '">');
    }
}
  • png以外でもsvgやepsにも対応している模様

参考サイト

simple-qrcode

18
19
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
18
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?