demarches-normaliennes/app/controllers/concerns/uninterlace_png_concern.rb

20 lines
568 B
Ruby
Raw Normal View History

2023-09-19 15:37:28 +02:00
module UninterlacePngConcern
extend ActiveSupport::Concern
private
def uninterlace_png(uploaded_file)
2023-09-21 15:19:21 +02:00
if uploaded_file&.content_type == 'image/png' && interlaced?(uploaded_file.tempfile.to_path)
2023-09-19 15:37:28 +02:00
chunky_img = ChunkyPNG::Image.from_io(uploaded_file.to_io)
chunky_img.save(uploaded_file.tempfile.to_path, interlace: false)
uploaded_file.tempfile.reopen(uploaded_file.tempfile.to_path, 'rb')
end
uploaded_file
end
2023-09-21 15:19:21 +02:00
def interlaced?(png_path)
png = MiniMagick::Image.open(png_path)
png.data["interlace"] != "None"
end
2023-09-19 15:37:28 +02:00
end