Skip to content

Commit

Permalink
Add the ability to directly invoke rounding method
Browse files Browse the repository at this point in the history
  • Loading branch information
Billy He committed Feb 27, 2014
1 parent 34df8fa commit c7968f9
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions round_corners.rb
@@ -1,29 +1,25 @@
module Paperclip
class RoundCorners < Paperclip::Thumbnail

def parse_opts(key)
opt = @options["border_radius_#{key}".to_sym] || @options["border_radius_#{key.delete('_')}".to_sym] || @options[:border_radius]
opt.nil? ? nil : opt.to_i
end

def transformation
trans = " \\( +clone -threshold -1 "
trans << "-draw 'fill black polygon 0,0 0,#{@topleft} #{@topleft},0 fill white circle #{@topleft},#{@topleft} #{@topleft},0' " unless @topleft.nil?
trans << "-draw 'fill black polygon #{@left},0 #{@left},#{@topright} #{@left-@topright},0 fill white circle #{@left-@topright},#{@topright} #{@left},#{@topright}' " unless @topright.nil?
trans << "-draw 'fill black polygon 0,#{@bottom} #{@bottomleft},#{@bottom} 0,#{@bottom-@bottomleft} fill white circle #{@bottomleft},#{@bottom-@bottomleft} #{@bottomleft},#{@bottom}' " unless @bottomleft.nil?
trans << "-draw 'fill black polygon #{@left},#{@bottom} #{@left},#{@bottom-@bottomright} #{@left-@bottomright},#{@bottom} fill white circle #{@left-@bottomright},#{@bottom-@bottomright} #{@left},#{@bottom-@bottomright}' " unless @bottomright.nil?
trans << "\\) +matte -compose CopyOpacity -composite "
end

def round_corners(dst)
options = [
"#{ File.expand_path(@thumbnail.path) }[0]",
def self.round(source, destination, topleft, topright, bottomleft, bottomright)
# Need to `-1` becuase when drawing, the coordinates start from 0
left = Paperclip::Geometry.from_file(source).width.to_i - 1
bottom = Paperclip::Geometry.from_file(source).height.to_i - 1

transformation = " \\( +clone -threshold -1 "
transformation << "-draw 'fill black polygon 0,0 0,#{topleft} #{topleft},0 fill white circle #{topleft},#{topleft} #{topleft},0' " unless topleft.nil?
transformation << "-draw 'fill black polygon #{left},0 #{left},#{topright} #{left-topright},0 fill white circle #{left-topright},#{topright} #{left},#{topright}' " unless topright.nil?
transformation << "-draw 'fill black polygon 0,#{bottom} #{bottomleft},#{bottom} 0,#{bottom-bottomleft} fill white circle #{bottomleft},#{bottom-bottomleft} #{bottomleft},#{bottom}' " unless bottomleft.nil?
transformation << "-draw 'fill black polygon #{left},#{bottom} #{left},#{bottom-bottomright} #{left-bottomright},#{bottom} fill white circle #{left-bottomright},#{bottom-bottomright} #{left},#{bottom-bottomright}' " unless bottomright.nil?
transformation << "\\) +matte -compose CopyOpacity -composite "

Paperclip.run('convert', [
"#{File.expand_path(source.path)}[0]",
transformation,
"#{ File.expand_path(dst.path) }"
].flatten.compact.join(" ")
"#{File.expand_path(destination.path)}"
].flatten.compact.join(" "))

Paperclip.run('convert', options)
dst
destination
end

def initialize(file, options = {}, attachment = nil)
Expand All @@ -39,18 +35,19 @@ def initialize(file, options = {}, attachment = nil)
@process = @topleft || @topright || @bottomleft || @bottomright
end

def parse_opts(key)
opt = @options["border_radius_#{key}".to_sym] || @options["border_radius_#{key.delete('_')}".to_sym] || @options[:border_radius]
opt.nil? ? nil : opt.to_i
end

def make
@thumbnail = super

if @process
# Need to `-1` becuase when drawing, the coordinates start from 0
@left = Paperclip::Geometry.from_file(@thumbnail).width.to_i - 1
@bottom = Paperclip::Geometry.from_file(@thumbnail).height.to_i - 1

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

return round_corners(dst)
return Paperclip::RoundCorners.round(@thumbnail, destination, @topleft, @topright, @bottomleft, @bottomright)
else
return @thumbnail
end
Expand Down

0 comments on commit c7968f9

Please sign in to comment.