【问题标题】:Rails ActiveStorage strip image EXIF dataRails ActiveStorage 条带图像 EXIF 数据
【发布时间】:2018-10-31 17:13:02
【问题描述】:

有没有一种简单的方法可以删除每次上传的图片中的所有 EXIF 数据?也许在before_save 钩子里?

【问题讨论】:

标签: ruby-on-rails rails-activestorage


【解决方案1】:

根据 iGian 的评论,我最终得到了以下代码:

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

【讨论】:

    猜你喜欢
    • 2020-05-24
    • 2019-05-19
    • 1970-01-01
    • 2018-08-27
    • 2013-07-11
    • 2017-09-11
    • 1970-01-01
    • 2020-08-12
    相关资源
    最近更新 更多