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

Is it possible to make QR code with logo at the center #314

Closed
vinayk7 opened this issue Apr 15, 2021 · 6 comments
Closed

Is it possible to make QR code with logo at the center #314

vinayk7 opened this issue Apr 15, 2021 · 6 comments
Assignees
Labels

Comments

@vinayk7
Copy link

vinayk7 commented Apr 15, 2021

I wanted to know is it possible to create a QR something like this?

Note the center padding so it avoids the logo touching the nearby QR pixels

image

@endroid
Copy link
Owner

endroid commented Apr 15, 2021

Hi @vinayk7 one way would be to add the padding to the image you place over the QR code but then you would probably have some QR code blocks with partial overlap. If you want to avoid this partial overlap I suggest you either generate the matrix before like this.

$matrixFactory = new MatrixFactory();
$matrix = $matrixFactory->create($qrCode);

This gives you the block size etc. so you can exactly calculate the desired image width and height. Another option is to create your own writer based on the PNG writer and put the logo calculation logic there. In other words: it is possible but not out of the box. Good luck!

@endroid endroid closed this as completed Apr 15, 2021
@endroid endroid self-assigned this Apr 15, 2021
@mstnorris
Copy link

mstnorris commented Apr 28, 2021

@endroid and @vinayk7 I am also looking to do this. @endroid please would you expand on your comment as to how it could be used with the following?

$result = Builder::create()
    ->writer(new PngWriter())
    ->writerOptions([])
    ->data($menu->getURL())
    ->encoding(new Encoding('UTF-8'))
    ->errorCorrectionLevel(new ErrorCorrectionLevelHigh())
    ->size(512)
    ->margin(16)
    ->roundBlockSizeMode(new RoundBlockSizeModeMargin())
    ->logoResizeToHeight(64)
    ->logoResizeToHeight(64)
    ->logoPath("logo.png")
    ->labelText('Label Test')
    ->labelFont(new NotoSans(24))
    ->labelAlignment(new LabelAlignmentCenter())
    ->build();

$matrixFactory = new MatrixFactory();
$matrix = $matrixFactory->create($result); // This is of course the wrong type and I've already added the logo above

@endroid
Copy link
Owner

endroid commented Apr 28, 2021

Hi @mstnorris if you need a centered logo exactly the size of a specific number of blocks (with no overlap) like @vinayk7 you should first generate the matrix like you do in your example. As soon as you know all dimensions, margins and the block size you can use this in your own implementation / writer where you resize and place the image exactly based on the matrix dimensions and block size.

@benja2111
Copy link

@endroid I reopen this post to find out if it is now possible to have a rendering like the one above easily?
If not, can you explain more precisely how to get the same result?
I'm having trouble understanding the use of matrix.
Thank's in advance !

@endroid
Copy link
Owner

endroid commented Jun 8, 2022

Hi @benja2111 the matrix consists of the block size, the x,y values (0=white, 1=black) and the inner width and outer width that indicate the image width excluding and including the margin. You can use that information to make a square in the middle that exactly fits and place an image in the middle. How to do that is up to you, I have no algorithm that does that for you as mentioned above ;) If you have a working solution feel free to open a PR. Good luck!

@saruman
Copy link

saruman commented Apr 27, 2023

I wish this explanations where a little bit easier to understand.
I had to pay for a qr code to use a custom logo because I am not be able to us this project to render my qr codes with a logo.
The logo and qr shows in my result, but the image is over the code and that affects the readability.

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

No branches or pull requests

5 participants