-
-
Notifications
You must be signed in to change notification settings - Fork 320
Help saving in correct format #280
-
HI, found out recently about this tool and trying it out to see how it works.
I'm pretty new to coding in general, but I got a grasp on how to make things work.
Thing is, I don't understand how to save locally my generated qr code as a png.
I'll paste below what I've done so far.
<?php declare(strict_types=1); require_once __DIR__ . '/vendor/autoload.php'; use chillerlan\QRCode\{QRCode, QROptions}; use chillerlan\QRCode\Data\QRMatrix; use chillerlan\QRCode\Output\QRGdImagePNG; $options = new QROptions; $options->version = 7; $options->outputInterface = QRGdImagePNG::class; $options->scale = 20; $options->outputBase64 = false; $options->bgColor = [200, 150, 200]; $options->imageTransparent = true; #$options->transparencyColor = [233, 233, 233]; $options->drawCircularModules = false; $options->drawLightModules = true; $options->circleRadius = 0.4; $options->keepAsSquare = [ QRMatrix::M_FINDER_DARK, QRMatrix::M_FINDER_DOT, QRMatrix::M_ALIGNMENT_DARK, ]; $options->moduleValues = [ // finder QRMatrix::M_FINDER_DARK => [0, 63, 255], // dark (true) QRMatrix::M_FINDER_DOT => [0, 63, 255], // finder dot, dark (true) QRMatrix::M_FINDER => [233, 233, 233], // light (false), white is the transparency color and is enabled by default // alignment QRMatrix::M_ALIGNMENT_DARK => [255, 0, 255], QRMatrix::M_ALIGNMENT => [233, 233, 233], // timing QRMatrix::M_TIMING_DARK => [255, 0, 0], QRMatrix::M_TIMING => [233, 233, 233], // format QRMatrix::M_FORMAT_DARK => [67, 159, 84], QRMatrix::M_FORMAT => [233, 233, 233], // version QRMatrix::M_VERSION_DARK => [62, 174, 190], QRMatrix::M_VERSION => [233, 233, 233], // data QRMatrix::M_DATA_DARK => [0, 0, 0], QRMatrix::M_DATA => [233, 233, 233], // darkmodule QRMatrix::M_DARKMODULE => [0, 0, 0], // separator QRMatrix::M_SEPARATOR => [233, 233, 233], // quietzone QRMatrix::M_QUIETZONE => [233, 233, 233], // logo (requires a call to QRMatrix::setLogoSpace()), see QRImageWithLogo QRMatrix::M_LOGO => [233, 233, 233], ]; $data = 'https://dfstudio-d420.kxcdn.com/wordpress/wp-content/uploads/2019/06/digital_camera_photo-980x653.jpg'; $qrcode = (new QRCode($options))->render($data, __DIR__.'/qrOutput/qrCode.svg'); // default output is a base64 encoded data URI printf('<img src="%s" alt="QR Code" />', $qrcode);
With that I'm able to both show the generated qr code, and save it locally. The problem is that it saves it as a svg, not a png (jpeg would be fine too). Already tried changing the extension in the render() but the output is a corrupted file.
Beta Was this translation helpful? Give feedback.
All reactions
If it is still saved as SVG, this means it is using the SVG output class, which is the default output. And just now I realize that you might be using an example from dev-main with one of the v5.x releases. In dev-main (v6) the option outputType has been removed (see #223). You will need to add this setting and it should work fine:
$options->outputType = QROutputInterface::CUSTOM;
(you could also use one of the several constants that v5 provides for the several built-in output classes, but for simpler migration to future versions i wouldn't recommend this anymore)
See also https://github.com/chillerlan/php-qrcode?tab=readme-ov-file#documentation
Replies: 1 comment 7 replies
-
The example as you posted it here would save a PNG file, however, there's a .svg extension in the line where the QR Code is generated. Please note that the file extension you pass has absolutely no effect on the output format, the file is saved in the format defined by the output class, QRGdImagePNG in your case.
Beta Was this translation helpful? Give feedback.
All reactions
-
I have copy pasted the full code of the file in question, so yes, it's all in there
Beta Was this translation helpful? Give feedback.
All reactions
-
Hmm, I cannot reproduce the issue with the above code - the image is properly rendered as PNG. The visual output however is dumped as raw into the html image tag because the outputBase64 is set to false (this has no effect on file output).
Beta Was this translation helpful? Give feedback.
All reactions
-
Can it be because I'm missing some lib of some sort? Or maybe some use line that is supposed to be there? Tried again just now and it still save locally an svg file. If i change the extension in the render() with png it gives me a broken file that I'm not able to open with any app on my pc.
Beta Was this translation helpful? Give feedback.
All reactions
-
If it is still saved as SVG, this means it is using the SVG output class, which is the default output. And just now I realize that you might be using an example from dev-main with one of the v5.x releases. In dev-main (v6) the option outputType has been removed (see #223). You will need to add this setting and it should work fine:
$options->outputType = QROutputInterface::CUSTOM;
(you could also use one of the several constants that v5 provides for the several built-in output classes, but for simpler migration to future versions i wouldn't recommend this anymore)
See also https://github.com/chillerlan/php-qrcode?tab=readme-ov-file#documentation
Beta Was this translation helpful? Give feedback.
All reactions
-
Ok so, I found out what the problem was.
Shortrly yes, thanks 'cause your last comment fixed the issue.
I was having problems the whole time because the gd ext wasn't loaded correctly by php. After some work with the ini, everything is working as expected. Thank you so much.
Beta Was this translation helpful? Give feedback.