DEV Community

Sushil Subedi
Sushil Subedi

Posted on

How to Read EXIF Capture Time from HEIC/HEIF Photos in Ruby on Rails

If you’ve ever uploaded an iPhone photo and noticed your captured_at field missing, you’re not alone.

Modern iPhones save images as HEIC/HEIF instead of traditional JPEGs. These formats store metadata a little differently and that’s where many Ruby EXIF libraries fall short.

In this post, I’ll walk you through a reliable way to read photo capture dates (DateTimeOriginal, CreateDate, etc.) from HEIC/HEIF files inside a Rails app even if you’re using Active Storage.

Quick Summary

  • Use ExifTool (through the mini_exiftool or exiftool gem), it works with HEIC/HEIF files directly.
  • You don’t need to decode or convert images (libheif or libvips) just to read metadata.
  • Always convert timestamps to UTC and check multiple EXIF date fields for reliability.
  • When using Active Storage, call blob.open to access a temporary file path for ExifTool.

Why HEIC/HEIF can be tricky

Here’s the issue in plain terms:

  • HEIC stores metadata inside QuickTime-style boxes, not the standard EXIF segments used by JPEG.
  • Most Ruby EXIF libraries (like exifr) only understand JPEG/TIFF files.
  • ExifTool is a Perl-based tool that can read just about anything, including HEIC/HEIF, RAW, and Live Photo sidecars.

Setup

Install the ExifTool CLI on your machine/container:

  • macOS (Homebrew): brew install exiftool
  • Debian/Ubuntu: sudo apt-get install -y libimage-exiftool-perl
  • Alpine: apk add exiftool

Then add one of these gems to your Gemfile:

gem 'exiftool'       # lightweight wrapper; also shells out to exiftool
Enter fullscreen mode Exit fullscreen mode

And install them:

bundle install
Enter fullscreen mode Exit fullscreen mode

Reading EXIF data from a file

Here’s a simple Ruby method using the exiftool gem:

require 'exiftool'

def extract_capture_time(path)
  data = Exiftool.new(path).to_hash # symbolized keys
  raw  = data[:date_time_original] ||
         data[:create_date] ||
         data[:media_create_date] ||
         data[:modify_date]
  return nil unless raw

  case raw
  when Time then raw.utc
  when DateTime then raw.to_time.utc
  else
    (Time.parse(raw.to_s) rescue nil)&.utc
  end
end
Enter fullscreen mode Exit fullscreen mode

That’s it!

ExifTool takes care of all the format quirks, so this works for HEIC, JPEG, and even Live Photos.

Reading EXIF with Active Storage

When your image lives in Active Storage (e.g., S3 or Disk), just open it temporarily before reading EXIF:

def extract_capture_time_from_blob(blob)
  blob.open do |file|
    extract_capture_time(file.path) # call one of the helpers above
  end
end
Enter fullscreen mode Exit fullscreen mode

This downloads the file into a Tempfile, passes its path to ExifTool, and cleans up afterward, no extra libraries required.

Normalizing captured_at in your models

When you save your capture time, normalize it to UTC and support multiple sources:

def normalized_captured_at(file_path: nil, client_value: nil)
  exif_time = file_path && extract_capture_time(file_path)
  client_time = begin
    case client_value
    when Time then client_value
    when DateTime then client_value.to_time
    when String then Time.iso8601(client_value) rescue Time.parse(client_value)
    end
  rescue ArgumentError
    nil
  end
  (exif_time || client_time)&.utc
end
Enter fullscreen mode Exit fullscreen mode

This way, even if the photo doesn’t have EXIF data, you can still fall back to a timestamp sent from the frontend.

Troubleshooting

  • No DateTimeOriginal Some devices only set CreateDate or MediaCreateDate. Always try fallbacks.
  • Nil/empty type from browser, client uploads may send application/octet-stream. Fix on the frontend by inferring a proper MIME type from the filename before direct upload; EXIF reading itself is unaffected but downstream analyzers may behave better with the right type.
  • It worked for JPEG but not HEIC, ensure you’re using ExifTool, not EXIFR, for HEIC files.

References

If you’d like to make this reusable, you can wrap the logic into a small service class, ImageMetadataExtractorthat returns either a UTC Time or nil.
Once you have this in place, your app can reliably extract captured_at from any image, regardless of format. 📸

Top comments (0)