Skip to content

Instantly share code, notes, and snippets.

@artemave
Last active February 10, 2023 21:08

Revisions

  1. artemave revised this gist Nov 12, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion carrierwave.rb
    Original file line number Diff line number Diff line change
    @@ -49,4 +49,6 @@ def round

    # somewhere in an uploader

    process :round
    varsion :email do
    process :round
    end
  2. artemave created this gist Nov 12, 2014.
    52 changes: 52 additions & 0 deletions carrierwave.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    # 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

    process :round