使用Paperclip圆角 [英] Rounded corners with Paperclip
问题描述
如何使用Paperclip创建圆角?我找到了这个解决方案,使用convert_options创建使用回形针的圆角 ,但它不适用于Rails 3和Paperclip 2.4.5。仅当我使用 ImageMagick alpha参数而不是阈值参数时,生成的convert命令才有效。 :
How do you create rounded corners with Paperclip? I have found this solution which creates rounded corners with paperclip using convert_options, but it does not work with Rails 3 and Paperclip 2.4.5. The generated convert command works only if I use the ImageMagick alpha parameter instead of the threshold paramter:
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的缩略图。
This code snippet seems to produce the right convert command, but gives an "error processing the thumbnail for 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>
推荐答案
最后我发现它,这是一个简单的错字。您必须使用 -alpha off
而不是 + alpha off
,然后上面的代码可以正常工作。有时单个符号(而不是+,反之亦然)会产生差异。
Finally I found it out, it was a simple typo. You have to use -alpha off
instead of +alpha off
, then the code above works without error. Sometimes a single sign (- instead of + or vice versa) makes the difference.
这篇关于使用Paperclip圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!