How to convert an image to Base64 encoding
To convert an image to base64 encoding in PHP, you can use the base64_encode() function. Here's an example of how to do it:
<?php
$image = file_get_contents('image.jpg');
$imageData = base64_encode($image);
echo $imageData;
Watch a video course
Learn object oriented PHP
This will print the base64-encoded image data to the screen. If you want to save the base64-encoded image to a file, you can use the file_put_contents() function like this:
<?php
$image = file_get_contents('image.jpg');
$imageData = base64_encode($image);
file_put_contents('image_encoded.txt', $imageData);
This will save the base64-encoded image data to a file called image_encoded.txt
.
Keep in mind that the base64-encoded image will be much larger than the original image, so it may not be suitable for all use cases.