简体   繁体   中英

Lost data while converting image png to base64 string

I have an image.png which is generated dynamically (barcode) and which is returned to html as

<img src="data:image/png;base64,B64STR" />

but it sometimes get defects (white dots).

To convert image to base64 string I use this code:

    ob_start();
    imagepng($img);
    $contents = ob_get_contents();
    ob_end_clean();

    return base64_encode($contents);

Can be the output buffer be the cause of lost data? Can I convert the image to base64 some other way?

imagepng return a bool. Use file_get_contents without ob :

return base64_encode(file_get_contents($img));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM