Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: How to convert an image from URL to Base64?

Tags:

I want to convert image from its url to base64.

like image 396
Sharj Avatar asked Dec 03 '10 08:12

Sharj


People also ask

Can we convert image URL to Base64?

We can convert an image to a base64 URL by loading the image with the given URL into the canvas and then call toDataURL to convert it to base64.

How do you Base64 encode in PHP?

Syntax. base64_encode() function can encode the given data with base64. This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean such as mail bodies. Base64-encoded data can take about 33% more space than original data.

What is Base64 decode in PHP?

The base64_decode() is an inbuilt function in PHP which is used to Decodes data which is encoded in MIME base64. Syntax: string base64_decode( $data, $strict ) Parameters: This function accepts two parameter as mentioned above and described below: $data: It is mandatory parameter which contains the encoded string.


1 Answers

Do you want to create a data url? You need a MIME-Type and some other additional information then (see Wikipedia). If this is not the case, this would be a simple base64 representation of the image:

$b64image = base64_encode(file_get_contents('path/to/image.png')); 

Relevant docs: base64_encode()-function, file_get_contents()-function.

like image 118
jwueller Avatar answered Sep 28 '22 17:09

jwueller