Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic image trim with background #96

Closed
AlecRust opened this issue Jan 14, 2022 · 2 comments
Closed

Basic image trim with background #96

AlecRust opened this issue Jan 14, 2022 · 2 comments

Comments

@AlecRust
Copy link

We use image_processing with ImageMagick currently, but I'm looking to switch to faster VIPS now it's default in Rails 7.

I'm struggling with a basic equivalent "trim" to remove excess pixels (white in this case) around a product image:

image

With ImageProcessing::MiniMagick these options passed to the image variant work to trim it:

{
  fuzz: '1%',
  trim: true
}

The equivalent in VIPS looks to be find_trim but this doesn't work when I pass it to ImageProcessing::Vips:

{
  find_trim: true
}

# OR

{
  find_trim: {
    threshold: 10
  }
}

I initially created this issue at libvips/ruby-vips#327 but it seems maybe things like this trim function would need to be added as a feature to image_processing in order to work in a similar way?

@nikolasgd
Copy link

This was the one thing keeping me from migrating away from Paperclip to ActiveStorage.

Being not familiar with the code and abstraction layers of ActiveStorage, libvips and image_processing I found it very hard and time consuming to implement this simple feature. However, here is a solution for anyone who comes across this:

Create this file:

# lib/image_processing/vips/processing.rb

require "image_processing"

module ImageProcessing
  module Vips
    module Processing
      extend ActiveSupport::Concern

      included do
        def crop_whitespace
          coordinates = image.find_trim(threshold: 7, background: 0)
          image.crop(*coordinates)
        end
      end
    end
  end
end

Load it from an initializer:

# config/initializers/image_processing.rb

Rails.configuration.after_initialize do
  ImageProcessing::Vips::Processor.include(ImageProcessing::Vips::Processing)
end

And finally, this is how you use in your model with ActiveStorage named variants (Rails 7 required):

has_one_attached :logo do |blob| 
  blob.variant :small, crop_whitespace: true, resize_to_fit: [110, 55], saver: { quality: 82 }
end

If the maintainers feel this should become part of the gem's functionality, let me know if I should create a pull request.

@janko
Copy link
Owner

janko commented Jun 17, 2024

AFAIK, Active Storage doesn't currently support registering custom processing operations like this one, which is one if its limitations. It's not a limitation of ImageProcessing, because it allows you to do custom processing:

ImageProcessing::Vips.source(...)
  .custom do |image|
    coordinates = image.find_trim(threshold: 7, background: 0)
    image.crop(*coordinates)
  end
  # ...

@janko janko closed this as completed Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants