WordPress.org

Make WordPress Core

Changeset 6313


Ignore:
Timestamp:
11/05/2007 12:01:26 AM (11 years ago)
Author:
ryan
Message:

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

Location:
trunk/wp-admin/includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/image.php

    r6309 r6313  
    153153                $metadata['thumb'] = basename($thumb);
    154154        }
     155       
     156        // fetch additional metadata from exif/iptc
     157        $image_meta = wp_read_image_metadata( $file );
     158        if ($image_meta)
     159            $metadata['image_meta'] = $image_meta;
     160
    155161    }
    156162    return apply_filters( 'wp_generate_attachment_metadata', $metadata );
     
    223229}
    224230
     231// convert a fraction string to a decimal
     232function wp_exif_frac2dec($str) {
     233    @list( $n, $d ) = explode( '/', $str );
     234    if ( !empty($d) )
     235        return $n / $d;
     236    return $str;
     237}
     238
     239// convert the exif date format to a unix timestamp
     240function wp_exif_date2ts($str) {
     241    // seriously, who formats a date like 'YYYY:MM:DD hh:mm:ss'?
     242    @list( $date, $time ) = explode( ' ', trim($str) );
     243    @list( $y, $m, $d ) = explode( ':', $date );
     244
     245    return strtotime( "{$y}-{$m}-{$d} {$time}" );
     246}
     247
     248// get extended image metadata, exif or iptc as available
     249function wp_read_image_metadata( $file ) {
     250    if ( !file_exists( $file ) )
     251        return false;
     252
     253    // exif contains a bunch of data we'll probably never need formatted in ways that are difficult to use.
     254    // We'll normalize it and just extract the fields that are likely to be useful.  Fractions and numbers
     255    // are converted to floats, dates to unix timestamps, and everything else to strings.
     256    $meta = array(
     257        'aperture' => 0,
     258        'credit' => '',
     259        'camera' => '',
     260        'caption' => '',
     261        'created_timestamp' => 0,
     262        'copyright' => '',
     263        'focal_length' => 0,
     264        'iso' => 0,
     265        'shutter_speed' => 0,
     266        'title' => '',
     267    );
     268
     269    // read iptc first, since it might contain data not available in exif such as caption, description etc
     270    if ( is_callable('iptcparse') ) {
     271        getimagesize($file, $info);
     272        if ( !empty($info['APP13']) ) {
     273            $iptc = iptcparse($info['APP13']);
     274            if ( !empty($iptc['2#110'][0]) ) // credit
     275                $meta['credit'] = trim( $iptc['2#110'][0] );
     276            elseif ( !empty($iptc['2#080'][0]) ) // byline
     277                $meta['credit'] = trim( $iptc['2#080'][0] );
     278            if ( !empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0]) ) // created datee and time
     279                $meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]);
     280            if ( !empty($iptc['2#120'][0]) ) // caption
     281                $meta['caption'] = trim( $iptc['2#120'][0] );
     282            if ( !empty($iptc['2#116'][0]) ) // copyright
     283                $meta['copyright'] = trim( $iptc['2#116'][0] );
     284            if ( !empty($iptc['2#005'][0]) ) // title
     285                $meta['title'] = trim( $iptc['2#005'][0] );
     286         }
     287    }
     288
     289    // fetch additional info from exif if available
     290    if ( is_callable('exif_read_data') ) {
     291        $exif = exif_read_data( $file );
     292        if (!empty($exif['FNumber']))
     293            $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 );
     294        if (!empty($exif['Model']))
     295            $meta['camera'] = trim( $exif['Model'] );
     296        if (!empty($exif['DateTimeDigitized']))
     297            $meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized']);
     298        if (!empty($exif['FocalLength']))
     299            $meta['focal_length'] = wp_exif_frac2dec( $exif['FocalLength'] );
     300        if (!empty($exif['ISOSpeedRatings']))
     301            $meta['iso'] = $exif['ISOSpeedRatings'];
     302        if (!empty($exif['ExposureTime']))
     303            $meta['shutter_speed'] = wp_exif_frac2dec( $exif['ExposureTime'] );
     304    }
     305    // FIXME: try other exif libraries if available
     306
     307    return apply_filters( 'wp_read_image_metadata', $meta, $file );
     308
     309}
     310
    225311?>
  • trunk/wp-admin/includes/upload.php

    r6235 r6313  
    1616    if ( isset($attachment_data['width']) )
    1717        list($width,$height) = wp_shrink_dimensions($attachment_data['width'], $attachment_data['height'], 171, 128);
     18    // check for extended metadata from exif/iptc
     19    if ( !isset($attachment_data['image_meta']) && $is_image ) {
     20        $image_meta = wp_read_image_meta( $filesystem_path );
     21        $attachment_data['image_meta'] = $image_meta;
     22        wp_update_attachment_metadata( $id, $attachment_data );
     23    }
    1824
    1925    $post_title = attribute_escape( the_title( '', '', false ) );
     
    164170                <td><textarea name="post_content" id="post_content"><?php echo $attachment->post_content; ?></textarea></td>
    165171            </tr>
     172            <?php if (isset($attachment_data['image_meta'])) { ?>
     173                <tr>
     174                    <th scope="row"><label for="url"><?php _e('Aperture'); ?></label></th>
     175                    <td>f/<?php echo $attachment_data['image_meta']['aperture']; ?></td>
     176                </tr>
     177                <tr>
     178                    <th scope="row"><label for="url"><?php _e('Credit'); ?></label></th>
     179                    <td><?php echo $attachment_data['image_meta']['credit']; ?></td>
     180                </tr>
     181                <tr>
     182                    <th scope="row"><label for="url"><?php _e('Camera'); ?></label></th>
     183                    <td><?php echo $attachment_data['image_meta']['camera']; ?></td>
     184                </tr>
     185                <tr>
     186                    <th scope="row"><label for="url"><?php _e('Created'); ?></label></th>
     187                    <td><?php echo date_i18n(get_option('date_format').' '.get_option('time_format'), $attachment_data['image_meta']['created_timestamp']); ?></td>
     188                </tr>
     189                <tr>
     190                    <th scope="row"><label for="url"><?php _e('Copyright'); ?></label></th>
     191                    <td><?php echo $attachment_data['image_meta']['copyright']; ?></td>
     192                </tr>
     193                <tr>
     194                    <th scope="row"><label for="url"><?php _e('Focal Length'); ?></label></th>
     195                    <td><?php echo $attachment_data['image_meta']['focal_length']; ?>mm</td>
     196                </tr>
     197                <tr>
     198                    <th scope="row"><label for="url"><?php _e('ISO'); ?></label></th>
     199                    <td><?php echo $attachment_data['image_meta']['iso']; ?></td>
     200                </tr>
     201                <tr>
     202                    <th scope="row"><label for="url"><?php _e('Shutter Speed'); ?></label></th>
     203                    <td><?php $secs = $attachment_data['image_meta']['shutter_speed'];
     204                        echo ($secs > 0.0 and $secs < 1.0) ? ("1/" . round(1/$secs)) : ($secs); ?>s</td>
     205                </tr>
     206                <tr>
     207                    <th scope="row"><label for="url"><?php _e('Title'); ?></label></th>
     208                    <td><?php echo $attachment_data['image_meta']['title']; ?></td>
     209                </tr>
     210            <?php } ?>
    166211            <tr id="buttons" class="submit">
    167212                <td colspan='2'>
Note: See TracChangeset for help on using the changeset viewer.