3

Is there an easy way to strip all the EXIF data in every Image upload? Maybe in an before_save hook?

kannix
  • 5,107
  • 6
  • 28
  • 47
  • 1
    Interesting... I did not try yet, but maybe mixing these two post can give an hint: https://stackoverflow.com/questions/11839664/how-to-retrieve-exif-information-of-an-image-in-rails and https://stackoverflow.com/questions/50844197/how-to-compress-images-before-uploading-to-the-cloud-using-activestorage – iGian Oct 31 '18 at 20:20

1 Answers1

0

based on iGian's comment I ended up with this code:

before_save :strip_exif_data

private

def strip_exif_data
  return unless image.attached?
  filename = image.filename.to_s
  attachment_path = "#{Dir.tmpdir}/#{image.filename}"
  File.open(attachment_path, 'wb') do |file|
    file.write(image.download)
    file.close
  end
  mm_image = MiniMagick::Image.open(attachment_path)
  mm_image.strip
  mm_image.write attachment_path
  image.attach(io: File.open(attachment_path), filename: filename)
end
kannix
  • 5,107
  • 6
  • 28
  • 47