Is base64 encoded image uploading a bad practice? - rest

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: 'example#email.com',
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 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

Related

Unable to Load image from url in flutter

I am Fetching images from URL and it is providing broken images while on the web the same URL is providing complete and accurate images.
Scheme not starting with alphabetic character
throwing error.
// Depending on where the exception was thrown, the image cache may not
// have had a chance to track the key in the cache at all.
// Schedule a microtask to give the cache a chance to add the key.
Image Url:- https://smott.world/upload/source/1646761765-WhatsApp Image 2022-03-08 at 5.41.29 PM.jpeg
This is happening because your url is not encoded, you can encode using Uri.encodeFull to encode. Encode url will replace white spaces by %20
Example:
var uri = Uri.encodeFull('https://smott.world/upload/source/1646761765-WhatsApp Image 2022-03-08 at 5.41.29 PM.jpeg');
var url = uri.toString();
// url will be "https://smott.world/upload/source/1646761765-WhatsApp%20Image%202022-03-08%20at%205.41.29%20PM.jpeg"
You can see more about percent encoding here

Alamofire Chunked Upload - How API knows when its the last chunk?

I have a Golang web server that I have written to handle large file uploads 30GB or more. In a proof of concept using Dropzone.js I can upload files of any size with no issue as long as they are chunked.
The way DropzoneJS.js implemented this is that each chunk has items added the to the header like:
dzchunkindex: 435
dzchunksize: 10000
dztotalchunkcount: 3498274
So I receive a chunk, I create the file (if needed), write the data, and check to see if I'm on the last chunk. Then repeat as needed. Once I see I've written the last chunk I close the file.
It seems like Alamofire supports chunked uploads using its AF.Upload method.
However, how should my server know when the last chunk has been uploaded? I can certainly check this a different way. Just curious what that way should be? Ive combed over the Alamofire docs and can't find much.
I can chunk the file manually and upload it but id rather use Alamofire if possible.
Thanks,
Ed

flic.kr/s/ short url for sets base58 issue

I am having trouble with flickr short urls, specifically those generated for sets.
Photos
I can see that for individual photos, if I select share and get the short url, they I would get flic.kr/p/{base58_encode(photoid)}. I can easily decode and encode the photoid and get the correct values.
Sets
I have tried the same for sets. If I select share on a set and get the short url, then I would get something like flic.kr/s/{base58_encode(setid)}. However, I am not sure that it is base58 encoded this time as decoding/encoding setid's with a base58 encoder/decoder does not return the correct values.
So, the question is, are the setid's encoded with a different encoder, or is there some sort of salt used with sets?
Many thanks in advance.

UIImage Base64 encoding fails

i'm trying to convert a UIImage which comes from the iPhones photo library to a base64 encoded string. My problem is that the output of the base64 encoded string does not match a base64 string which i've created on a online base64 encoder for jpegs.
Here is my code:
NSData *myAttachment = UIImageJPEGRepresentation(myImage, 1.0);
NSString *base64encodedAttachment = [attachment base64EncodingWithLineLenght:76];
While "attachment" is a string which will be part of an url request.
Why does the base64 code not match the "normal" from encoding websites encoding? Is there something special about UIImage oder NSData?
Cheers
It may happen that the UIImage is re-encoded when you call UIImageJPEGRepresentation and therefore it is not identical to the JPEG image you have uploaded to that online site. If you want to be 100% sure that the base64 encoder works as expected, save the JPEG representation that comes out of UIImageJPEGRepresentation to a JPEG file and upload that file to the online tool to obtain the base64 representation to compare your output with.
If you are trying to use this jpeg file in the messageBody of the mailComposer.. then i must clear you buddy...
You cannot use the local images inside the mailbody of mailComposer, the image shud be available on the server and used in the mailbody by passing url of the same..
If you're trying something else...i would say follow "Tamas" :)

XML data text + binary

I built an iPhone app that is getting information from a server (this is also a server that I built).
The data from the server is XML and I use the XML parser to parse the message.
What I want is to add an image to be sent from the server, and I am asking if I can add binary data of such an image to the XML message. For example 10 tags will be text and 1 tag will be binary (the image). So when the XML parser gets to the binary tag, it inserts the data to NSDATA object and the rest of the tags will be inserted to NSString.
Does the XML parser of Cocoa can handle this situation?
If not, what do you think will be the easiest way to do this with one connection to the server so that the data from the server is sent once.
To transfer binary data wrapped in XML, encode it using e.g. Base64, which turns your binary data into characters that won't mess up your XML.
You can transfer the image data, encoded using Base64. There is this NSData category by Matt Gallagher that adds Base64 decoding support to NSData (dateFromBase64String). You can find it on his Cocoa with love website.
Mind you that encoding images in Base64 adds about 33% in file size.