d
Amit DhamuSoftware Engineer
 

Embed Images With Data URIs

1 minute read 00000 views
function getDataURI($image, $mime = '') {
    return 'data: ' . (function_exists('mime_content_type') ? mime_content_type($image) : $mime) . ';base64,' . base64_encode(file_get_contents($image));
}

$image = 'image.jpg';
$embed_image = getDataURI($image);

// Put it on screen
<img src="<?=$embed_image;?>" />

The main advantage of using Data URIs to embed almost anything onto your webpages is to minimise HTTP requests.

Credit goes to David Walsh for the function.