title: Rails 5 - Active Storage - Variant - Exception: "#<MiniMagick::Error: `mogrify -resize-to-fit [800, 800]"
body: <p>Rails 5.2.0 (as API)</p>
  
  <p><em>/config/application.rb</em></p>
  
  <blockquote>
    <p>config.active_storage.variant_processor = :vips</p>
  </blockquote>
  
  <p>Problem: </p>
  
  <p><em>/serializers/api/v1/user/current_user_serializer.rb</em></p>
  
  <pre><code>class Api::V1::User::CurrentUserSerializer &lt; Api::V1::User::BaseSerializer
    include Rails.application.routes.url_helpers
  
    attributes(
      [...]
      :avatar
      :created_at
    )
  
    def avatar
      if object.avatar.attachment
        avatar = {
          image: url_for( object.avatar ), # This one works
          thumb: url_for( object.avatar.variant(resize_to_fit: [800, 800]) ), # EXCEPTION
          thumb_test: url_for( object.avatar.variant(resize: '800x800') ) # Returns image of size: 640x800 (expected 800x800)
        }
      end
    end
  end
  </code></pre>
  
  <p>I get the following exception:</p>
  
  <pre><code>exception: "&lt;MiniMagick::Error: `mogrify -resize-to-fit [800, 800] /tmp/mini_magick20180625-19749-rghjbg.jpg` failed with error: mogrify.im6: unrecognized option `-resize-to-fit' @ error/mogrify.c/MogrifyImageCommand/5519. &gt;"
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>Thanks @George Claghorn</p>
  
  <p>I now created my own variant based on this post:
  <a href="https://prograils.com/posts/rails-5-2-active-storage-new-approach-to-file-uploads" rel="noreferrer">https://prograils.com/posts/rails-5-2-active-storage-new-approach-to-file-uploads</a></p>
  
  <p><em>lib/active_storage_variants.rb</em></p>
  
  <pre><code>class ActiveStorageVariants
    class &lt;&lt; self
      def resize_to_fill(width:, height:, blob:, gravity: 'Center')
        blob.analyze unless blob.analyzed?
  
        cols = blob.metadata[:width].to_f
        rows = blob.metadata[:height].to_f
        if width != cols || height != rows
          scale_x = width / cols
          scale_y = height / rows
          if scale_x &gt;= scale_y
            cols = (scale_x * (cols + 0.5)).round
            resize = cols.to_s
          else
            rows = (scale_y * (rows + 0.5)).round
            resize = "x#{rows}"
          end
        end
  
        {
          resize: resize,
          gravity: gravity,
          background: 'rgba(255,255,255,0.0)',
          extent: cols != width || rows != height ? "#{width}x#{height}" : ''
        }.merge(optimize_hash(blob))
      end
    end
  end
  </code></pre>
  
  <p><em>/models/concerns/users/active_storage_variants.rb</em></p>
  
  <pre><code>require 'active_storage_variants' # /lib/active_storage_variants.rb
  
  module Users::ActiveStorageVariants
  
    def avatar_thumbnail
      variation = ActiveStorage::Variation.new(
        ActiveStorageVariants.resize_to_fill(
          width: 300, height: 300, blob: avatar.blob
        )
      )
      ActiveStorage::Variant.new(avatar.blob, variation)
    end
  end
  </code></pre>
  
  <p><em>/models/user.rb</em></p>
  
  <pre><code>class User &lt; ApplicationRecord
    ...
  
    ## Concerns
    include Users::ActiveStorageVariants
  
    ...
  end
  </code></pre>
  
  <p>To call it: </p>
  
  <blockquote>
    <p>user.avatar_thumbnail</p>
  </blockquote>