2

We use images in lots of places within the site:

  1. header image for articles
  2. image for the avatar

and so forth.

I have the control to make sure an image doesn't exceed 5mb. However, the page takes to long to load.

We use Ruby to produce the APIs and Angular above it. We've made lots of optimizations, we moved to use CDNs to lower the loads of the js files.

The images are the only problem now. I don't want to use the paperclip Gem as we already use dragonfly.

I can use a background process to work with every uploaded image, but I seek a more clean and convenient way.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
ahmgeek
  • 201
  • 1
  • 8
  • 16

2 Answers2

5

I would recommend ImageMagick for this task. You can strip out the EXIF headers and other extraneous information to reduce the filesizes using -strip option.

convert input.jpg -strip output.jpg

Or use mogrify to do all images in one go like this

mogrify -strip *.jpg 

You can also specify a lower quality to use to reduce the filesize, like this

mogrify -quality 70% *.jpg

I find the better option though, is to specify a maximum filesize (in kB) and let ImageMagick figure out the best quality it can achieve whilst not exceeding that file size, like this:

mogrify -strip -define jpeg:extent=100kb *.jpg

which will strip EXIF information and also reduce the filesize to 100kB max.


If you want a way to do something similar with Python, I wrote an answer that works pretty well here. It does a binary search for a JPEG quality that satisfies a maximum size requirement.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thanks @Mark, I didn't accept it yet as I still experiment it. Please, if you have another way share it, the more options the more goodness. – ahmgeek Feb 18 '15 at 20:27
  • No problem! Take your time and try it out :-) – Mark Setchell Feb 18 '15 at 20:32
  • `mogrify -strip -define jpeg:extent=100kb *.jpg` how can I do the same for png, or bitmap for instance without repeating my self? and should I convert all images to png first and starts striping them ? or just let the extension as is and process what ever comes in – ahmgeek Feb 21 '15 at 16:57
  • The parameter `jpeg:extent` only exists and works for `JPG` files because only they support the concept of trading quality for size. BMP and PNG files only support lossless compression with no option to reduce quality in order to reduce size. You may have to look to another tool for those formats. – Mark Setchell Feb 21 '15 at 17:01
0

You can use ImageMagick Batch resize

mogrify -resize 50% *
sansarp
  • 1,446
  • 11
  • 18