Amit Dhamu

Software Engineer

Embed Images With Data URIs

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="<?php echo $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.

data uris / images / embed / php / base64

← Conditional Vs Ternary OnClick Function →