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.
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.
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.
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.
Encoding to/from Base64 is completely lossless. The quality loss happens probably when you save it. To prevent that, use an ImageWriter directly ( ImageIO.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With