Install Dependencies
# Install PHP Exiftool package via Composer
composer require alchemy-fr/exiftool
# Install the required Perl ExifTool binary (Linux/macOS)
sudo apt-get install libimage-exiftool-perl # Debian/Ubuntu
brew install exiftool # macOS (Homebrew)
Windows users: Download from ExifTool's official site and add to PATH.
Basic Usage
use Alchemy\ExifTool\ExifTool;
$exifTool = new ExifTool();
$exifTool->setWorkingDirectory(__DIR__); // Optional: Set working dir for temp files
// Read metadata from a single file
$metadata = $exifTool->parseFile('path/to/image.jpg');
dd($metadata); // Array of metadata (e.g., ['Image::Make' => 'Canon'])
// Read metadata from multiple files
$metadata = $exifTool->parseFiles(['file1.jpg', 'file2.png']);
First Use Case: Batch Metadata Extraction
$files = glob('uploads/*.{jpg,png}', GLOB_BRACE);
$results = $exifTool->parseFiles($files);
foreach ($results as $file => $data) {
// Process each file's metadata (e.g., save to DB)
DB::table('media_metadata')->updateOrCreate(
['path' => $file],
['make' => $data['Image::Make'] ?? null, 'model' => $data['Image::Model'] ?? null]
);
}
Metadata Validation
// Check if a file has GPS coordinates
$metadata = $exifTool->parseFile('travel.jpg');
if (isset($metadata['GPS::GPSLatitude'])) {
// Process geotagged file
}
Editing Metadata
// Update EXIF data (requires ExifTool binary with write permissions)
$exifTool->writeFile(
'output.jpg',
[
'Image::Make' => 'Modified',
'XPComment' => 'Edited via PHP ExifTool'
],
'input.jpg'
);
Recursive Directory Processing
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator('media/')
);
foreach ($iterator as $file) {
if ($file->isFile()) {
$metadata = $exifTool->parseFile($file->getPathname());
// Log or index metadata
}
}
Laravel Storage Integration
use Illuminate\Support\Facades\Storage;
$path = Storage::path('images/camera.jpg');
$metadata = $exifTool->parseFile($path);
Queueing Heavy Operations
// In a job class
public function handle() {
$files = File::where('processed', false)->get();
foreach ($files as $file) {
$metadata = $exifTool->parseFile(storage_path("app/{$file->path}"));
// Save metadata to DB
}
File::whereIn('id', $files->pluck('id'))->update(['processed' => true]);
}
Caching Results
$cacheKey = 'metadata_' . md5($filePath);
$metadata = Cache::remember($cacheKey, now()->addHours(1), function () use ($exifTool, $filePath) {
return $exifTool->parseFile($filePath);
});
Binary Path Issues
ExifToolException: Could not execute ExifTool binary.$exifTool = new ExifTool('/usr/bin/exiftool');
which exiftool (Linux/macOS) or check PATH in Windows.Permission Denied
ExifToolException: Permission denied.www-data, nginx) has read/write access to files and the ExifTool binary.Memory Limits
Allowed memory size exhausted when parsing large batches.memory_limit in php.ini.Corrupted Metadata
try-catch to handle exceptions:
try {
$metadata = $exifTool->parseFile($file);
} catch (\Exception $e) {
Log::warning("Failed to parse {$file}: " . $e->getMessage());
$metadata = []; // Fallback
}
$exifTool->setVerbose(true); // Logs raw ExifTool output to STDERR
$version = $exifTool->getVersion();
// Ensure it matches your PHP ExifTool package requirements (e.g., >= 10.88).
Custom Metadata Mappings
// Normalize metadata keys (e.g., 'Image::Make' => 'camera_make')
$normalized = collect($metadata)->mapWithKeys(function ($value, $key) {
return ["{$key->replace('::', '_')}" => $value];
})->toArray();
Plugin Integration
ExifTool::addArgument() to pass custom ExifTool arguments:
$exifTool->addArgument('-config', 'custom_config.txt');
Parallel Processing
$files = ['file1.jpg', 'file2.jpg', 'file3.jpg'];
$results = collect($files)->map(function ($file) use ($exifTool) {
return $exifTool->parseFile($file);
})->toArray();
exiftool(-k).exe is in PATH and files use Unix-style line endings if processing logs/configs.$timestamp = Carbon::parse($metadata['EXIF::DateTimeOriginal'])->timezone('America/New_York');
How can I help you explore Laravel packages today?