The WordPress Exif-Meta-Datas


WordPress stores when uploading files, some data in the meta data – the metadata. If there are pictures involved and these files contain EXIF data, then it outputs some EXIF data which can be used. For example, as additional information about the picture in a photo blog.

No matter what, and how you want to do that, here is a small function that reads the Exif data.

The second function fb_simple_exif() is for the direct output and uses the first function, which gives the values back to use.

// grab exif data from wp attachment
function grab_exif_data_from_wp($post_ID, $debug = FALSE) {
	global $id, $post;
	
	if ( !isset($post_ID) || '' == $post_ID )
		return FALSE;
	
	$meta = wp_get_attachment_metadata($post_ID, FALSE);
	
	$return = '';
	
	if ( $meta['image_meta']['created_timestamp'] )
		$return .= date( "d-M-Y H:i:s", $meta['image_meta']['created_timestamp'] );
	if ( $meta['image_meta']['copyright'] )
		$return .= $meta['image_meta']['copyright'];
	if ( $meta['image_meta']['credit'] )
		$return .= $meta['image_meta']['credit'];
	if ( $meta['image_meta']['title'] )
		$return .= $meta['image_meta']['title'];
	if ( $meta['image_meta']['caption'] )
		$return .= $meta['image_meta']['caption'];
	if ( $meta['image_meta']['camera'] )
		$return .= $meta['image_meta']['camera'];
	if ( $meta['image_meta']['focal_length'] )
		$return .= __( '· Brennweite:', FB_GREYFOTO_TEXTDOMAIN )
		           . ' ' . $meta['image_meta']['focal_length'] 
		           . __( 'mm', FB_GREYFOTO_TEXTDOMAIN );
	if ( $meta['image_meta']['aperture'] )
		$return .= $meta['image_meta']['aperture'];
	if ( $meta['image_meta']['iso'] )
		$return .= $meta['image_meta']['iso'];
	if ( $meta['image_meta']['shutter_speed'] )
		$return .= number_format($meta['image_meta']['shutter_speed'], 2) . ' ' . __( 'seconds', FB_GREYFOTO_TEXTDOMAIN );
	
	if ($debug) {
		ob_start();
		var_dump($meta);
		$return = ob_get_clean();
	}
	
	return $return;
}

function fb_simple_exif($post_ID, $debug) {
	
	$echo = grab_exif_data_from_wp($post_ID, $debug);
	
	if ( $echo && '' != $echo )
		echo $echo;
}

Posted

in

by

Comments

6 responses to “The WordPress Exif-Meta-Datas”

  1. Matt Avatar

    Great tutorial, I have used something like this in the past. http://pastie.org/1374398

  2. […] Si vos photos contiennent des informations EXIF, sachez que WordPress sait les retranscrire. Cela peut être sympa pour les amateurs de photos. Voici la marche à suivre. […]

  3. Piergi Avatar

    I’m sure the solution is trivial, but I think I am missing something.

    I would like to use the function into the image.php template, but I am not getting any metadata.

    I am using: ID, 1) ?>

    I get the dump of the variable, and it is related to the actual image.

    Any hint?

  4. Piergi Avatar

    The code in the preceding comment went away.

    I am using fb_simple_exif($post->ID, 1)

    Thanks in advance.

  5. Piergi Avatar

    I’m sorry. I got it now.

  6. Joachim Kudish Avatar

    I get this error when I try to use the function:

    Fatal error: Cannot use string offset as an array

    Any ideas?