我有一个字符串,我需要使用 php 将其转换为 base64 图像,因为我的控制器用于系统的多个地方,所以我不想在谷歌图表或 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"/>
回答1
如果您想将以下字符串转换为 base64,您可以使用 base64 https://www.php.net/manual/en/function.base64-encode.php (base64_encode()
) 或者如果您想从 URL 下载图像并将其转换为 base64,您可以可以使用此代码:
$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"/>