How to convert an image to Base64 encoding
Also use this way to represent an image in Base64-encoded format...
Find the PHP function file_get_content
and next use the function base64_encode
.
And get the result to prepare str as data:" . file_mime_type . " base64_encoded string
. Use it in the img
src
attribute. The following code ma be helpful:
// A few settings$img_file = 'raju.jpg';// Read image path, convert to base64 encoding$imgData = base64_encode(file_get_contents($img_file));// Format the image SRC: data:{mime};base64,{data};$src = 'data: '.mime_content_type($img_file).';base64,'.$imgData;// Echo out a sample imageecho '<img src="'.$src.'">';