Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Base64 to png #290

Unanswered
oskay12 asked this question in Q&A
Nov 27, 2024 · 2 comments · 2 replies
Discussion options

hello im storing base64 formats to db. Now i need to download QR as png. is it possible to convert base64 code to png ?

You must be logged in to vote

Replies: 2 comments 2 replies

Comment options

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

You must be logged in to vote
0 replies
Comment options

actually the header starts with svg

You must be logged in to vote
2 replies
Comment options

I mean the base64 that i store is svg

Comment options

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /