• Skip to main content
  • Skip to primary sidebar
  • Home
  • Professional Blog
  • Contact Me

Jesse Smith, MBA

Web developer & business consultant

You are here: Home / Web Design / Performance implications of serving base64 encoded images

April 10, 2014 by Jesse Smith, MBA

Performance implications of serving base64 encoded images

This started when I was trying to build a secure system that would store user input.  The fundamental question is, if you allow authenticated users to upload image files and store them on a web server, how do you then prevent someone else from viewing those files?  In the process of attempting to answer the question, I checked to see how Facebook does it; and I realized that they do not.  No, not at all.  Anyone with the direct URL can view any of the photos that you or anyone else has uploaded to Facebook, regardless of whatever your privacy settings might happen to be.  The person viewing the images does not even have to be logged in to Facebook.  They just enter the image URL in any browser, and there it is.

So, OK, the next question is how to build a system that’s more secure than Facebook?  (Because unlike our Mr. Zuckerberg, I actually believe in that whole privacy thing, you know, the 4th Amendment and whatnot…)

The thing is, if the image is served the usual way, as a src attribute URL in a standard <img> tag, it’s very difficult to prevent someone from simply accessing the direct URL.  That’s the weakness I observed with Facebook.  Of course, the  system’s file server could (and should) at least check to see if the user has a login cookie set; this can be done in Apache’s access directives, or in an .htaccess file on shared hosting.  Facebook does not even take this basic step.  That said, it is trivial for anyone who knows what they are doing to create a fake authentication cookie; and if they are accessing an image file directly, then their request will not call the script that would otherwise check the validity of the token.

But what if the image were to be encoded, stored in the database, and served as data embedded in the web page?  In that case, the user would only be able to access the image if they requested the page itself.  In that case, the script to authenticate the login token would be called before any image or other information was displayed.  This would clearly be a more secure way to serve an image file.

But what about the performance penalty?

Enter benchmark testing.

My first test only checks the performance implications of serving a base64 encoded image file.  This test does NOT check the performance implications of retrieving the file data from the database.  I’ll post that in a follow-up.

The script in this case is so simple, it’s not even worth posting it on GitHub.  This is all there is to it:
$file = 'image.png';
$start = (float) $_SERVER['REQUEST_TIME'];
for($i=0; $i<10; $i++){
// based on a function suggested by commenter luke at lukeoliff.com
// from the manual at http://us3.php.net/manual/en/function.base64-encode.php
$imgbinary = fread(fopen($file, "r"), filesize($file));
echo '<img alt="blah" src = "data:image/png;base64,' . base64_encode($imgbinary) . '" />';
}
$finish = microtime(true);
$elapsed = $finish - $start ;
echo 'Completed the request in ' . $elapsed . ' seconds.';
// now start over
$start = microtime(true);
for($i=0; $i<10; $i++){
$file = 'image.png';
echo '<img alt="blah" src="'. $file . '" />';
}
$finish = microtime(true);
$elapsed = $finish - $start ;
echo 'Completed the request in ' . $elapsed . ' seconds.';

I note that several commenters in different forums have pointed out that cURL is significantly faster than fopen().  However, I think they were talking about calling fopen over the network.  cURL doesn’t seem to be designed for use with local files.

The upshot is that even with the overhead of both encoding and then parsing the encoded image files, the base64-encode version of this script suffered a performance penalty of a mere 0.006 seconds.  That’s fast enough for even the toughest of performance junkies.

This proves the viability of serving base64 encoded files to the user.  But what about retrieving the data from the database?

To be continued.

 

Filed Under: Web Design Tagged With: technology, web design

Primary Sidebar

Categories

  • Announcements (3)
  • Events (8)
  • Reviews (8)
  • Sales and Marketing (2)
  • Security (4)
  • Thoughts (13)
  • Tips (24)
  • Unimportant (6)
  • Web Design (19)
  • Web server administration (5)

Archives

  • October 2017
  • September 2017
  • August 2017
  • November 2016
  • August 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • June 2013
  • May 2013
  • March 2013
  • February 2013
  • December 2012
  • October 2012
  • September 2012
  • August 2012
  • May 2012
  • April 2012
  • March 2012
  • January 2012
  • November 2011
  • October 2010
  • August 2010
  • June 2010
  • April 2010

Recent Posts

  • Direct Mail Campaign Components
  • Introduction to Multi Networks for WordPress Multisite
  • How to Modify a WordPress Plugin
  • Always Changing
  • Putting the Plan in Action!

Copyright © 2026 · Workstation Pro on Genesis Framework · WordPress · Log in