Content · Web Development · PHP

Reading EXIF Metadata from Photos

EXIF records camera settings and orientation, but optional fields vary widely by image and camera.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Admin

EXIF records camera settings and orientation, but optional fields vary widely by image and camera.

What It Does and When to Use It


Read optional EXIF fields without assuming their presence. GD operates on raster images in PHP; Imagick delegates to ImageMagick; EXIF reads metadata and is not an image decoder.

Requirements, Inputs and Expected Behavior


Image decoding and encoding need the relevant GD, EXIF, or Imagick support; features vary by PHP build and server. Treat EXIF strings as untrusted input and apply orientation correctly before generating previews.

Example 1: Basic Operation


Read optional EXIF fields without assuming their presence.

<?php
$meta = exif_read_data(__DIR__ . '/camera.jpg');
 if ($meta === false) throw new RuntimeException('No readable EXIF data');
 echo $meta['DateTimeOriginal'] ?? 'Unknown capture time';

Example 2: Behavior and Validation


Validate the EXIF orientation enum before applying rotations.

<?php
$details = exif_read_data(__DIR__ . '/portrait.jpg', 'IFD0', true);
 $rotation = $details['IFD0']['Orientation'] ?? 1;
 if (!in_array($rotation, range(1, 8), true)) $rotation = 1;
 echo 'Orientation: ', $rotation;

Practical Example: Application Use


Escape attacker-controlled metadata when displaying it in HTML.

<?php
$data = exif_read_data(__DIR__ . '/photo.jpg');
 $camera = (string)($data['Model'] ?? 'Not recorded');
 echo htmlspecialchars($camera, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');

Important Behavior and Edge Cases


The second example highlights: Validate the EXIF orientation enum before applying rotations. The practical example adds: Escape attacker-controlled metadata when displaying it in HTML.

Comparison with Related Approaches


ApproachWhat changes
GD / Imagick / EXIFGD operates on raster images in PHP; Imagick delegates to ImageMagick; EXIF reads metadata and is not an image decoder.
Choosing safelyCheck image dimensions, true decoder availability, and actual output format rather than trusting a filename suffix.

Common Beginner Mistakes


MistakeCorrect approach
Trusting the image extension or MIME header aloneDecode the source and verify the actual format, dimensions, and resource limits.
Forgetting transparency or aspect ratioPreserve alpha for transparent formats and calculate dimensions before resampling.
Skipping operational constraintsTreat EXIF strings as untrusted input and apply orientation correctly before generating previews.

Key Takeaways


  • EXIF records camera settings and orientation, but optional fields vary widely by image and camera.
  • Escape attacker-controlled metadata when displaying it in HTML.
  • Check image dimensions, true decoder availability, and actual output format rather than trusting a filename suffix.
Share this resourceSend this page to someone who may find it useful.