Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an image to Base64 encoding

Tags:

php

image

base64

How can I convert an image from a URL to Base64 encoding?

like image 460
Volatil3 Avatar asked Oct 19 '10 10:10

Volatil3


2 Answers

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); 
like image 83
Ronny Sherer Avatar answered Oct 03 '22 18:10

Ronny Sherer


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.

like image 25
Pekka Avatar answered Oct 03 '22 18:10

Pekka