WordPress.org

Make WordPress Core

Changeset 13244


Ignore:
Timestamp:
02/20/2010 09:17:34 AM (8 years ago)
Author:
nacin
Message:

Improved IPTC and EXIF handling. Drag a wider net and also try to avoid numeric attachment slugs where possible. Props miqrogroove. See #11417

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

Legend:

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

    r12823 r13244  
    253253    if ( is_callable('iptcparse') ) {
    254254        getimagesize($file, $info);
     255
    255256        if ( !empty($info['APP13']) ) {
    256257            $iptc = iptcparse($info['APP13']);
     258
     259            if ( ! empty($iptc['2#105'][0] ) ) // headline, "A brief synopsis of the caption."
     260                $meta['title'] = utf8_encode( trim( $iptc['2#105'][0] ) );
     261            elseif ( !empty($iptc['2#005'][0]) ) // title, "Many use the Title field to store the filename of the image, though the field may be used in many ways."
     262                $meta['title'] = utf8_encode( trim( $iptc['2#005'][0] ) );
     263
     264            if ( !empty( $iptc['2#120'][0] ) ) { // description / legacy caption
     265                $caption = utf8_encode( trim( $iptc['2#120'][0] ) );
     266                if ( empty( $meta['title'] ) ) {
     267                    // Assume the title is stored in 2:120 if it's short.
     268                    if ( strlen( $caption ) < 80 )
     269                        $meta['title'] = $caption;
     270                    else
     271                        $meta['caption'] = $caption;
     272                } elseif ( $caption != $meta['title'] ) {
     273                    $meta['caption'] = $caption;
     274                }
     275            }
     276
    257277            if ( !empty($iptc['2#110'][0]) ) // credit
    258278                $meta['credit'] = utf8_encode(trim($iptc['2#110'][0]));
    259             elseif ( !empty($iptc['2#080'][0]) ) // byline
     279            elseif ( !empty($iptc['2#080'][0]) ) // creator / legacy byline
    260280                $meta['credit'] = utf8_encode(trim($iptc['2#080'][0]));
     281
    261282            if ( !empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0]) ) // created date and time
    262283                $meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]);
    263             if ( !empty($iptc['2#120'][0]) ) // caption
    264                 $meta['caption'] = utf8_encode(trim($iptc['2#120'][0]));
     284
    265285            if ( !empty($iptc['2#116'][0]) ) // copyright
    266286                $meta['copyright'] = utf8_encode(trim($iptc['2#116'][0]));
    267             if ( !empty($iptc['2#005'][0]) ) // title
    268                 $meta['title'] = utf8_encode(trim($iptc['2#005'][0]));
    269287         }
    270288    }
     
    273291    if ( is_callable('exif_read_data') && in_array($sourceImageType, apply_filters('wp_read_image_metadata_types', array(IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM)) ) ) {
    274292        $exif = @exif_read_data( $file );
     293
     294        if ( !empty( $exif['Title'] ) )
     295            $meta['title'] = utf8_encode( trim( $exif['Title'] ) );
     296
     297        if ( ! empty( $exif['ImageDescription'] ) ) {
     298            if ( empty($meta['title']) && strlen( $exif['ImageDescription'] ) < 80 ) {
     299                // Assume the title is stored in ImageDescription
     300                $meta['title'] = utf8_encode( trim( $exif['ImageDescription'] ) );
     301                if ( ! empty( $exif['COMPUTED']['UserComment'] ) && trim( $exif['COMPUTED']['UserComment'] ) != $meta['title'] )
     302                    $meta['caption'] = utf8_encode( trim( $exif['COMPUTED']['UserComment'] ) );
     303            } elseif ( trim( $exif['ImageDescription'] ) != $meta['title'] ) {
     304                $meta['caption'] = utf8_encode( trim( $exif['ImageDescription'] ) );
     305            }
     306        } elseif ( ! empty( $exif['Comments'] ) && trim( $exif['Comments'] ) != $meta['title'] ) {
     307            $meta['caption'] = utf8_encode( trim( $exif['Comments'] ) );
     308        }
     309
     310        if ( ! empty( $exif['Artist'] ) )
     311            $meta['credit'] = utf8_encode( trim( $exif['Artist'] ) );
     312        elseif ( ! empty($exif['Author'] ) )
     313            $meta['credit'] = utf8_encode( trim( $exif['Author'] ) );
     314
     315        if ( ! empty( $exif['Copyright'] ) )
     316            $meta['copyright'] = utf8_encode( trim( $exif['Copyright'] ) );
    275317        if (!empty($exif['FNumber']))
    276318            $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 );
  • trunk/wp-admin/includes/media.php

    r13243 r13244  
    209209    // use image exif/iptc data for title and caption defaults if possible
    210210    if ( $image_meta = @wp_read_image_metadata($file) ) {
    211         if ( trim($image_meta['title']) )
     211        if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
    212212            $title = $image_meta['title'];
    213         if ( trim($image_meta['caption']) )
     213        if ( trim( $image_meta['caption'] ) )
    214214            $content = $image_meta['caption'];
    215215    }
     
    260260    // use image exif/iptc data for title and caption defaults if possible
    261261    if ( $image_meta = @wp_read_image_metadata($file) ) {
    262         if ( trim($image_meta['title']) )
     262        if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
    263263            $title = $image_meta['title'];
    264         if ( trim($image_meta['caption']) )
     264        if ( trim( $image_meta['caption'] ) )
    265265            $content = $image_meta['caption'];
    266266    }
Note: See TracChangeset for help on using the changeset viewer.