-
Notifications
You must be signed in to change notification settings - Fork 82
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
Comments
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. |
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
# ... |
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:
With
ImageProcessing::MiniMagick
these options passed to the image variant work to trim it:The equivalent in VIPS looks to be
find_trim
but this doesn't work when I pass it toImageProcessing::Vips
: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?The text was updated successfully, but these errors were encountered: