ahmgeek
ahmgeek

Reputation: 201

Best image optimization technique for Rails

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.

Upvotes: 2

Views: 3064

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 207670

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.

Upvotes: 5

sansarp
sansarp

Reputation: 1466

You can use ImageMagick Batch resize

mogrify -resize 50% *

Upvotes: 0

Related Questions