-
-
Notifications
You must be signed in to change notification settings - Fork 319
-
hello im storing base64 formats to db. Now i need to download QR as png. is it possible to convert base64 code to png ?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 2 replies
-
You mean converting the base64 URI to a PNG? In that case you need to cut off the header part and then decode the base64, which gives you the raw PNG, like so:
// the URI from your database $base64URI = 'data:image/png;base64,<base64 encoded data...>'; // explode on the comma (this is safe because there is only one occurrence) // and take the second part of the array $base64 = explode(',', $base64URI)[1]; // decode the image data $rawPNG = base64_decode($base64); // output with proper header header('Content-Type: image/png'); echo $rawPNG;
See also:
https://developer.mozilla.org/en-US/docs/Web/URI/Schemes/data
https://en.wikipedia.org/wiki/Data_URI_scheme
Beta Was this translation helpful? Give feedback.
All reactions
-
actually the header starts with svg
Beta Was this translation helpful? Give feedback.
All reactions
-
I mean the base64 that i store is svg
Beta Was this translation helpful? Give feedback.
All reactions
-
In that case it's an SVG image, which you can't convert to PNG that simple. You have to either change the output method for the QR Codes to PNG or convert the SVG, in which case you'll need ImageMagick or external tools (e.g. Inkscape). See the converter example:
https://github.com/chillerlan/php-qrcode/blob/main/examples/imagickConvertSVGtoPNG.php
Beta Was this translation helpful? Give feedback.