Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is It More Efficient to Put Raw Image Data in the Src Attr?

I recently found out that the src attribute of an image allows you to put raw base 64 image data straight into it. Am i right in supposing this is technically more efficient than a separate image file as additional requests for the images don't have to be made? Or is the overhead so small that it's not worth it?

Also, assuming i ended up doing this, what would be the best way to get that raw data? (out of, say, an image i whipped up in paint?)

like image 987
Thomas Shields Avatar asked May 12 '11 21:05

Thomas Shields


People also ask

What is the difference between data src and src?

src will render the value immediately and it's the default for images, videos using a single source and iframes. data-src is used when lazy loading to prevent the default image from loading when the page loads.

What purpose does the src attribute serve in an image element?

The <img> src attribute is used to specify the URL of the source image. Attribute Values: It contains single value URL which specifies the link of source image.

What is the correct format of src attribute?

Example: src="img_girl. jpg". If the URL begins with a slash, it will be relative to the domain. Example: src="/images/img_girl.

How does src attribute work?

The src attribute instructs the browser where on the server it should look for the image that's to be presented to the user. This may be an image in the same directory, an image somewhere else on the same server, or an image stored on another server. Here, ../ equates to “move up one directory in the hierarchy.”


2 Answers

It depends on what you mean by "more efficient". If your measure is time, then it can be more efficient.

The technique you're referring to is using a data URI. Typically you take the image data and base64 encode it so it contains only ASCII characters. base64 encoding data has the effect of making it 33% larger (every 6 bits become 8).

So this works for small images, but for large ones, the 33% premium may be too much.

The reason why it might be a good idea is that latency is often the limiting factor for browser requests. It used to be (back in the day) that bandwidth was the restriction, so the common advice was to split up your resources, but that's not true anymore. With a data URI image, the browser doesn't have to make a second round trip.

Aside from all that, you have to consider browser support. Prior to version 8, IE has no support for data URIs. And in IE 8, there's an upper limit of 32KB of data.

Hope this helps!

like image 53
jimbo Avatar answered Nov 16 '22 01:11

jimbo


There is a size limit to this. I don't know for certain off the top of my head, but 2K seems about right.

Remember that there is overhead for base64 encoding something. If you have a 500 byte image, this might be fine, but for other things, no.

Really though, you shouldn't be doing this right now for compatibility. Maybe in the coming years...

like image 43
Brad Avatar answered Nov 16 '22 02:11

Brad