How to convert an image to base64 encoding?

Volatil3 picture Volatil3 · Oct 19, 2010 · Viewed 424.1k times · Source

Can you please guide me how can I convert an image from a URL to base64 encoding?

Answer

Ronny Sherer picture Ronny Sherer · Dec 7, 2012

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);