Active Storage image variants can run on two different engines: MiniMagic/Imagemagic or ruby-vips/libvips which variant options have different syntax. The following will work for MiniMagic/Imagemagic setup:

tested on Rails 6.0.3.2 Ruby 2.7.1 on 2020-09-29

class Medium < ApplicationRecord
  has_one_attached :image
end

Let say we upload an image

medium = Medium.create(params.require(:medium).permit(:image))

Here is the original image. Size is 1024x4246

medium.image.variant({ combine_options: { resize: "400x300^",  crop: '400x300+0+0' }})

Result

Order of combine options does matter !!! If I swap resize and crop this will happen

medium.image.variant({ combine_options: { crop: '400x300+0+0', resize: "400x300^" }})

Result

If you want to center

medium.image.variant({:combine_options=>{:gravity=>"Center", :resize=>"400x300^", :crop=>"400x300+0+0" }})

Result

If you need to rotate/auto-orient image:

medium.image.variant({:combine_options=>{:auto_orient=>true, :rotate=>"0", :gravity=>"Center", :resize=>"400x300^", :crop=>"400x300+0+0" }})

As For libvips yes you can do the same thing but I don’t have code to paste here. I’ll update this article once I have.

Discussion