如何使用Paperclip降低上传图像的质量?
我正在运行Ruby on Rails 3,我想使用Paperclip插件/ gem来降低上传图像的质量。 我怎样才能做到这一点?
此时在我的模型文件中我有:
has_attached_file :avatar, :styles => { :thumb => ["50x50#", :jpg], :medium => ["250x250#", :jpg], :original => ["600x600#", :jpg] }
将图像转换为.jpg
格式并设置尺寸。
尝试使用convert_options。
has_attached_file :avatar, :styles => { :thumb => '50x50#' }, :convert_options => { :thumb => '-quality 80' }
从回形针维基 ,有一个质量选项:
class User < ActiveRecord::Base has_attached_file :photo, :styles => { :small => { :geometry => '38x38#', :quality => 40, :format => 'JPG' }, :medium => { :geometry => '92x92#', :quality => 50 } end
正如James所说,一旦你通过在命令行上进行实验来找出传递给ImageMagick convert
的正确参数,你可以通过convert_options
选项将它们传递给Paperclip,就像在James的例子中一样。
如果您有多个参数,请将它们作为数组传递。 这是一个我努力了一段时间的例子:
:convert_options => {:medium => ["-shave", "2x2", "-background", "white", "-gravity", "center", "-extent", "530x322", "+repage"], :small => ["-shave", "1x1"] }
除了-quality
, ImageMagick
的-strip
选项可以从图像中删除所有轮廓和其他绒毛,这可能会减少更多的尺寸
has_attached_file :photo, :styles => { :thumb => "100x100#" }, :convert_options => { :thumb => "-quality 75 -strip" }