Skip to content

Instantly share code, notes, and snippets.

@iqbalhasnan
Last active May 25, 2023 06:06
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save iqbalhasnan/9621174 to your computer and use it in GitHub Desktop.
Save iqbalhasnan/9621174 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
@jt535
Copy link

jt535 commented Aug 20, 2017

Thanks a bunch for this! I think that "exif_rotation" in "carrierwave.rb" should be "fix_exif_rotation" though.

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