Skip to content

Instantly share code, notes, and snippets.

@karwank
Last active March 11, 2024 04:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save karwank/628ea766ee93defd12cfdecd157cca25 to your computer and use it in GitHub Desktop.
Save karwank/628ea766ee93defd12cfdecd157cca25 to your computer and use it in GitHub Desktop.
Carrierwave with Mini Magick images optimization.
module CarrierWave
module MiniMagick
def quality(percentage)
manipulate! do |img|
img.quality(percentage.to_s)
img = yield(img) if block_given?
img
end
end
def slimming combine_options = {}
manipulate! do |img|
img.combine_options do |cmd|
if @file.content_type == 'image/jpeg'
cmd.strip
cmd.sampling_factor "2x2"
cmd.depth "8"
cmd.filter "Lanczos"
cmd.unsharp "0.25x0.08+8.3+0.045"
cmd.dither "None"
cmd.posterize "136"
cmd.quality "80"
cmd.define "jpeg:fancy-upsampling=off"
cmd.interlace "JPEG"
cmd.colorspace "sRGB"
elsif @file.content_type == 'image/png'
cmd.quantize "transparent"
cmd.colors "255"
cmd.depth "8"
cmd.dither "FloydSteinberg"
cmd.define "png:compression-level=9"
cmd.define "png:exclude-chunk=all"
cmd.colorspace "sRGB"
end
append_combine_options cmd, combine_options
end
img = yield(img) if block_given?
img
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment