目标是拍摄图像并生成图像的多个变体,具有预定义的高度,但宽度不同,从而根据顶部、左侧原点进行裁剪。
为 Rails 的 ActiveStorage 选择
libvips
库,该过程应该可以通过 image_processing
gem 帮助程序执行。 gem 的 wiki 提供了指向此执行的指针:
ImageProcessing::Vips.crop(0, 0, 300, 300)
但是语法没有正确执行
class Pretti < ApplicationRecord
extend Mobility
require "image_processing/vips"
belongs_to :shop
validates :name, presence: true
translates :name_locale, type: :string
# pipeline = ImageProcessing::Vips
# .source(file)
# .convert("png")
# mobile = pipeline.crop: [0, 0, 90, 480]
# medium = pipeline.crop: [0, 0, 90, 780]
# small = pipeline.crop: [0, 0, 90, 1280]
has_one_attached :background_image do |attachable|
attachable.variant :mobile, ImageProcessing::Vips.crop: [0, 0, 90, 480]
attachable.variant :tablet, ImageProcessing::Vips.crop: [0, 0, 90, 780]
attachable.variant :desktop, ImageProcessing::Vips.crop: [0, 0, 90, 1280]
end
end
记录的错误是
NameError (uninitialized constant ImageProcessing::Vips):
如果
require
行被注释掉,那么错误是:
uninitialized constant ImageProcessing::Vips
[..]
attachable.variant :mobile, ImageProcessing::Vips.crop(0, 0, 90, 480)
注释行来自单独的尝试,其中语法是从文档中提取的
这个应该如何投射?
如果有人遇到这个问题...
在撰写本文时,各种文档来源(导轨指南、边缘指南、图像处理宝石)可能会产生误导。
目前的情况还算整洁。不需要任何要求,并且依赖性处理器之间的语法看起来是同质的。因此:
# require "image_processing/vips"
has_one_attached :background_image do |attachable|
attachable.variant :mobile, crop: [0, 0, 480, 90], preprocessed: true
attachable.variant :tablet, crop: [0, 0, 780, 90], preprocessed: true
attachable.variant :desktop, crop: [0, 0, 1280, 90], preprocessed: true
end