Make WordPress Core


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

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

File:
1 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?>
Note: See TracChangeset for help on using the changeset viewer.