Skip to content

Instantly share code, notes, and snippets.

@Firsh
Last active May 25, 2024 11:00
Show Gist options
  • Save Firsh/35531da46a63db5b94b33bf28798f524 to your computer and use it in GitHub Desktop.
Save Firsh/35531da46a63db5b94b33bf28798f524 to your computer and use it in GitHub Desktop.
Simple EXIF display for JIG
<?php
// I recommend using the Code Snippets plugin https://wordpress.org/plugins/code-snippets/
// In the case of which only the below code is needed
// This inserts basic EXIF data in the description of photos in JIG galleries, if available
add_action('init', function() {
add_filter('jig_images', 'jig_exif_captions', 10, 2);
});
function jig_exif_captions($images, $atts)
{
// Ensure the required file is included
if (!function_exists('wp_read_image_metadata')) {
require_once ABSPATH . 'wp-admin/includes/image.php';
}
foreach ($images as &$image) {
if (empty($image['extra_class']) || strpos($image['extra_class'], 'jig-contentID-ML-') === false) {
continue; // Changed from break to continue to ensure the loop continues to the next image
}
if (preg_match('/(\d)+/i', $image['extra_class'], $regs)) {
$id = $regs[0];
} else {
continue; // Changed from break to continue to ensure the loop continues to the next image
}
// Get the file path of the image using the ID
$file = get_attached_file($id);
// Read the EXIF data from the image
$metadata = wp_read_image_metadata($file);
if ($metadata) {
$exif_data = [];
if (!empty($metadata['focal_length'])) {
$exif_data[] = $metadata['focal_length'] . ' mm';
}
if (!empty($metadata['shutter_speed'])) {
$exif_data[] = format_shutter_speed($metadata['shutter_speed']);
}
if (!empty($metadata['aperture'])) {
$exif_data[] = 'f/' . $metadata['aperture'];
}
if (!empty($metadata['iso'])) {
$exif_data[] = 'ISO ' . $metadata['iso'];
}
if (!empty($metadata['created_timestamp'])) {
$exif_data[] = date('Y-m-d H:i:s', $metadata['created_timestamp']);
}
// Join the EXIF data with a middle dot
$exif_string = implode(' · ', $exif_data);
// Append the EXIF data to the image description
if (empty($image['description'])) {
$image['description'] = $exif_string;
} else {
$image['description'] .= '<br>' . $exif_string;
}
}
}
return $images;
}
function format_shutter_speed($shutter_speed)
{
if ($shutter_speed <= 0) {
return $shutter_speed . ' sec';
}
// Calculate the reciprocal of the shutter speed
$reciprocal = 1 / $shutter_speed;
if ($reciprocal > 1) {
// Format as a fraction if the reciprocal is greater than 1
return '1/' . round($reciprocal) . ' sec';
} else {
// Otherwise, display the original value in seconds
return round($shutter_speed, 2) . ' sec';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment