Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is base64 encoded image uploading a bad practice?

Is there a problem to use base64 encoding to upload (only upload) the image to the server? Considering the common image size of around 1-2 MB, not icon sized images. Is this a bad practice? Should it always use form data for image uploading?

The image would be sent inside a POST body (JSON content type) together with other data, like:

// POST /signup
{
  email: '[email protected]',
  password: '12345678',
  name: 'Example Name',
  picture: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAA...',
}

Once in the server, it would be sent to AWS bucket and get served as a binary file, not a base64 encoded string.

like image 263
Mateus Pires Avatar asked Mar 30 '19 23:03

Mateus Pires


People also ask

What are the drawbacks of using Base64 encoding?

The only downside is that base64 encoding will require around 33% more space than regular strings. So with base64 you can encode and transfer any sets of binary data through any system and then decode them to original binary data.

Is Base64 good for images?

Base64 is only useful for very small images. This is because when it comes to larger images, the encoded size of a picture in bytes will end up being much larger than JPEGs or PNG files.

Why is Base64 bad?

Although Base64 is a relatively efficient way of encoding binary data it will, on average still increase the file size for more than 25%. This not only increases your bandwidth bill, but also increases the download time.

Does Base64 encoding reduce quality?

Encoding to/from Base64 is completely lossless. The quality loss happens probably when you save it. To prevent that, use an ImageWriter directly ( ImageIO.


2 Answers

The generally accepted result of Base64 encoding a binary image is a result roughly 30% greater than the original. If the server limit is 2MB, you're effectively limited to a 1.4MB image as you're increasing it via encoding. Base64 isn't a compression method, it's just a method of getting binary data to a server over HTTP.

If you have control of the server, make it accept gzip compressed binary data instead, or if you can put the image somewhere, send the url of it in the request and the server can download it.

like image 113
codebrane Avatar answered Sep 20 '22 23:09

codebrane


Bas64 encoded images are good practice for a small size (KB) images. For bigger size image you will get size errors probably.

Although if you want to use (MB) size images, i suggest to pass them as a thumbnail.

Thumbnails are reduced-size versions of pictures or videos, used to help in recognizing and organizing them, serving the same role for images as a normal text index does for words

https://www.npmjs.com/package/image-thumbnail

like image 41
bembas Avatar answered Sep 21 '22 23:09

bembas