Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SaadKiani-5 opened this issue Dec 9, 2024 · 0 comments
Open

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

SaadKiani-5 opened this issue Dec 9, 2024 · 0 comments

Comments

@SaadKiani-5
Copy link

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());
}

}
}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant