6

I am working on something where users can upload pictures(size of image is not limited). Now I have two options either compressing the image using PHP(Server side) or compressing the image on client's machine using JavaScript and then uploading it to the server. I wanted to ask which approach out of the two would be better to implement? Compression on server might lead to heavy load on the server so I thought of client side compression however if I upload an image of bigger size (lets assume 12MB or so) then browser freezes up for a while due to the script.

There is as such no code just a theoretical question. Currently I am using J-I-C for client side compression

Any other good library for client side image compression? and which approach would be better? Any help would be much appreciated.

Sjon
  • 4,989
  • 6
  • 28
  • 46
Yomesh
  • 288
  • 1
  • 4
  • 19
  • This question is rather broad and mostly opinionated. You should think about customer satisfaction as well. If my mobile is having to compress images for sending data its costing battery performance while only saving a minimum amount of bytes cause images are compressed already. – Xorifelse Mar 20 '16 at 10:11
  • @Xorifelse Yes I understand question is broad and opinionated but I couldn't think of a better place to ask the query. Yes battery performance would be there that is why I wanted to get an insight which approach would have a lesser trade off and better results. – Yomesh Mar 20 '16 at 10:16
  • You should always compress images on server side. You must be carefull with file upload due to security reasons. Search for code injection in image – instead Mar 20 '16 at 10:25

1 Answers1

9

As @Xorifelse said, the question might be "too broad", but here are some ideas.

Cons

  • user input must not be trusted; by doing compression on the client-side, you will have to do some sanity check on the server side anyway
  • image compression (or optimization) involve complex operations, there is less choice in JavaScript than in other languages
  • since the operations are complex, you are putting the stress on your clients; if you don't control their configuration (hardware, browser and vers), a situation you can have almost only in an intranet, you will probably degrade (or fail) the browsing experience of some of your users
  • for all those reasons, client-side bugs are harder to track and fix and can quickly cost you more in development than adding resources to your servers

Pros

  • you offload some computation from your servers
  • you help people with a small bandwidth but powerful computers and recent browser to upload large images

Image compression tools

JPEG only

BGP

My suggestion

  • if you need to spare your server, you can make the images optimization in batch, asynchronously at some time of the day when your server is not heavily loaded
  • if you have a lot of input, it can be cheaper to send the optimization to another server (ie: an on-demand virtual-machine at Amazon, DigitalOcean, Linode, etc., so you pay only when you need) than to upgrade your "main" server
bufh
  • 3,153
  • 31
  • 36