Drupal is a registered trademark of Dries Buytaert

media_from_images

5 sites No security coverage
View on drupal.org

The Media From Images module automatically creates media entities for image files that do not already have corresponding media entities. The use case is if you have a site that was built in Drupal 8 without the media module at first and uses the file insert function in CKEditor. It has accumulated many many file entries, many that are duplicates.

A future version of the module may look at the files outside of styles to see if they have a file entry and if not, add one and also add a related media entry. This would be helpful if the site had the IMCE uploader in the CKEditor that does not track files.

The module provides a comprehensive tracking system using SHA256 file hashes to prevent duplicate media creation and maintain data integrity. It includes both web-based administration tools and Drush commands for batch processing of large numbers of files.

Features

  • Automatic Media Creation: Creates media entities for image files without existing media representations
  • Hash-Based Tracking: Uses SHA256 file hashes to track media-file relationships and prevent duplicates
  • Batch Processing: Processes large numbers of files in manageable batches with progress tracking
  • Operation Locking: Prevents concurrent operations that could cause data conflicts
  • Existing Media Attachment: Identifies and tracks existing media entities not yet in the tracking system
  • Administrative Interface: Web-based interface showing statistics and allowing batch operations
  • Drush Integration: Command-line tools for server-side processing
  • Automatic Cleanup: Removes tracking entries when media or file entities are deleted
  • Comprehensive Logging: Detailed logging of all operations for monitoring and debugging

Requirements

  • Drupal 10.0 or later
  • Media module (core)
  • File module (core)
  • Image media bundle configured with field_media_image field

Installation

  1. Copy the module to your modules/custom directory
  2. Enable the module through the Drupal administrative interface or using Drush: drush en media_from_images
  3. The module will automatically create the required database table upon installation
  4. Navigate to Configuration > Media > Media From Images to access the administrative interface

Configuration

No additional configuration is required. The module works with the standard Drupal media system and assumes:

  • An 'image' media bundle exists
  • The image media bundle has a 'field_media_image' field for file references
  • Image files are managed through Drupal's file system

Usage

Web Interface

Access the administrative interface at Configuration > Media > Media From Images to:

  • View statistics about image files and media entities
  • See current tracking table entries with media and file information
  • Start batch processes to create media entities for untracked files
  • Attach existing media entities to the tracking system
  • Monitor operation status and progress

Drush Commands

  • drush media-from-images:process or drush mfi:process - Process all image files and create media entities for those without them

Database Schema

The module creates a custom table media_from_images with the following structure:

  • media_image_hash (varchar 255, primary key) - SHA256 hash of the image file
  • media_image_id (int 10) - Media entity ID
  • image_entity_id (int 10) - File entity ID

API

The module provides a service media_from_images.service implementing MediaFromImagesServiceInterface with the following key methods:

  • getImageFileIds() - Get all image file entity IDs
  • getFileHash(FileInterface $file) - Generate SHA256 hash for a file
  • mediaEntityExistsForFile(FileInterface $file) - Check if media entity exists for a file
  • createMediaFromFile(FileInterface $file) - Create media entity from file
  • startBatchProcess() - Start batch process for creating media entities
  • startAttachUnattachedProcess() - Start batch process for attaching existing media entities

Similar projects

  • Image to Media Handles a similar conversion on image fields on entities. Image to Media module can be a complement to this module.
  • Migrate File to Media. This module does something similar but with image fields only. It doesn't handle file entities that may have been generated in CKEditor.

Troubleshooting

  • Button doesn't work on first click: This has been resolved in the current version by fixing the entity save order
  • Batch operation stuck: Operation locks expire after 30 minutes automatically
  • Missing field_media_image: Ensure your image media bundle has the correct field name
  • File not found errors: Check that image files physically exist on the filesystem
  • Permission errors: Ensure users have 'administer site configuration' permission

Uninstallation

When uninstalling the module:

  1. Disable the module: drush pmu media_from_images
  2. The custom database table will be automatically removed during uninstallation
  3. Media entities created by this module will remain in your system

Technical Notes

  • Uses Drupal's batch API for processing large numbers of files
  • Implements operation locking to prevent concurrent batch processes
  • Automatically cleans up tracking entries when entities are deleted
  • Processes files in configurable batch sizes (default: 50 files per batch)
  • Includes comprehensive error handling and logging

Activity

Total releases
4
First release
Jun 2025
Latest release
8 months ago
Release cadence
2 days
Stability
75% stable

Release Timeline

Releases

Version Type Release date
1.0.2 Stable Jun 25, 2025
1.0.1 Stable Jun 23, 2025
1.0.0 Stable Jun 21, 2025
1.0.x-dev Dev Jun 20, 2025