简体   繁体   中英

Rails Paperclip rotate image before uploading

My problem is simple: Before uploading an image to Amazon S3, I want to rotate it to 270°. I understood from researching that I have to use a "processor" to do this. Eg I tried a lot of things but got stuck.

My model looks like this:

has_attached_file :label,
  :storage => :s3,
  :bucket => 'my_bucket',
  :s3_permissions => :private,
  :s3_credentials => {
    access_key_id: Rails.application.secrets[:aws_access_key_id],
    secret_access_key: Rails.application.secrets[:aws_secret_access_key]
  },
  :s3_host_name => "s3-eu-west-1.amazonaws.com",
  :processors => [:rotator]

  def rotator
    self.rotation = 270
    self.save
  end

I think I misunderstood where to place this processor and how it works exactly. I thought I need to put it into the model directly and it gets called like a method? Then I can set the rotation of the uploading image (self) to the amount of degrees (270) and save it afterwards. But that won't work.

How can I realise this very simple?

I assume you have ImageMagick set up if you're doing this. The convert command accepts a whole bunch of options. I just tried convert screen.jpg -rotate 270 270.jpg in the console with success. With Paperclip you'll use convert_options . This will only work on converted images, so you need to explicitly define a style, eg original, thumb etc for conversion. Here's an example.

has_attached_file :image,
                url:        '/system/:class/:id/:style/image.:extension',
                styles: {
                    original: '650x',
                    thumb: '100x100'
                },
                convert_options: {
                    all: '-rotate 270 -strip -interlace Plane -quality 80%'
                }

One note is that -rotate may not be losslessly converted, so you might want to check the image quality and adjust accordingly.

旋转270度

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM