ساخت QR Code در لاراول

برای به اشتراک گذاری لینک ها راه های متفاوتی وجود دارد.

1- نمایش آدرس اصلی URL،در صورتی که در URL کاراکترهای فارسی باشد این روش اصلا کاربردی نیست.

2- استفاده از کوتاه کننده های لینک مثل: ADR3.IR

3- نمایش QR Code

برای این کار در لاراول ما از پکیج  Simpe QRCode استفاده می کنیم.

نصب و راه اندازی

پکیج Simple QR Code رو به فایل composer.json اضافه کنید،سپس دستور composer update را بزنید.

"require": {
    "simplesoftwareio/simple-qrcode": "~1"
}

اگر هم از نسخه 5.4 و قدیمی تر استفاده می کنید موارد زیر رو انجام بدید:

متن SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class در مسیر app\config و در بخش Provider کپی نمایید.

همچنین متن ‘QrCode’ => SimpleSoftwareIO\QrCode\Facades\QrCode::class در مسیر app\config و در بخش aliases کپی نمایید.

مراحل نصب به اتمام رسید.

چگونه از پکیج استفاده کنیم ؟

برای استفاده در بخش controller مربوطه که قرار هست نمایش داده شود دستور زیر را وارد نمایید.

public static function QRCode($url)
{
     return SimpleSoftwareIO\QrCode\Facades\QrCode::size(200)->generate(url($url));
}

تابع را فراخوانی کنید و URL را به عنوان پارامتر ورودی ارسال کنید ، سپس به عنوان یک متغییر به view ارسال کنید.

نمایش خروجی

برای نمایش در blade دستور زیر را استفاده کنید تا نمایش QR انجام شود.

{!! $qrCode !!}

نمایش QrCode در پرینت ها

<div class="visible-print text-center">
    {!! QrCode::size(100)->generate(Request::url()); !!}
    <p>Scan me to return to the original page.</p>
</div>

استفاده در ایمیل ها

//Inside of a blade template.
<img src="{!!$message->embedData(QrCode::format('png')->generate('Embed me into an e-mail!'), 'QrCode.png', 'image/png')!!}">

تغییر سایز

این پکیج بصورت پیشفرض کمترین سایز را تولید می کند. برای اینکه سایز را تغییر بدید از متد Size استفاده کنید.

QrCode::size(100);

سایر توابع ها

QrCode::color(255,0,255);
 
QrCode::backgroundColor(255,255,0);
 
QrCode::margin(100);
 
QrCode::errorCorrection('H');
 
QrCode::encoding('UTF-8')->generate('Make me a QrCode with special symbols ♠♥!!');
 
QrCode::merge($filename, $percentage, $absolute);
 
//Generates a QrCode with an image centered in the middle.
QrCode::format('png')->merge('path-to-image.png')->generate();
 
//Generates a QrCode with an image centered in the middle.  The inserted image takes up 30% of the QrCode.
QrCode::format('png')->merge('path-to-image.png', .3)->generate();
 
//Generates a QrCode with an image centered in the middle.  The inserted image takes up 30% of the QrCode.
QrCode::format('png')->merge('http://www.google.com/someimage.png', .3, true)->generate();

برای مشاهده مستندات کامل simple software QR Code اینجا کلیک کنید.

دیدگاهتان را بنویسید