Home >Backend Development >PHP Tutorial >PHP source code converts images into data/base64 data flow example detailed explanation_php example
php source code to convert images into data/base64 data stream
Here we share a method to convert images to base64 encoding format:
<?php $img = 'test.jpg'; $base64_img = base64EncodeImage($img); echo '<img src="' . $base64_img . '" />'; /* 作者:http://www.manongjc.com */ function base64EncodeImage ($image_file) { $base64_image = ''; $image_info = getimagesize($image_file); $image_data = fread(fopen($image_file, 'r'), filesize($image_file)); $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data)); return $base64_image; } ?>
The base64 encoded string converted by the above method can be stored in the database and can be read directly from the database when needed, reducing the number of requests when accessing images.
Thanks for reading, I hope it can help everyone, thank you for your support of this site!