Last active
August 29, 2015 14:01
-
-
Save scofennell/4a3065f648ba3ee342d2 to your computer and use it in GitHub Desktop.
WordPress Function to get EXIF date of attachment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Return the EXIF date for a jpeg attachment post. | |
* | |
* @return string The Exif time on which the image was taken. | |
*/ | |
function sjf_deh_get_exif_date() { | |
// Grab the current post. | |
$post = get_post(); | |
// If this isn't an attachment, bail. | |
if( $post->post_type != 'attachment' ) { return false; } | |
// If this isn't an jpeg, bail. | |
$mime = get_post_mime_type(); | |
if( $mime != 'image/jpeg' ) { return false; } | |
// Get the path to the attachment file. | |
$path = get_attached_file( $post->ID ); | |
if( empty( $path ) ) { return false; } | |
// Get the exif data. | |
$exif = exif_read_data( $path ); | |
// Get the exif time. | |
if( isset( $exif[ 'DateTimeOriginal' ] ) ) { | |
$exif_time = $exif[ 'DateTimeOriginal' ]; | |
// If there is no exif time, bail. | |
} else { | |
return false; | |
} | |
// Convert the time to Unix time. | |
$unix_time = strtotime( $exif_time ); | |
// If the time seems like the Unix epoch, which it might be if strtotime gets an oddball value, bail. | |
if( stristr( '1970-01-01', $exif_time ) ) { return false; } | |
// Convert the time back to the format used by WordPress $post->post_date. | |
$time = date( 'Y-m-d H:i:s', $unix_time ); | |
return $time; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment