What are odds of two photos (large) having same byte count?
I have an app takes product photos and saves them back to server. Now it is expensive to send back large file and convert format etc. So I was wondering how I can check if previously uploaded photo is same or not? What are odds they have same number of bytes if they are same resolution? Also what is most efficient way to upload a photo via HTTP? should it 开发者_开发知识库be compressed etc Thanks
Rather than depending on byte size of two images you could create MD5 hashes (or other hash algorithms) of those images and compare the hash values. Hash collisions are much, much less likely than image size comparisons would be.
You should only bother compressing if an image can be significantly compressed. Otherwise you're really just wasting CPU on the client and the server.
'Same size' is a necessary but not sufficient requirement for an equal picture. You have to truly look at the contents of the bytes. md5sum is a fingerprint option to verify equality.
As mentioned by Paul Sasi compressing won't help a lot, because most used file-formats (PNG,JPG, etc.) are already compressed and optimized to consume as least memory as possible.
There is another option to limit the upload size: In most cases clients are pushing far too high resolution sizes to servers in a pic-upload form (e.g. they take files straight from their digicam, which are meant for printing not for online consumption). This often ends up with pics >2MB in size. A big optimization would be to do a resize of the pictures on client's side (let's say from 4MB to 200KB). Unfortunately I forgot the name of the library (I will add it when it came to my mind again).
精彩评论