Advertisement:

Author Topic: [Tip] Imagemagick to optimize jpg  (Read 1847 times)

design

  • Hero Member
  • *****
  • Posts: 2619
  • Osclass 3.5 MAC/PC w/ Modern Browsers
[Tip] Imagemagick to optimize jpg
« on: November 21, 2014, 08:03:24 pm »
I found this little gem while surfing and thought it might be useful around here since we allow images on our sites...
https://coderwall.com/p/ryzmaa/use-imagemagick-to-create-optimised-and-progressive-jpgs

keeping the original url

Use the following command to optimise a JPG and make it progressive:

Code: [Select]
convert -strip -interlace Plane -quality 80 input-file.jpg output-file.jpg

Batch all the images in a folder like this:

Code: [Select]
for i in source/images/backgrounds/*.jpg; do convert -strip -interlace Plane -quality 80 $i $i; done


With Carrierwave and MiniMagick you can create an optimise function like this:

Code: [Select]
def optimize
  manipulate! do |img|
      return img unless img.mime_type.match /image\/jpeg/
      img.strip
      img.combine_options do |c|
          c.quality "80"
          c.depth "8"
          c.interlace "plane"
      end
      img
  end
end
And use it in your uploader like this:

Code: [Select]
version :large do
  process :optimize
end