Skip to content

how to add logo(image) in the QrCode #314

@SaadKiani-5

Description

@SaadKiani-5

my method : "
use Illuminate\Support\Facades\Storage;
use App\Http\Controllers\Controller;
use App\Model\QrCodeGenerateF\QrCodeGenerate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Barryvdh\DomPDF\Facade\Pdf;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use Intervention\Image\Facades\Image;

class QrCodeGeneratorController extends Controller
{
public function store(Request $request)
{
try {
$validatedData = $request->validate([
'url' => 'required|url',
'image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
]);

    $logoPath = $request->hasFile('image')
        ? $request->file('image')->store('uploads/logos', 'public')
        : null;

    $qrCodeFilename = 'qrcodes/' . uniqid() . '.png';
    $pdfFilename = 'qrcodes/' . uniqid() . '.pdf';


    $qrCodePath = storage_path('app/public/' . $qrCodeFilename);


    if (!file_exists(dirname($qrCodePath))) {
        mkdir(dirname($qrCodePath), 0755, true);
    }

    QrCode::size(300)
    ->margin(1)
    ->errorCorrection('H')
    ->format('png')
    ->generate($request->input('url'), $qrCodePath);

    // dd($QrCode);

       

    if ($logoPath) {
        $fullLogoPath = storage_path('app/public/' . $logoPath);
        $qrCodeImage = Image::make($qrCodePath);
        $logo = Image::make($fullLogoPath);


        $logoSize = 100;
        $logo->resize($logoSize, $logoSize, function ($constraint) {
            $constraint->aspectRatio();
            $constraint->upsize();
        });


        $logoX = ($qrCodeImage->width() - $logo->width()) / 2;
        $logoY = ($qrCodeImage->height() - $logo->height()) / 2;
        $qrCodeImage->insert($logo, 'top-left', $logoX, $logoY);


        $qrCodeImage->save($qrCodePath);
    }

    if (!file_exists($qrCodePath)) {
        throw new \Exception('Failed to generate QR Code');
    }


    $pdf = PDF::loadHTML('<img src="' . asset('storage/' . $qrCodeFilename) . '" />')
        ->save(storage_path('app/public/' . $pdfFilename));


    QrCodeGenerate::create([
        'image_path' => $logoPath ? str_replace('public/', '', $logoPath) : null,
        'qr_path' => $qrCodeFilename,
        'pdf_path' => $pdfFilename,
        'url' => $request->input('url'),
    ]);


    return redirect()->route('qrcode.show', [
        'qr_path' => $qrCodeFilename,
        'pdf_path' => $pdfFilename,
    ])->with('current_page', 'show_qr')
        ->with('success', 'QR Code generated successfully');
} catch (\Exception $e) {

    Log::error('QR Code Generation Error: ' . $e->getMessage());
    Log::error('Trace: ' . $e->getTraceAsString());

    return back()->withInput()
        ->with('error', 'Failed to generate QR Code: ' . $e->getMessage());
}

}
}"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions