WordPress.org

Make WordPress Core

Ticket #9257: 9257.4.diff

File 9257.4.diff, 3.2 KB (added by Mte90, 2 years ago)

new refreshed patch with unit test

  • src/wp-admin/includes/image.php

    diff --git src/wp-admin/includes/image.php src/wp-admin/includes/image.php
    index 8282499e8f..e6c8ce99db 100644
    function wp_exif_date2ts($str) { 
    323323        return strtotime( "{$y}-{$m}-{$d} {$time}" );
    324324}
    325325
     326/**
     327 * Convert the exif geo longitude and latitude format from degrees, minutes and
     328 * seconds to a float degrees.
     329 *
     330 * @since 4.9.0
     331 *
     332 * @param string $geo The coordinate to convert as standard.
     333 * @return float
     334 */
     335function wp_exif_gpsconvert($geo) {
     336        @list( $degree, $minute, $second ) = $geo;
     337        $float = wp_exif_frac2dec($degree)  + (wp_exif_frac2dec($minute)/60) + (wp_exif_frac2dec($second)/3600);
     338
     339        return ( ( is_float( $float ) || ( is_int( $float ) && $degree == $float ) ) && ( abs( $float ) <= 180 ) ) ? $float : 999;
     340}
     341
    326342/**
    327343 * Get extended image metadata, exif or iptc as available.
    328344 *
    329345 * Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso
    330  * created_timestamp, focal_length, shutter_speed, and title.
     346 * created_timestamp, focal_length, shutter_speed, title, gps_longitude and gps_latitude.
    331347 *
    332348 * The IPTC metadata that is retrieved is APP13, credit, byline, created date
    333349 * and time, caption, copyright, and title. Also includes FNumber, Model,
    function wp_read_image_metadata( $file ) { 
    364380                'title' => '',
    365381                'orientation' => 0,
    366382                'keywords' => array(),
     383                'gps_longitude' => 999,
     384                'gps_latitude' => 999,
    367385        );
    368386
    369387        $iptc = array();
    function wp_read_image_metadata( $file ) { 
    481499                if ( ! empty( $exif['ExposureTime'] ) ) {
    482500                        $meta['shutter_speed'] = (string) wp_exif_frac2dec( $exif['ExposureTime'] );
    483501                }
     502                if ( ! empty( $exif['GPSLongitude'] ) && count( $exif['GPSLongitude'] ) == 3 && !empty( $exif['GPSLongitudeRef'] ) ) {
     503                        $meta['gps_longitude'] = ( $exif['GPSLongitudeRef']== 'W' ? -1 : 1 ) * wp_exif_gpsconvert( $exif['GPSLongitude'] );
     504                }
     505                if ( ! empty( $exif['GPSLatitude'] ) && count($exif['GPSLatitude']) == 3 && !empty($exif['GPSLatitudeRef'] ) ) {
     506                        $meta['gps_latitude'] = ( $exif['GPSLatitudeRef']== 'S' ? -1 : 1 ) * wp_exif_gpsconvert( $exif['GPSLatitude'] );
     507                }
    484508                if ( ! empty( $exif['Orientation'] ) ) {
    485509                        $meta['orientation'] = $exif['Orientation'];
    486510                }
  • tests/phpunit/tests/image/meta.php

    diff --git tests/phpunit/data/images/test-gps-location.jpg tests/phpunit/data/images/test-gps-location.jpg
    new file mode 100644
    index 0000000000..18e8d45e47
    Binary files /dev/null and tests/phpunit/data/images/test-gps-location.jpg differ
    diff --git tests/phpunit/tests/image/meta.php tests/phpunit/tests/image/meta.php
    index f132d90d4e..defa77500e 100644
    class Tests_Image_Meta extends WP_UnitTestCase { 
    158158                $this->assertEquals( array( 'beach', 'baywatch', 'LA', 'sunset' ), $out['keywords'] );
    159159        }
    160160
     161
     162        /**
     163         * @ticket 9257
     164         */
     165        function test_exif_gps() {
     166                // exif from https://github.com/mkttanabe/exif/blob/master/test.jpg
     167                $out = wp_read_image_metadata(DIR_TESTDATA.'/images/test-gps-location.jpg');
     168
     169                $this->assertEquals('39.005833333333', $out['gps_longitude']);
     170                $this->assertEquals('-69.002833333333', $out['gps_latitude']);
     171
     172        }
     173
    161174}