Extract and save image metadata. Props tellyworth. fixes #5162

git-svn-id: http://svn.automattic.com/wordpress/trunk@6313 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-11-05 00:01:26 +00:00
parent 6f761b1ef3
commit dd29614f60
2 changed files with 131 additions and 0 deletions

View File

@ -152,6 +152,12 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
if ( @file_exists($thumb) ) if ( @file_exists($thumb) )
$metadata['thumb'] = basename($thumb); $metadata['thumb'] = basename($thumb);
} }
// fetch additional metadata from exif/iptc
$image_meta = wp_read_image_metadata( $file );
if ($image_meta)
$metadata['image_meta'] = $image_meta;
} }
return apply_filters( 'wp_generate_attachment_metadata', $metadata ); return apply_filters( 'wp_generate_attachment_metadata', $metadata );
} }
@ -222,4 +228,84 @@ function wp_shrink_dimensions( $width, $height, $wmax = 128, $hmax = 96 ) {
} }
} }
// convert a fraction string to a decimal
function wp_exif_frac2dec($str) {
@list( $n, $d ) = explode( '/', $str );
if ( !empty($d) )
return $n / $d;
return $str;
}
// convert the exif date format to a unix timestamp
function wp_exif_date2ts($str) {
// seriously, who formats a date like 'YYYY:MM:DD hh:mm:ss'?
@list( $date, $time ) = explode( ' ', trim($str) );
@list( $y, $m, $d ) = explode( ':', $date );
return strtotime( "{$y}-{$m}-{$d} {$time}" );
}
// get extended image metadata, exif or iptc as available
function wp_read_image_metadata( $file ) {
if ( !file_exists( $file ) )
return false;
// exif contains a bunch of data we'll probably never need formatted in ways that are difficult to use.
// We'll normalize it and just extract the fields that are likely to be useful. Fractions and numbers
// are converted to floats, dates to unix timestamps, and everything else to strings.
$meta = array(
'aperture' => 0,
'credit' => '',
'camera' => '',
'caption' => '',
'created_timestamp' => 0,
'copyright' => '',
'focal_length' => 0,
'iso' => 0,
'shutter_speed' => 0,
'title' => '',
);
// read iptc first, since it might contain data not available in exif such as caption, description etc
if ( is_callable('iptcparse') ) {
getimagesize($file, $info);
if ( !empty($info['APP13']) ) {
$iptc = iptcparse($info['APP13']);
if ( !empty($iptc['2#110'][0]) ) // credit
$meta['credit'] = trim( $iptc['2#110'][0] );
elseif ( !empty($iptc['2#080'][0]) ) // byline
$meta['credit'] = trim( $iptc['2#080'][0] );
if ( !empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0]) ) // created datee and time
$meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]);
if ( !empty($iptc['2#120'][0]) ) // caption
$meta['caption'] = trim( $iptc['2#120'][0] );
if ( !empty($iptc['2#116'][0]) ) // copyright
$meta['copyright'] = trim( $iptc['2#116'][0] );
if ( !empty($iptc['2#005'][0]) ) // title
$meta['title'] = trim( $iptc['2#005'][0] );
}
}
// fetch additional info from exif if available
if ( is_callable('exif_read_data') ) {
$exif = exif_read_data( $file );
if (!empty($exif['FNumber']))
$meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 );
if (!empty($exif['Model']))
$meta['camera'] = trim( $exif['Model'] );
if (!empty($exif['DateTimeDigitized']))
$meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized']);
if (!empty($exif['FocalLength']))
$meta['focal_length'] = wp_exif_frac2dec( $exif['FocalLength'] );
if (!empty($exif['ISOSpeedRatings']))
$meta['iso'] = $exif['ISOSpeedRatings'];
if (!empty($exif['ExposureTime']))
$meta['shutter_speed'] = wp_exif_frac2dec( $exif['ExposureTime'] );
}
// FIXME: try other exif libraries if available
return apply_filters( 'wp_read_image_metadata', $meta, $file );
}
?> ?>

View File

@ -15,6 +15,12 @@ function wp_upload_display( $dims = false, $href = '' ) {
} }
if ( isset($attachment_data['width']) ) if ( isset($attachment_data['width']) )
list($width,$height) = wp_shrink_dimensions($attachment_data['width'], $attachment_data['height'], 171, 128); list($width,$height) = wp_shrink_dimensions($attachment_data['width'], $attachment_data['height'], 171, 128);
// check for extended metadata from exif/iptc
if ( !isset($attachment_data['image_meta']) && $is_image ) {
$image_meta = wp_read_image_meta( $filesystem_path );
$attachment_data['image_meta'] = $image_meta;
wp_update_attachment_metadata( $id, $attachment_data );
}
$post_title = attribute_escape( the_title( '', '', false ) ); $post_title = attribute_escape( the_title( '', '', false ) );
$post_content = attribute_escape(apply_filters( 'content_edit_pre', $post->post_content )); $post_content = attribute_escape(apply_filters( 'content_edit_pre', $post->post_content ));
@ -163,6 +169,45 @@ function wp_upload_form() {
<th scope="row"><label for="post_content"><?php _e('Description'); ?></label></th> <th scope="row"><label for="post_content"><?php _e('Description'); ?></label></th>
<td><textarea name="post_content" id="post_content"><?php echo $attachment->post_content; ?></textarea></td> <td><textarea name="post_content" id="post_content"><?php echo $attachment->post_content; ?></textarea></td>
</tr> </tr>
<?php if (isset($attachment_data['image_meta'])) { ?>
<tr>
<th scope="row"><label for="url"><?php _e('Aperture'); ?></label></th>
<td>f/<?php echo $attachment_data['image_meta']['aperture']; ?></td>
</tr>
<tr>
<th scope="row"><label for="url"><?php _e('Credit'); ?></label></th>
<td><?php echo $attachment_data['image_meta']['credit']; ?></td>
</tr>
<tr>
<th scope="row"><label for="url"><?php _e('Camera'); ?></label></th>
<td><?php echo $attachment_data['image_meta']['camera']; ?></td>
</tr>
<tr>
<th scope="row"><label for="url"><?php _e('Created'); ?></label></th>
<td><?php echo date_i18n(get_option('date_format').' '.get_option('time_format'), $attachment_data['image_meta']['created_timestamp']); ?></td>
</tr>
<tr>
<th scope="row"><label for="url"><?php _e('Copyright'); ?></label></th>
<td><?php echo $attachment_data['image_meta']['copyright']; ?></td>
</tr>
<tr>
<th scope="row"><label for="url"><?php _e('Focal Length'); ?></label></th>
<td><?php echo $attachment_data['image_meta']['focal_length']; ?>mm</td>
</tr>
<tr>
<th scope="row"><label for="url"><?php _e('ISO'); ?></label></th>
<td><?php echo $attachment_data['image_meta']['iso']; ?></td>
</tr>
<tr>
<th scope="row"><label for="url"><?php _e('Shutter Speed'); ?></label></th>
<td><?php $secs = $attachment_data['image_meta']['shutter_speed'];
echo ($secs > 0.0 and $secs < 1.0) ? ("1/" . round(1/$secs)) : ($secs); ?>s</td>
</tr>
<tr>
<th scope="row"><label for="url"><?php _e('Title'); ?></label></th>
<td><?php echo $attachment_data['image_meta']['title']; ?></td>
</tr>
<?php } ?>
<tr id="buttons" class="submit"> <tr id="buttons" class="submit">
<td colspan='2'> <td colspan='2'>
<?php if ( $id ) : ?> <?php if ( $id ) : ?>