php image to base64 1

php image to base64

$path = 'myfolder/myimage.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

Here is what the above code is Doing:
1. We’re using the pathinfo() function to get the extension of the file.
2. We’re using the file_get_contents() function to read the file into a string.
3. We’re using the base64_encode() function to encode the string data into base64.
4. We’re creating a data URI for the image by concatenating the mime-type and base64 string.

Similar Posts