php source code to convert images into data/base64 data stream
Here we share a method to convert images into 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; } ?>
obtained after conversion through the above method Base64 encoded strings can be stored in the database and can be read directly from the database when needed, reducing the number of requests when accessing images.
Thank you for reading, I hope it can help you, thank you for your support of this site!
For more PHP source code to convert images into data/base64 data stream examples, please pay attention to the PHP Chinese website for related articles!