我已经在rails中设置了回形针,一切都工作正常(我实际上不得不谷歌......)。
然而我注意到Page Speed告诉我,我可以进一步压缩缩略图和大图像(回形针产生的图像)。我可以在我的模型中添加一个选项吗?我注意到mod_deflate不会压缩图像(我正在使用Firefox)。
答案 0 :(得分:10)
您可以使用paperclip-compression gem向回形针处理添加压缩。
在你的Gemfile中:
gem "paperclip-compression", "~> 0.1.1"
(当然是运行bundle install)
在你的模特中:
has_attached_file :avatar,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:processors => [:thumbnail, :compression]
“ jpegtran 通过重新排列压缩数据(DCT系数)来工作,而无需完全解码图像。因此,its transformations are lossless”
注意:如果你在heroku上运行,你需要jpegtran,并在你的应用程序中添加optipng二进制文件。这是关于running binaries on heroku的好文章。
答案 1 :(得分:1)
您应该对各种JPEG压缩级别进行自己的测试,但我注意到我可以将ImageMagicks的质量设置降低到75,但仍然没有看到任何明显的差异 - 节省大约30-40%的文件大小。
我的模型看起来像:
has_attached_file :photo,
:styles => {
:"185x138" => {
:geometry => "185x138>"
} },
:convert_options => {
:all => "-auto-orient",
:"185x138" => "-quality 75",
-quality 75
适用于ImageMagick。如果您使用的是其他处理器,则需要进行相应的调整。
答案 2 :(得分:0)
FFMPEG或AVCONV怎么样?
sudo apt-get install ffmpeg/avconv
=初始化程序
Paperclip.options[:command_path] = "/usr/bin/" # see `which ffmpeg`
=模态
after_save :compress_with_ffmpeg
def compress_with_ffmpeg
[:thumb, :original, :medium].each do |type|
img_path = self.avtar.path(type)
Paperclip.run("ffmpeg", " -i #{img_path} #{img_path}")
end
end