Php Base64 Encoded Image From Url

If you want to speed up your web site, you should to read this article.


Base64 is an encoding format that represent binary data. Using base64 encoding to decrease network request. Thus,

// A few settings
$image = ‘cricci.jpg’;

// Read image path, convert to base64 encoding
$imageData = base64_encode(file_get_contents($image));

// Format the image SRC: data:{mime};base64,{data};
$src = ‘data: ‘.mime_content_type($image).’;base64,’.$imageData;

// Echo out a sample image
echo ‘<img src="’,$src,’">’;

With the image data in base64 format, the last step is placing that data within the data URI format, including the image’s MIME type. This would make for a good function:

function getDataURI($image, $mime = ”) {
return ‘data: ‘.(function_exists(‘mime_content_type’) ? mime_content_type($image) : $mime).’;base64,’.base64_encode(file_get_contents($image));

You can learn more information about base64 encode images