How can I convert an image from a URL to Base64 encoding?
I think that it should be:
$path = 'myfolder/myimage.png'; $type = pathinfo($path, PATHINFO_EXTENSION); $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
Easy:
$imagedata = file_get_contents("/path/to/image.jpg"); // alternatively specify an URL, if PHP settings allow $base64 = base64_encode($imagedata);
Bear in mind that this will enlarge the data by 33%, and you'll have problems with files whose size exceed your memory_limit
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With