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