使用Paperclip圆角

时间:2012-01-09 14:17:51

标签: ruby ruby-on-rails-3 imagemagick paperclip

如何使用Paperclip创建圆角?我找到了这个创建rounded corners with paperclip using convert_options的解决方案,但它不适用于Rails 3和Paperclip 2.4.5。生成的convert命令仅在我使用ImageMagick alpha parameter而不是阈值参数时才有效:

  convert example.jpg \
     \( +clone  -alpha extract \
        -draw 'fill black polygon 0,0 0,15 15,0 fill white circle 15,15 15,0' \
        \( +clone -flip \) -compose Multiply -composite \
        \( +clone -flop \) -compose Multiply -composite \
     \) -alpha off -compose CopyOpacity -composite  rounded_corners.png

对应

has_attached_file :avatar,
                  :styles => { :medium => ["918x483#", :png] },
                  :convert_options => {:medium => Proc.new{self.convert_options}}

def self.convert_options(px = 15)
  trans = ""
  trans << " \\( +clone  -alpha extract "
  trans << "-draw 'fill black polygon 0,0 0,#{px} #{px},0 fill white circle #{px},#{px} #{px},0' "
  trans << "\\( +clone -flip \\) -compose Multiply -composite "
  trans << "\\( +clone -flop \\) -compose Multiply -composite "
  trans << "\\) +alpha off -compose CopyOpacity -composite "
end

此代码段似乎产生了正确的转换命令,但是提供了“错误处理stream-xyz的缩略图”。

Command :: convert '/tmp/stream20120109-15817-1lju7p6-0.jpg[0]' -resize "918x" 
  -crop "918x483+0+105" +repage \( +clone -alpha extract -draw 'fill black polygon 
  0,0 0,15 15,0 fill white circle 15,15 15,0' \( +clone -flip \) -compose 
  Multiply -composite \( +clone -flop \) -compose Multiply -composite \) +alpha off
  -compose CopyOpacity -composite '/tmp/stream20120109-15817-1lju7p6-....png'
[paperclip] An error was received while processing: #<Paperclip::PaperclipError: 
There was an error processing the thumbnail for stream20120109-15817-1lju7p6-0>

1 个答案:

答案 0 :(得分:4)

最后我发现它,这是一个简单的错字。您必须使用-alpha off而不是+alpha off,然后上面的代码才能正常运行。有时单个符号(而不是+,反之亦然)会产生差异。