Make WordPress Core

Ticket #5162: wp-exif-r6201.patch

File wp-exif-r6201.patch, 6.4 KB (added by tellyworth, 17 years ago)
  • wordpress/wp-admin/includes/image.php

     
    156156                        if ( @file_exists($thumb) )
    157157                                $metadata['thumb'] = basename($thumb);
    158158                }
     159               
     160                // fetch additional metadata from exif/iptc
     161                $image_meta = wp_read_image_metadata( $file );
     162                if ($image_meta)
     163                        $metadata['image_meta'] = $image_meta;
     164
    159165        }
    160166        return apply_filters( 'wp_generate_attachment_metadata', $metadata );
    161167}
     
    189195                return array( (int) ($width / $height * $hmax ), $hmax );
    190196}
    191197
     198// convert a fraction string to a decimal
     199function wp_exif_frac2dec($str) {
     200        @list( $n, $d ) = explode( '/', $str );
     201        if ( !empty($d) )
     202                return $n / $d;
     203        return $str;
     204}
     205
     206// convert the exif date format to a unix timestamp
     207function wp_exif_date2ts($str) {
     208        // seriously, who formats a date like 'YYYY:MM:DD hh:mm:ss'?
     209        @list( $date, $time ) = explode( ' ', trim($str) );
     210        @list( $y, $m, $d ) = explode( ':', $date );
     211       
     212        return strtotime( "{$y}-{$m}-{$d} {$time}" );
     213}
     214
     215// get extended image metadata, exif or iptc as available
     216function wp_read_image_metadata( $file ) {
     217        if ( !file_exists( $file ) )
     218                return false;
     219
     220        // exif contains a bunch of data we'll probably never need formatted in ways that are difficult to use.
     221        // We'll normalize it and just extract the fields that are likely to be useful.  Fractions and numbers
     222        // are converted to floats, dates to unix timestamps, and everything else to strings.
     223        $meta = array(
     224                'aperture' => 0,
     225                'credit' => '',
     226                'camera' => '',
     227                'caption' => '',
     228                'created_timestamp' => 0,
     229                'copyright' => '',
     230                'focal_length' => 0,
     231                'iso' => 0,
     232                'shutter_speed' => 0,
     233                'title' => '',
     234        );
     235
     236        // read iptc first, since it might contain data not available in exif such as caption, description etc
     237        if ( is_callable('iptcparse') ) {
     238                getimagesize($file, $info);
     239                if ( !empty($info['APP13']) ) {
     240                        $iptc = iptcparse($info['APP13']);
     241                        if ( !empty($iptc['2#110'][0]) ) // credit
     242                                $meta['credit'] = trim( $iptc['2#110'][0] );
     243                        elseif ( !empty($iptc['2#080'][0]) ) // byline
     244                                $meta['credit'] = trim( $iptc['2#080'][0] );
     245                        if ( !empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0]) ) // created datee and time
     246                                $meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]);
     247                        if ( !empty($iptc['2#120'][0]) ) // caption
     248                                $meta['caption'] = trim( $iptc['2#120'][0] );
     249                        if ( !empty($iptc['2#116'][0]) ) // copyright
     250                                $meta['copyright'] = trim( $iptc['2#116'][0] );
     251                        if ( !empty($iptc['2#005'][0]) ) // title
     252                                $meta['title'] = trim( $iptc['2#005'][0] );
     253                 }
     254        }
     255
     256        // fetch additional info from exif if available
     257        if ( is_callable('exif_read_data') ) {
     258                $exif = exif_read_data( $file );
     259                if (!empty($exif['FNumber']))
     260                        $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 );
     261                if (!empty($exif['Model']))
     262                        $meta['camera'] = trim( $exif['Model'] );
     263                if (!empty($exif['DateTimeDigitized']))
     264                        $meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized']);
     265                if (!empty($exif['FocalLength']))
     266                        $meta['focal_length'] = wp_exif_frac2dec( $exif['FocalLength'] );
     267                if (!empty($exif['ISOSpeedRatings']))
     268                        $meta['iso'] = $exif['ISOSpeedRatings'];
     269                if (!empty($exif['ExposureTime']))
     270                        $meta['shutter_speed'] = wp_exif_frac2dec( $exif['ExposureTime'] );
     271                return $meta;
     272        }
     273        // FIXME: try other exif libraries if available
     274
     275}
     276
    192277?>
  • wordpress/wp-admin/includes/upload.php

     
    1515        }
    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 ) );
    2026        $post_content = attribute_escape(apply_filters( 'content_edit_pre', $post->post_content ));
     
    160166                                <th scope="row"><label for="post_content"><?php _e('Description'); ?></label></th>
    161167                                <td><textarea name="post_content" id="post_content"><?php echo $attachment->post_content; ?></textarea></td>
    162168                        </tr>
     169                        <?php if (isset($attachment_data['image_meta'])) { ?>
     170                                <tr>
     171                                        <th scope="row"><label for="url"><?php _e('Aperture'); ?></label></th>
     172                                        <td>f/<?php echo $attachment_data['image_meta']['aperture']; ?></td>
     173                                </tr>
     174                                <tr>
     175                                        <th scope="row"><label for="url"><?php _e('Credit'); ?></label></th>
     176                                        <td><?php echo $attachment_data['image_meta']['credit']; ?></td>
     177                                </tr>
     178                                <tr>
     179                                        <th scope="row"><label for="url"><?php _e('Camera'); ?></label></th>
     180                                        <td><?php echo $attachment_data['image_meta']['camera']; ?></td>
     181                                </tr>
     182                                <tr>
     183                                        <th scope="row"><label for="url"><?php _e('Created'); ?></label></th>
     184                                        <td><?php echo date_i18n(get_option('date_format').' '.get_option('time_format'), $attachment_data['image_meta']['created_timestamp']); ?></td>
     185                                </tr>
     186                                <tr>
     187                                        <th scope="row"><label for="url"><?php _e('Copyright'); ?></label></th>
     188                                        <td><?php echo $attachment_data['image_meta']['copyright']; ?></td>
     189                                </tr>
     190                                <tr>
     191                                        <th scope="row"><label for="url"><?php _e('Focal Length'); ?></label></th>
     192                                        <td><?php echo $attachment_data['image_meta']['focal_length']; ?>mm</td>
     193                                </tr>
     194                                <tr>
     195                                        <th scope="row"><label for="url"><?php _e('ISO'); ?></label></th>
     196                                        <td><?php echo $attachment_data['image_meta']['iso']; ?></td>
     197                                </tr>
     198                                <tr>
     199                                        <th scope="row"><label for="url"><?php _e('Shutter Speed'); ?></label></th>
     200                                        <td><?php $secs = $attachment_data['image_meta']['shutter_speed'];
     201                                                echo ($secs > 0.0 and $secs < 1.0) ? ("1/" . round(1/$secs)) : ($secs); ?>s</td>
     202                                </tr>
     203                                <tr>
     204                                        <th scope="row"><label for="url"><?php _e('Title'); ?></label></th>
     205                                        <td><?php echo $attachment_data['image_meta']['title']; ?></td>
     206                                </tr>
     207                        <?php } ?>
    163208                        <tr id="buttons" class="submit">
    164209                                <td colspan='2'>
    165210<?php   if ( $id ) : ?>