« Génération de QRCode Laravel » : différence entre les versions

De Gandal
(Page créée avec « <nowiki>https://github.com/endroid/qr-code</nowiki> SimpleSoftwareIO/simple-qrcode: An easy-to-use PHP QrCode generator with first-party support for Laravel. (github.com) <syntaxhighlight lang="php"> <?php namespace App\Http\Controllers; use App\Mail\TicketPurchased; use App\Models\Participant; use App\Models\Ticket; use Illuminate\Http\Request; use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\Mail; use SimpleSoftwareIO\QrCode\Facades\QrCode;... »)
 
Aucun résumé des modifications
 
(Une version intermédiaire par le même utilisateur non affichée)
Ligne 1 : Ligne 1 :
<nowiki>https://github.com/endroid/qr-code</nowiki>
Version plus complète : <nowiki>https://github.com/endroid/qr-code</nowiki>


SimpleSoftwareIO/simple-qrcode: An easy-to-use PHP QrCode generator with first-party support for Laravel. (github.com)
SimpleSoftwareIO/simple-qrcode: An easy-to-use PHP QrCode generator with first-party support for Laravel. (github.com)
Ligne 5 : Ligne 5 :
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
<?php
<?php
 
/* Un exemple*/
namespace App\Http\Controllers;
private function generateQrCode($text)
 
use App\Mail\TicketPurchased;
use App\Models\Participant;
use App\Models\Ticket;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Mail;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
 
class TicketController extends Controller
{
    /**
    * Purchase a ticket
    *
    * @param Request $request
    * @response array{message: "Ticket purchased successfully and email sent."}
    * @return JsonResponse
    */
    public function purchase(Request $request)
     {
     {
         // Generate a unique code for the ticket
         $result = Builder::create()
        $ticketCode = uniqid();
            ->writer(new PngWriter())
            ->writerOptions([])
            ->data($text)
            ->encoding(new Encoding('UTF-8'))
            ->errorCorrectionLevel(ErrorCorrectionLevel::High)
            ->size(300)
            ->margin(10)
            ->roundBlockSizeMode(RoundBlockSizeMode::Margin)
            ->foregroundColor(new Color(0, 0, 0))
            ->backgroundColor(new Color(255, 255, 255))
            ->logoPath("https://www.mouleagaufres.com/wp-content/uploads/2022/03/logoMAG-blanc.png")
            ->logoResizeToWidth(50)
            ->logoPunchoutBackground(true)
            ->validateResult(false)
            ->build();


         // Generate a link to validate the ticket
         return $result->getString();
        $qrCodeUrl = route('validate.ticket', ['code' => $ticketCode]);
       
        // Generate QR code with the link
        $qrCode = QrCode::format('png')->size(200)->generate($qrCodeUrl);
 
        // Create ticket
        $ticket = Ticket::create([
            'type' => $request->type,
            'price' => $request->price,
            'code' => $ticketCode,
            'qr_code' => base64_encode($qrCode),
            'status' => 'unvalidated',
            'event_id' => $request->event_id,
            'participant_id' => $request->participant_id,
        ]);
 
        // Send email with QR code
        $participant = Participant::find($request->participant_id);
        Mail::to($participant->email)->send(new TicketPurchased($ticket));
 
        return response()->json(['message' => 'Ticket purchased successfully and email sent.', 'ticketCode' => $ticketCode, 'qrCode' => $qrCodeUrl], 201);
     }
     }
}
</syntaxhighlight>
</syntaxhighlight>

Dernière version du 11 juin 2024 à 15:52

Version plus complète : https://github.com/endroid/qr-code

SimpleSoftwareIO/simple-qrcode: An easy-to-use PHP QrCode generator with first-party support for Laravel. (github.com)

<?php
/* Un exemple*/
private function generateQrCode($text)
    {
        $result = Builder::create()
            ->writer(new PngWriter())
            ->writerOptions([])
            ->data($text)
            ->encoding(new Encoding('UTF-8'))
            ->errorCorrectionLevel(ErrorCorrectionLevel::High)
            ->size(300)
            ->margin(10)
            ->roundBlockSizeMode(RoundBlockSizeMode::Margin)
            ->foregroundColor(new Color(0, 0, 0))
            ->backgroundColor(new Color(255, 255, 255))
            ->logoPath("https://www.mouleagaufres.com/wp-content/uploads/2022/03/logoMAG-blanc.png")
            ->logoResizeToWidth(50)
            ->logoPunchoutBackground(true)
            ->validateResult(false)
            ->build();

        return $result->getString();
    }