Hello Reader's!
If you want to to encode an image with base64 then by using PHP you can do such. You can use the code as below:-
This method is very simple and commonly used:
function getDataURI($imagePath) {
$finfo = new finfo(FILEINFO_MIME_TYPE);
$type = $finfo->file($imagePath);
return 'data:'.$type.';base64,'.base64_encode(file_get_contents($imagePath));
}
Now you have declared the function and you can use the above function like below:
echo '<img src="'.getDataURI('./images/my-file.svg').'" alt="">';
echo '<img src="'.getDataURI('./images/my-file.png').'" alt="">';
Note: The Mime-Type of the file will be added automatically.
0 Comment(s)