demarches-normaliennes/app/services/auto_rotate_service.rb

37 lines
736 B
Ruby
Raw Normal View History

# frozen_string_literal: true
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?
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
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
end
end