Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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('/').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?

like image 662
spacemonkey Avatar asked Jan 31 '11 13:01

spacemonkey


1 Answers

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.

like image 125
Winfield Avatar answered Sep 29 '22 20:09

Winfield