0

I have a website in which I let users upload images. I convert these images to data URLs through HTML5 and store them as text fields in a database: http://en.wikipedia.org/wiki/Data_URI_scheme

I figured this would reduce the time for page load since I need to make fewer HTTP requests even though the main HTML page would be much longer.

I'm suspicious though that these images stored in URLs take up more space in the database than their static counterparts on disk. I noticed that a data URL for image had 250K characters (so I assume stored in 250KB), but when I right-clicked and saved the same image on disk, the image was only 180K.

Do data URLs significantly inflate the memory required to store an image?

dangerChihuahua007
  • 20,299
  • 35
  • 117
  • 206

3 Answers3

2

Yes. Images stored as Data URIs go through a base64_encode process. Which inflates their size by approximately 30%.

You're approaching this wrong.

  • Store the files themselves on the disk, separated by folders. I like naming the images as hashes of their contents. That way you can avoid duplicates easily if you want.
  • Store data on those images on the database. Include the path, upload time, uploading user, position of Saturn's moons, and whatever other data you may want on the database, except for the image itself, which should be stored on the file system.
  • If you wish, you can generate the Data URI in real time (like I said, it's a simple base64_encode).
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
1

It can also take more than 250KB of space if you're using a multi-byte character encoding for your DB column. If they are 250K characters, it may be using between 500K and 1M of disk space.

Not only do they take up more space, but they can not be cached by the users browser independently of the page they are on. This can reduce performance significantly.

Daniel
  • 4,481
  • 14
  • 34
  • No, that's incorrect. Data URI uses base64_encode, which only include ASCII compatible characters. It means that no multibyte character can ever be a part of a Data URI :) – Madara's Ghost Feb 27 '14 at 23:25
  • It doesn't matter what characters are in the data, only what the DB has allocated room for. If it is using UTF-16 encoding, every character is at least 2 bytes long. "TEXT" fields may be better off, but VARCHAR fields typically allocate the largest possible string space. – Daniel Feb 27 '14 at 23:29
1

it's definitly not worth it memorywise. as said in other answers:

  • base64 encoded grow by about 30% (Second Rikudos Answer)
  • I would not recommend storing image data in a database at all, but there is controversy, see here
  • serving base64 encoded dataURLs in your main html may be ok for small images, but as we talk about user uploaded images, I doubt they are lightweight (no more than 10k, depends a bit). also, Daniels note about caching is weighting strong. but in fact, that's another question already.
Community
  • 1
  • 1
benzkji
  • 1,718
  • 20
  • 41