我编写了一个简单的代码,它接受一个图像的url,并将其大小调整后的版本上传到Amazon S3存储。代码如下所示:
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
虽然有效,但图片的上传质量非常低。可能有什么问题?
答案 0 :(得分:1)
看起来问题是主图像尺寸上的几何图形字符串,请尝试更改:
:styles => {
:original => "128x128#",
:thumb => "48x48#"
},
到
:styles => {
:original => "128x128>",
:thumb => "48x48#"
},
如果尺寸太大,应该只调整图像大小/转换。