How To 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));
}
?>
 

Leave a Comment