7
我编写了一个简单的代码,它将图像的url,并将其大小版本上传到Amazon S3存储。代码如下所示:Rails 3,回形针和从远程URL上传图像
attr_accessor :profile_image_url
has_attached_file :avatar,
:default_url => "/system/avatars/:style_default.png",
:styles => {
:original => "128x128#",
:thumb => "48x48#"
},
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "/avatars/:id/:style.:extension"
before_validation :download_profile_pic
...
def download_profile_pic
begin
io = open(URI.parse(self.profile_image_url))
def io.original_filename; base_uri.path.split('/').last; end
self.avatar = io.original_filename.blank? ? nil : io
rescue Timeout::Error
self.avatar = nil
rescue OpenURI::Error => e
self.avatar = nil
end
end
它的工作原理,但图像的质量非常低上传。什么可能是一个问题?
你的原始图像将变成“128x128”像素,这是你的意图?定义低质量,压缩率? – andrea 2011-01-31 13:35:21