How To Convert JPG, GIF, PNG, And WebP Images To Base64 In PHP With Example?

Convert JPG, GIF, PNG, And WebP Images To Base64 In PHP With Example

$file = "./image.jpg";
$path = pathinfo($file);
$ext = mb_strtolower($path['extension']);
 
if (in_array($ext, array('jpeg', 'jpg', 'gif', 'png', 'webp'))) {     
    $size = getimagesize($file);  
    $file_img = 'data:' . $size['mime'] . ';base64,' . base64_encode(file_get_contents($file));
}
?>
 
<img decoding="async" src="<?php echo $file_img; ?>">
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments