Class: Paperclip::RoundCorners

Inherits:
Thumbnail
  • Object
show all
Defined in:
lib/carousel/paperclip/round_corners.rb

Overview

Paperclip processor that uses ImageMagick to create rounded corners.

Derived from github.com/kerinin/paperclip-rounded-corners

Instance Method Summary collapse

Constructor Details

#initialize(file, options = { }, attachment = nil) ⇒ RoundCorners

Returns a new instance of RoundCorners


37
38
39
40
41
42
# File 'lib/carousel/paperclip/round_corners.rb', line 37

def initialize(file, options = { }, attachment = nil)
  super file, options, attachment

  @radius = options[:border_radius]
  @process = @radius
end

Instance Method Details

#makeObject


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/carousel/paperclip/round_corners.rb', line 45

def make
  @thumbnail = super

  if @process
    @width  = Paperclip::Geometry.from_file(@thumbnail).width.to_i
    @height = Paperclip::Geometry.from_file(@thumbnail).height.to_i

    dst = Tempfile.new([@basename, @format].compact.join("."))
    dst.binmode

    return round_corners(dst)
  else
    return @thumbnail
  end
end

#parse_opts(key) ⇒ Object


9
10
11
12
# File 'lib/carousel/paperclip/round_corners.rb', line 9

def parse_opts(key)
  opt = @options[:border_radius]
  opt.nil? ? nil : opt.to_i
end

#round_corners(dst) ⇒ Paperclip::Attachment

Performs the ImageMagick corner-roundage.

Parameters:

  • dst (Paperclip::Attachment)

    The Paperclip image.

Returns:

  • (Paperclip::Attachment)

    `dst`.


30
31
32
33
34
# File 'lib/carousel/paperclip/round_corners.rb', line 30

def round_corners(dst)
  command = "\"#{ File.expand_path(@thumbnail.path) }[0]\" #{ transformation } \"#{ File.expand_path(dst.path) }\""
  Paperclip.run('convert', command)
  dst
end

#transformationString

Returns The ImageMagick command that rounds an image's corners.

Returns:

  • (String)

    The ImageMagick command that rounds an image's corners.


16
17
18
19
20
21
22
23
# File 'lib/carousel/paperclip/round_corners.rb', line 16

def transformation
  trans = " \\( +clone  -threshold -1 "
  trans << "-draw 'fill black polygon 0,0 0,#{@radius} #{@radius},0 fill white circle #{@radius},#{@radius} #{@radius},0' "
  trans << "\\( +clone -flip \\) -compose Multiply -composite "
  trans << "\\( +clone -flop \\) -compose Multiply -composite "
  trans << "\\) +matte -compose CopyOpacity -composite "
  trans
end