'Convert string to image with base64 php

I have a string and I need to convert it to a base64 img using php, because my controller is used in multiples places of system, so i dont want to convert it in google chart or JS.

$string = '00020101021226930014br.gov.bcb.pix2571api-h.developer.btgpactual.com/v1/p/v2/50436e18635640a4a2ee3b30fab4035e5204000053039865802BR5925ZAZ TECNOLOGIA FINANCEIRA6013FLORIANOPOLIS61088807015062070503***6304C2FD';

<img src="data:image/png;base64,<?= $string ?>" id="imageQRCode" alt="QR Code" class="qrcodeImg"/>



Solution 1:[1]

If you want to convert the following string to base64 you can use the base64 internal PHP function (base64_encode()) or if you want to download an image from the URL and convert it to base64 you can use this code:

$url = 'address to the image url';
$image = file_get_contents($url);
$base64 = base64_encode($image);

<img src="data:image/png;base64,<?= $base64 ?>" id="imageQRCode" alt="QR Code" class="qrcodeImg"/>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 A.Seddighi