How can base64 encoding be used to handle images in PHP?

Base64 encoding can be used to handle images in PHP by converting the image file into a base64 encoded string. This allows the image data to be easily transmitted as text, making it suitable for embedding in HTML or CSS. To achieve this, you can read the image file using file_get_contents(), encode it using base64_encode(), and then use the resulting string in your application.

// Read image file
$image = file_get_contents('image.jpg');

// Encode image to base64
$base64_image = base64_encode($image);

// Output base64 encoded image
echo '<img src="data:image/jpeg;base64,' . $base64_image . '" />';