Ruby on Rails载波:自动定向不工作,如果退出数据被剥离的同时



我使用的是Ruby 2.4.0和Ruby on Rails 4.2.8。

我已经从mini_magic转移到vips处理我的Ruby on Rails上传器上的图像。由于wkhtmltopdf不支持EXIF数据的图像,我必须正确旋转他们时,他们被保存到存储。我在载波上传器上运行两个进程,一个用于旋转和剥离退出数据,另一个用于调整大小。

class ImageUploader < CarrierWave::Uploader::Base

include CarrierWave::Vips

process :fix_orientation
process resize_to_fit: [800, 800]
# Choose what kind of storage to use for this uploader:
storage :gcloud
# storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

调用process:auto_orient(这是Carrierwave-vips提供的方法)在我的Ruby on Rails图像上传器上不起作用。图像没有被旋转,就像什么都没发生一样。我进入了carrier - wave-vip gem,并将auto_orient相关的代码作为fix_orientation放入我的上传器中。这是取自该库的代码- https://github.com/eltiare/carrierwave-vips/blob/master/lib/carrierwave/vips.rb

def auto_orient
manipulate! do |image|
o = image.get('exif-Orientation').to_i rescue nil
o ||= image.get('exif-ifd0-Orientation').to_i rescue 1
case o
when 1
# Do nothing, everything is peachy
when 6
image.rot270
when 8
image.rot180
when 3
image.rot90
else
raise('Invalid value for Orientation: ' + o.to_s)
end
image.set_type GObject::GSTR_TYPE, 'exif-Orientation', ''
image.set_type GObject::GSTR_TYPE, 'exif-ifd0-Orientation', ''
end
end

在摆弄它之后,我发现代码只有在去掉exif数据的最后一节时才会旋转图像:

image.set_type GObject::GSTR_TYPE, 'exif-Orientation',
image.set_type GObject::GSTR_TYPE, 'exif-ifd0-Orientation',

我试过将函数分解为旋转图像的部分,以及剥离数据的部分,看看是否有效,所以像

这样的东西
process :fix_orientation #code checking exif data number and rotating image accordingly
process :fix_orientation_2 #the last two lines stripping the exif data from the image

这仍然使它不能再次工作。如果没有调用fix_orientation_2中的行,则图像实际上会正确旋转。

这是相当令人费解的行为,因为它直接来自载波库,我希望剥离exif数据不会否定以前旋转所做的事情。调整大小的处理似乎仍然很顺利所以也不是整个进程堆栈都被反转了

你知道是什么导致了这种行为吗?可能是Ruby版本?

虽然我想了解如何解决潜在的问题,但我确实通过不调用fix_orientation_2而不是调用:strip函数来解决这个问题,该函数是该库的一部分,这工作得很好:

process :fix_orientation
process :strip

另外,至少对我来说,使用这些示例图像来测试exif: https://github.com/recurser/exif-orientation-examples

发现Carrierwave-vips auto_orient中的逻辑不正确:

case o
when 1
# Do nothing, everything is peachy
when 6
image.rot270
when 8
image.rot180
when 3
image.rot90

要在git上正确上传1、6、8和3张图片,正确的逻辑(至少对我来说)是这样的:

case o
when 1
# Do nothing, everything is peachy
when 6
image.rot90
when 8
image.rot270
when 3
image.rot180

最新更新