开发者

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. 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('/').las开发者_如何转开发t; 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜