Skip to content

Instantly share code, notes, and snippets.

@eveevans
Forked from iqbalhasnan/carrierwave.rb
Created November 29, 2018 00:18
Show Gist options
  • Save eveevans/94df24683e446e688f2fa0564914b799 to your computer and use it in GitHub Desktop.
Save eveevans/94df24683e446e688f2fa0564914b799 to your computer and use it in GitHub Desktop.
carrierwave mini_magick image processing - quality, strip, exif rotation, gaussian blur, interlace
#config/initializers/carrierwave.rb
module CarrierWave
module MiniMagick
# Rotates the image based on the EXIF Orientation
def exif_rotation
manipulate! do |img|
img.auto_orient
img = yield(img) if block_given?
img
end
end
# Strips out all embedded information from the image
def strip
manipulate! do |img|
img.strip
img = yield(img) if block_given?
img
end
end
# Tiny gaussian blur to optimize the size
def gaussian_blur(radius)
manipulate! do |img|
img.gaussian_blur(radius.to_s)
img = yield(img) if block_given?
img
end
end
# set the Interlace of the image plane/basic
def interlace(type)
manipulate! do |img|
img.interlace(type.to_s)
img = yield(img) if block_given?
img
end
end
# Reduces the quality of the image to the percentage given
def quality(percentage)
manipulate! do |img|
img.quality(percentage.to_s)
img = yield(img) if block_given?
img
end
end
end
end
# fix exif rotation before strip the exif data
process :fix_exif_rotation
process :strip
# set gaussion blur to optimize the image
process :gaussian_blur => 0.05
# default quality is 85
# process :quality => 85 # Percentage from 0 - 100
# set the interlace to plane for progressive jpeg
# this might increase the size of images a bit
process :interlace => Plane
# Create 820x820 fill image size
version :medium do
process :resize_to_limit => [820, 820]
process :interlace => Plane
# Create 380x380 fill image size
end
version :small do
process :resize_to_limit => [380, 380]
process :interlace => Plane
end
@Bivek
Copy link

Bivek commented May 5, 2023

If blur is not working for you, update the gaussian_blur method like below

def gaussian_blur(radius)
  manipulate! do |img|
    img.gaussian_blur("0x#{radius}")
    img = yield(img) if block_given?
    img
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment