2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-04-22 15:12:49 +02:00
|
|
|
class AutoRotateService
|
|
|
|
def process(file, output)
|
|
|
|
auto_rotate_image(file, output)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def auto_rotate_image(file, output)
|
|
|
|
image = MiniMagick::Image.new(file.to_path)
|
|
|
|
|
2024-04-29 16:53:21 +02:00
|
|
|
return nil if !image.valid?
|
|
|
|
|
2024-04-22 15:12:49 +02:00
|
|
|
case image["%[orientation]"]
|
|
|
|
when 'LeftBottom'
|
|
|
|
rotate_image(file, output, 90)
|
|
|
|
when 'BottomRight'
|
|
|
|
rotate_image(file, output, 180)
|
|
|
|
when 'RightTop'
|
|
|
|
rotate_image(file, output, 270)
|
2024-04-29 16:53:21 +02:00
|
|
|
else
|
|
|
|
nil
|
2024-04-22 15:12:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def rotate_image(file, output, degree)
|
|
|
|
MiniMagick::Tool::Convert.new do |convert|
|
|
|
|
convert << file.to_path
|
|
|
|
convert.rotate(degree)
|
|
|
|
convert.auto_orient
|
|
|
|
convert << output.to_path
|
|
|
|
end
|
2024-04-29 16:53:21 +02:00
|
|
|
output
|
2024-04-22 15:12:49 +02:00
|
|
|
end
|
|
|
|
end
|