From 2d1eae820f0915a561494c1a12fd2dec1759e6fd Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Tue, 18 Sep 2012 23:38:25 +0000 Subject: [PATCH] Avoid mangling UTF-8 strings that may be present in image metadata. props SergeyBiryukov for the unit tests [UT665]. fixes #9417. git-svn-id: http://core.svn.wordpress.org/trunk@21905 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/image.php | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/wp-admin/includes/image.php b/wp-admin/includes/image.php index 2de1a7ee3b..5af65465d9 100644 --- a/wp-admin/includes/image.php +++ b/wp-admin/includes/image.php @@ -237,13 +237,13 @@ function wp_read_image_metadata( $file ) { // headline, "A brief synopsis of the caption." if ( ! empty( $iptc['2#105'][0] ) ) - $meta['title'] = utf8_encode( trim( $iptc['2#105'][0] ) ); + $meta['title'] = trim( $iptc['2#105'][0] ); // title, "Many use the Title field to store the filename of the image, though the field may be used in many ways." elseif ( ! empty( $iptc['2#005'][0] ) ) - $meta['title'] = utf8_encode( trim( $iptc['2#005'][0] ) ); + $meta['title'] = trim( $iptc['2#005'][0] ); if ( ! empty( $iptc['2#120'][0] ) ) { // description / legacy caption - $caption = utf8_encode( trim( $iptc['2#120'][0] ) ); + $caption = trim( $iptc['2#120'][0] ); if ( empty( $meta['title'] ) ) { // Assume the title is stored in 2:120 if it's short. if ( strlen( $caption ) < 80 ) @@ -256,15 +256,15 @@ function wp_read_image_metadata( $file ) { } if ( ! empty( $iptc['2#110'][0] ) ) // credit - $meta['credit'] = utf8_encode(trim($iptc['2#110'][0])); + $meta['credit'] = trim( $iptc['2#110'][0] ); elseif ( ! empty( $iptc['2#080'][0] ) ) // creator / legacy byline - $meta['credit'] = utf8_encode(trim($iptc['2#080'][0])); + $meta['credit'] = trim( $iptc['2#080'][0] ); if ( ! empty( $iptc['2#055'][0] ) and ! empty( $iptc['2#060'][0] ) ) // created date and time $meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] ); if ( ! empty( $iptc['2#116'][0] ) ) // copyright - $meta['copyright'] = utf8_encode( trim( $iptc['2#116'][0] ) ); + $meta['copyright'] = trim( $iptc['2#116'][0] ); } } @@ -273,44 +273,49 @@ function wp_read_image_metadata( $file ) { $exif = @exif_read_data( $file ); if ( !empty( $exif['Title'] ) ) - $meta['title'] = utf8_encode( trim( $exif['Title'] ) ); + $meta['title'] = trim( $exif['Title'] ); if ( ! empty( $exif['ImageDescription'] ) ) { if ( empty( $meta['title'] ) && strlen( $exif['ImageDescription'] ) < 80 ) { // Assume the title is stored in ImageDescription - $meta['title'] = utf8_encode( trim( $exif['ImageDescription'] ) ); + $meta['title'] = trim( $exif['ImageDescription'] ); if ( ! empty( $exif['COMPUTED']['UserComment'] ) && trim( $exif['COMPUTED']['UserComment'] ) != $meta['title'] ) - $meta['caption'] = utf8_encode( trim( $exif['COMPUTED']['UserComment'] ) ); + $meta['caption'] = trim( $exif['COMPUTED']['UserComment'] ); } elseif ( trim( $exif['ImageDescription'] ) != $meta['title'] ) { - $meta['caption'] = utf8_encode( trim( $exif['ImageDescription'] ) ); + $meta['caption'] = trim( $exif['ImageDescription'] ); } } elseif ( ! empty( $exif['Comments'] ) && trim( $exif['Comments'] ) != $meta['title'] ) { - $meta['caption'] = utf8_encode( trim( $exif['Comments'] ) ); + $meta['caption'] = trim( $exif['Comments'] ); } if ( ! empty( $exif['Artist'] ) ) - $meta['credit'] = utf8_encode( trim( $exif['Artist'] ) ); + $meta['credit'] = trim( $exif['Artist'] ); elseif ( ! empty($exif['Author'] ) ) - $meta['credit'] = utf8_encode( trim( $exif['Author'] ) ); + $meta['credit'] = trim( $exif['Author'] ); if ( ! empty( $exif['Copyright'] ) ) - $meta['copyright'] = utf8_encode( trim( $exif['Copyright'] ) ); + $meta['copyright'] = trim( $exif['Copyright'] ); if ( ! empty($exif['FNumber'] ) ) $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 ); if ( ! empty($exif['Model'] ) ) - $meta['camera'] = utf8_encode( trim( $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'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings']; - $meta['iso'] = utf8_encode( trim( $meta['iso'] ) ); + $meta['iso'] = trim( $meta['iso'] ); } if ( ! empty($exif['ExposureTime'] ) ) $meta['shutter_speed'] = wp_exif_frac2dec( $exif['ExposureTime'] ); } + foreach ( array( 'title', 'caption', 'credit', 'copyright', 'camera', 'iso' ) as $key ) { + if ( $meta[ $key ] && ! seems_utf8( $meta[ $key ] ) ) + $meta[ $key ] = utf8_encode( $meta[ $key ] ); + } + return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType ); }