バーコード画像を生成するには、色々な方法があります。
Laravel には、とても便利なバーコード画像を生成できるプラグインがあります。
本記事では、特に有名なプラグインである「milon/barcode」の利用方法を解説しています。
install
1 |
$ composer require milon/barcode |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
Info from https://repo.packagist.org: #StandWithUkraine ./composer.json has been updated Running composer update milon/barcode Loading composer repositories with package information Updating dependencies Lock file operations: 1 install, 0 updates, 0 removals - Locking milon/barcode (10.0.0) Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Downloading milon/barcode (10.0.0) - Installing milon/barcode (10.0.0): Extracting archive Package fabpot/goutte is abandoned, you should avoid using it. Use symfony/browser-kit instead. Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi INFO Discovering packages. barryvdh/laravel-dompdf ..................................................................................................................... DONE inertiajs/inertia-laravel ................................................................................................................... DONE kyslik/column-sortable ...................................................................................................................... DONE laravel/sail ................................................................................................................................ DONE laravel/sanctum ............................................................................................................................. DONE laravel/tinker .............................................................................................................................. DONE maatwebsite/excel ........................................................................................................................... DONE milon/barcode ............................................................................................................................... DONE nesbot/carbon ............................................................................................................................... DONE nunomaduro/collision ........................................................................................................................ DONE nunomaduro/termwind ......................................................................................................................... DONE spatie/laravel-ignition ..................................................................................................................... DONE tightenco/ziggy ............................................................................................................................. DONE 94 packages you are using are looking for funding. Use the `composer fund` command to find out more! > @php artisan vendor:publish --tag=laravel-assets --ansi --force INFO No publishable resources for tag [laravel-assets]. Found 1 security vulnerability advisory affecting 1 package. Run composer audit for a full list of advisories. Using version ^10.0 for milon/barcode |
Register Barcode Library
config/app.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php return [ 'providers' => [ .... .... .... Milon\Barcode\BarcodeServiceProvider::class, ], 'aliases' => [ .... .... .... 'DNS1D' => Milon\Barcode\Facades\DNS1DFacade::class, 'DNS2D' => Milon\Barcode\Facades\DNS2DFacade::class, ] |
サンプル
1 2 3 4 5 6 7 8 9 |
<div class="container mt-4"> <div class="mb-3">{!! DNS1D::getBarcodeHTML('1234567890', 'PHARMA') !!}</div> <div class="mb-3">{!! DNS1D::getBarcodeHTML('1234567890', 'PHARMA2T') !!}</div> <div class="mb-3">{!! DNS1D::getBarcodeHTML('1234567890', 'CODABAR') !!}</div> <div class="mb-3">{!! DNS1D::getBarcodeHTML('1234567890', 'KIX') !!}</div> <div class="mb-3">{!! DNS1D::getBarcodeHTML('1234567890', 'RMS4CC') !!}</div> <div class="mb-3">{!! DNS1D::getBarcodeHTML('1234567890', 'UPCA') !!}</div> <div class="mb-3">{!! DNS2D::getBarcodeHTML('1234567890', 'QRCODE') !!}</div> </div> |
公式サイト
milon/barcode
https://github.com/milon/barcode
https://www.positronx.io/how-to-quickly-generate-barcode-in-laravel-application/