<?php
// png, jpg, jpeg, gif
function base64_encode_image ($filename=string,$filetype=string) {
if (file_exists($filename)) {
$imgbin = fread(fopen($filename, "r"), filesize($filename));
return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbin);
}else{
echo "le fichier n'existe pas";
}
}
$img_src = "./image/source.jpg";
?>
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>base64_encode</title>
</head>
<body>
<img src="<?php echo base64_encode_image ($img_src,'png'); ?>"/><br />
<div style="width:50%; height:100%; border:1px solid orange; margin:10px; word-wrap: break-word;"><?php echo base64_encode_image ($img_src,'png'); ?></div>
</body>
</html>
Intégration HTML:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA...">
Intégration CSS:
background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA...");