Skip to content

Instantly share code, notes, and snippets.

@artemave
Last active February 10, 2023 21:08
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save artemave/c20e7450af866f5e7735 to your computer and use it in GitHub Desktop.
Save artemave/c20e7450af866f5e7735 to your computer and use it in GitHub Desktop.
minimagick carrierwave round image (with white border)
# config/initializers/carrierwave.rb
require 'mini_magick'
module CarrierWave
module MiniMagick
# round _square_ image
def round
manipulate! do |img|
img.format 'png'
width = img[:width]-2
radius = width/2
mask = ::MiniMagick::Image.open img.path
mask.format 'png'
mask.combine_options do |m|
m.alpha 'transparent'
m.background 'none'
m.fill 'white'
m.draw 'roundrectangle 1,1,%s,%s,%s,%s' % [width, width, radius, radius]
end
overlay = ::MiniMagick::Image.open img.path
overlay.format 'png'
overlay.combine_options do |o|
o.alpha 'transparent'
o.background 'none'
o.fill 'none'
o.stroke 'white'
o.strokewidth 2
o.draw 'roundrectangle 1,1,%s,%s,%s,%s' % [width, width, radius, radius]
end
masked = img.composite(mask, 'png') do |i|
i.alpha "set"
i.compose 'DstIn'
end
masked.composite(overlay, 'png') do |i|
i.compose 'Over'
end
end
end
end
end
# somewhere in an uploader
varsion :email do
process :round
end
@nofxx
Copy link

nofxx commented Jun 27, 2015

Thank you, using it here: https://github.com/nofxx/yamg

@GreenAnts
Copy link

Thank you!

@aoredo
Copy link

aoredo commented Feb 26, 2016

Thanks a million!

@gunnar2k
Copy link

This is awesome. Thank you!

@jmuheim
Copy link

jmuheim commented Dec 7, 2016

You have varsion, should be version. ;-) Thanks!

@ericwhitlock
Copy link

Still finding this to be very useful. Thanks for sharing!

@vsvld
Copy link

vsvld commented May 26, 2022

thank you!!

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