spa*_*key 7 ruby-on-rails image paperclip
我编写了一个简单的代码,它获取了一个图像的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
Run Code Online (Sandbox Code Playgroud)
它有效,但图像上传的质量非常低.可能有什么问题?
看来问题出在主图像尺寸上的几何字符串,请尝试更改:
:styles => {
:original => "128x128#",
:thumb => "48x48#"
},
Run Code Online (Sandbox Code Playgroud)
到
:styles => {
:original => "128x128>",
:thumb => "48x48#"
},
Run Code Online (Sandbox Code Playgroud)
如果尺寸太大,则只能调整/转换图像大小。