Base64 for images a look

Using Base64 encoded images in website

Encode the image to base64 in php

<?php
$image = file_get_contents("https://www.facebook.com/images/fb_icon_325x325.png");
$base64 = base64_encode($image);
echo $base64; // This will echo the encoded base64 image
?>

Load images in website using php:

<?php
$image = file_get_contents("https://www.facebook.com/images/fb_icon_325x325.png");
$base64 = base64_encode($image);
echo $base64; // This will echo the encoded base64 image
?>
<img src= "data:image/png;base64,<?php echo $base64; ?>" >

Using css background:

background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUUAAAFFEAYAAAAr5CMeAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAIGNIUk0AAG2YAABzjgAA+3UAAITUAAB6swAA/2cAADBbAAAQhVDPV6EAABJcSURBVHja7N1/dNX1fcfxd0KI4TdECFiCAioERKER/EWk0lpEB5Ra6wSROeuPqBymlnlcZzsPc5Yq/uJQpa6lHkXKOudRjvO0mbNWA6KzqKttkSq1/AgQUtQkhJCEZH/ce5slhForP5Lcx+OfzzEnDfEVT336vbnfb8bEolmzlz0ch9mIEYlz6tTEOXFi4hyZ/PjxxyfOXr0TZ1ZWAACkpYaGxFlVmTg3b06c72xMnKVrEmdJSfLjGw7Xd3KIg6xrduKcMydx3nh94jx9vB86AMCflWfJPuuX2/IcOy5xXnppy89fvz5xLv1u4lyxInHW133a7yTz0PwNzZiRODf8JnEu/4FABAA4EgoLW/bXb36VOKfPOEqh2LNnslgfT5zPPJM4hw/3wwIAOJpOPClxrn6mZa+l+u2wheLg/MT5yiuJ8/I5fhgAAO1ZqtfWrGnZc4csFPOTX3DNy4lzzBijAwB0JKed1rLn8j82GD8mFFOXKJ/7z8R5wlAjAwB0ZKmeS/XdwV+S/phQTN0459TTjAoA0Jmk+u7gN0o8SCim3sXsdxABADq3VO8d+C7pVqGYug/i/fcbDQAgnTxwf8sePCAUUzfKdpsbAID0kuq/OXMOEoqpJ6kAAJCebri+VSgWFCROT1IBAEhv45M9OLIgGYpfnGIUAACaTZmSDMVzJxoDAIBmE89Oh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ymzqu+ljg96QUA4PB6Y33L/ioYlTgPfBfzJ5V1aL/R1H13fri85VlQkDinTEmcRcmXrkcmPz4kP3H26p38trL80AGA9NTQkDhTr+Bu3pw4N25MnKVrEmdJSeJMPYr50Pu/AQBZWmLMHTSOWQAAAABJRU5ErkJggg==);

Advantage of using base64:

The advantage lies in not having to open another connection and make a HTTP request to the server for the image. advantage for large numbers of very tiny individual images.

Disadvantages :

The actual length of MIME-compliant Base64-encoded binary data is usually about 137% of the original data length, Very roughly, the final size of Base64-encoded binary data is equal to 1.37 times the original data size + 814 bytes (for headers).

In other words, the size of the decoded data can be approximated with this formula:

<?php
$imagedata = file_get_contents("https://www.facebook.com/images/fb_icon_325x325.png");
$base64 = base64_encode($imagedata);
echo $bytes = (strlen($base64) -814 ) / 1.37;
?>

Source: http://en.wikipedia.org/wiki/Base64#MIME
If you do the base64 encoding from the source files with each request, you’ll be using up more CPU, thrashing your data caches, etc, which might hurt your servers response time.

Doing this will of course prevent most forms of caching, which could hurt a lot if the image is viewed often – say, a logo that is displayed on every page, which could normally be cached by the browser (or a proxy cache like squid or whatever) and requested once a month. It will also prevent the many many optimizations web servers have for serving static files using kernel APIs like sendfile

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from

Subscribe now to keep reading and get access to the full archive.

Continue reading