Encode and Inline an Image for faster loading in PHP

by Sasikumar 2014-05-21 12:30:33

Encode an image for faster loading:
function encode_image($filename)
{
    $contents = file_get_contents($filename);
    $base64 = base64_encode($contents);
    $imagetype = exif_imagetype($filename);
    $mime = image_type_to_mime_type($imagetype);
    return "data:$mime;base64,$base64";
}
Usage:

echo "<img src='".encodeimg("images/userimage.jpg")."'/>";
81
like
0
dislike
0
mail
flag

You must LOGIN to add comments
Guna

Normal images links will be cached how about this?