Make WordPress Core

Changeset 57267


Ignore:
Timestamp:
01/10/2024 09:57:50 PM (9 months ago)
Author:
joedolson
Message:

Media: Fix handling of multibyte exif description metadata.

The exif standards expect the UserComment field to be used as a substitute for ImageDescription if multibyte characters are needed. WordPress media only mapped the ImageDescription field and did not correctly handle descriptions with multibyte characters.

Fix metadata saving to better handle media with multibyte characters in metadata and update unit tests.

Props fotodrachen, antpb, joedolson, mikinc860, azaozz, nicolefurlan.
Fixes #58082.

Location:
trunk
Files:
2 edited

Legend:

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

    r56743 r57267  
    864864        }
    865865
     866        $exif_description = '';
     867        $exif_usercomment = '';
    866868        if ( ! empty( $exif['ImageDescription'] ) ) {
     869            $exif_description = trim( $exif['ImageDescription'] );
     870        }
     871
     872        if ( ! empty( $exif['COMPUTED']['UserComment'] ) ) {
     873            $exif_usercomment = trim( $exif['COMPUTED']['UserComment'] );
     874        }
     875
     876        if ( $exif_description ) {
    867877            mbstring_binary_safe_encoding();
    868             $description_length = strlen( $exif['ImageDescription'] );
     878            $description_length = strlen( $exif_description );
    869879            reset_mbstring_encoding();
    870 
    871880            if ( empty( $meta['title'] ) && $description_length < 80 ) {
    872881                // Assume the title is stored in ImageDescription.
    873                 $meta['title'] = trim( $exif['ImageDescription'] );
    874             }
    875 
    876             if ( empty( $meta['caption'] ) && ! empty( $exif['COMPUTED']['UserComment'] ) ) {
    877                 $meta['caption'] = trim( $exif['COMPUTED']['UserComment'] );
     882                $meta['title'] = $exif_description;
     883            }
     884
     885            // If both user comments and description are present.
     886            if ( empty( $meta['caption'] ) && $exif_description && $exif_usercomment ) {
     887                if ( ! empty( $meta['title'] ) && $exif_description === $meta['title'] ) {
     888                    $caption = $exif_usercomment;
     889                } else {
     890                    if ( $exif_description === $exif_usercomment ) {
     891                        $caption = $exif_description;
     892                    } else {
     893                        $caption = trim( $exif_description . ' ' . $exif_usercomment );
     894                    }
     895                }
     896                $meta['caption'] = $caption;
     897            }
     898
     899            if ( empty( $meta['caption'] ) && $exif_usercomment ) {
     900                $meta['caption'] = $exif_usercomment;
    878901            }
    879902
    880903            if ( empty( $meta['caption'] ) ) {
    881                 $meta['caption'] = trim( $exif['ImageDescription'] );
     904                $meta['caption'] = $exif_description;
     905            }
     906        } elseif ( empty( $meta['caption'] ) && $exif_usercomment ) {
     907            $meta['caption']    = $exif_usercomment;
     908            $description_length = strlen( $exif_usercomment );
     909            if ( empty( $meta['title'] ) && $description_length < 80 ) {
     910                $meta['title'] = trim( $exif_usercomment );
    882911            }
    883912        } elseif ( empty( $meta['caption'] ) && ! empty( $exif['Comments'] ) ) {
  • trunk/tests/phpunit/tests/image/meta.php

    r52269 r57267  
    5252        $this->assertSame( '', $out['credit'], 'Credit value not the same' );
    5353        $this->assertSame( 'NIKON D70', $out['camera'], 'Camera value not the same' );
    54         $this->assertSame( '', $out['caption'], 'Caption value not the same' );
     54        $this->assertSame( 'Copyright Alex Shiels', $out['caption'], 'Caption value not the same' );
    5555        $this->assertEquals( strtotime( '2007-06-17 21:18:00' ), $out['created_timestamp'], 'Timestamp value not equivalent' );
    5656        $this->assertSame( '', $out['copyright'], 'Copyright value not the same' );
     
    5858        $this->assertEquals( 0, $out['iso'], 'Iso value not equivalent' ); // Interesting - a Nikon bug?
    5959        $this->assertEquals( 1 / 500, $out['shutter_speed'], 'Shutter speed value not equivalent' );
    60         $this->assertSame( '', $out['title'], 'Title value not the same' );
     60        $this->assertSame( 'Copyright Alex Shiels', $out['title'], 'Title value not the same' );
    6161        // $this->assertSame( array( 'Flowers' ), $out['keywords'] );
    6262    }
Note: See TracChangeset for help on using the changeset viewer.