File metadata manager
A Drupal module providing a file metadata manager service and API to retrieve and manage metadata from files like EXIF photo information, font information, and image dimensions.
file_mdm
Install
composer require 'drupal/file_mdm:^3.2'
composer require 'drupal/file_mdm:^3.1'
Overview
File metadata manager provides a unified API to access and manage information stored within files such as EXIF photo metadata, TrueType font information, and image dimensions. The module implements a pluggable architecture where metadata protocols are implemented as plugins, allowing developers to extend the system with custom metadata handlers.
The module statically caches metadata during a request's lifetime to avoid redundant I/O operations, and optionally stores metadata in Drupal's cache system for persistence across requests. It also handles remote files by managing local temporary copies for PHP functions that cannot access remote stream wrappers directly.
Three plugins are provided: 'getimagesize' (included in base module) caches PHP's getimagesize() calls, 'exif' (submodule) uses PHP Exif Library for reading and writing EXIF data, and 'font' (submodule) uses PHP Font Lib to read TTF/OTF/WOFF font metadata.
Features
- Unified API to retrieve metadata from various file types through a single service interface
- Pluggable metadata protocol system allowing custom plugins for different file formats
- Static caching of metadata during request lifetime to minimize redundant file I/O operations
- Persistent caching of metadata in Drupal's cache bin with configurable expiration times
- Support for reading and writing EXIF metadata in JPEG and TIFF images (via submodule)
- Font metadata extraction from TTF, OTF, and WOFF files (via submodule)
- Cached PHP getimagesize() calls for image dimension and type detection
- Automatic handling of remote files by copying to local temporary storage for processing
- Configurable cache exclusion paths using wildcard patterns
- Per-plugin cache configuration with ability to override global settings
- Automatic cleanup of cached metadata when file entities are deleted
Use Cases
Automatic image orientation correction
Use the EXIF plugin to read the Orientation tag from uploaded photos, determine the correct rotation, apply image transformations, and optionally write the corrected orientation back to the file.
Photo gallery metadata display
Display EXIF information (camera model, exposure settings, GPS coordinates) alongside images in a gallery by using the exif plugin to extract metadata from uploaded photographs.
Image dimension caching for performance
Use the getimagesize plugin to cache image dimensions across requests, avoiding repeated filesystem access when displaying images in lists or grids.
Font management system
Build a font library with the font plugin to extract and display metadata (font family, weight, designer, license) from uploaded TTF/OTF/WOFF font files.
Remote file metadata extraction
Access metadata from files stored on remote file systems (S3, CDN) by using copyUriToTemp() to create local copies before processing, minimizing remote I/O.
Bulk image processing optimization
When processing multiple images, leverage the static caching to avoid repeated file reads when the same image is accessed by multiple modules or operations within a single request.
Tips
- Use the dedicated cache bin 'file_mdm' for cache invalidation: cache_tags for config changes are automatically handled
- Files in temporary:// scheme are never cached regardless of settings
- Metadata is statically cached during a request even if persistent caching is disabled
- For remote files, call copyUriToTemp() before getMetadata() to minimize network requests
- The EXIF plugin can write metadata - remember to call saveMetadataToFile() and optionally copyTempToUri() for remote files
- Use getSupportedKeys() to discover available metadata keys for each plugin
- Plugin configurations can be altered using hook_file_metadata_plugin_info_alter()
Technical Details
Admin Pages 1
/admin/config/system/file_mdm
Configure file metadata caching settings and per-plugin cache options. This page allows administrators to control how file metadata is cached to optimize performance.
Hooks 1
hook_file_delete
Cleans up cached metadata when a file entity is deleted. Automatically removes any cached file metadata and releases the file from the metadata manager.