[英]Rails 3, Paperclip and uploading image from remote url
I wrote simple code which takes url of an image, and uploads resized version of it to Amazon S3 storage. 我编写了一个简单的代码,它获取了一个图像的URL,并将其大小调整后的版本上传到Amazon S3存储。 Code looks like this: 代码如下所示:
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
It works, but the images are uploaded in a very low quality. 它有效,但图像上传的质量非常低。 What could be a problem? 可能有什么问题?
It looks like the issue is the geometry string on your main image size, try changing: 看起来问题是主图像大小上的几何字符串,请尝试更改:
:styles => {
:original => "128x128#",
:thumb => "48x48#"
},
to 至
:styles => {
:original => "128x128>",
:thumb => "48x48#"
},
Which should only resize/transform the image if the dimensions are too large. 如果尺寸太大,应该只调整图像大小/变换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.