Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/03/2015 10:42:02 PM (11 years ago)
Author:
imath
Message:

Make sure uploaded images are oriented the right way.

When using a mobile device to set an avatar or a cover image, there are cases when the image can be saved upside down. We need to check the exif Orientation of uploaded images and rotate them if needed.

Introduce 2 new methods into the BP_Attachment class:

  • BP_Attachment->get_image_data() to get an array of all image datas (including exif datas)
  • BP_Attachment->edit_image() to edit the image orientation and/or resize the image.

Use these two new methods within the BP_Attachment_Avatar->shrink() and BP_Attachment_Cover_Image()->fit() methods.

Finally include a unit test for the BP_Attachment->get_image_data() method to check we get the orientation meta for all supported WorPress versions.

Props modemlooper, shanebp, r-a-y, DJPaul

Fixes #5089
See #6570

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/classes/class-bp-attachment.php

    r10108 r10178  
    522522        return $script_data;
    523523    }
     524
     525    /**
     526     * Get full data for an image
     527     *
     528     * @since  2.4.0
     529     *
     530     * @param  string $file Absolute path to the uploaded image.
     531     * @return bool|array   An associate array containing the width, height and metadatas.
     532     *                      False in case an important image attribute is missing.
     533     */
     534    public static function get_image_data( $file ) {
     535        // Try to get image basic data
     536        list( $width, $height, $sourceImageType ) = @getimagesize( $file );
     537
     538        // No need to carry on if we couldn't get image's basic data.
     539        if ( is_null( $width ) || is_null( $height ) || is_null( $sourceImageType ) ) {
     540            return false;
     541        }
     542
     543        // Initialize the image data
     544        $image_data = array(
     545            'width'  => $width,
     546            'height' => $height,
     547        );
     548
     549        /**
     550         * Make sure the wp_read_image_metadata function is reachable for the old Avatar UI
     551         * or if WordPress < 3.9 (New Avatar UI is not available in this case)
     552         */
     553        if ( ! function_exists( 'wp_read_image_metadata' ) ) {
     554            require_once( ABSPATH . 'wp-admin/includes/image.php' );
     555        }
     556
     557        // Now try to get image's meta data
     558        $meta = wp_read_image_metadata( $file );
     559
     560        if ( ! empty( $meta ) ) {
     561            // Before 4.0 the Orientation wasn't included
     562            if ( ! isset( $meta['orientation'] ) &&
     563                is_callable( 'exif_read_data' ) &&
     564                in_array( $sourceImageType, apply_filters( 'wp_read_image_metadata_types', array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ) ) )
     565            ) {
     566                $exif = exif_read_data( $file );
     567
     568                if ( ! empty( $exif['Orientation'] ) ) {
     569                    $meta['orientation'] = $exif['Orientation'];
     570                }
     571            }
     572
     573            // Now add the metas to image data
     574            $image_data['meta'] = $meta;
     575        }
     576
     577        /**
     578         * Filter here to add/remove/edit data to the image full data
     579         *
     580         * @since  2.4.0
     581         *
     582         * @param  array $image_data An associate array containing the width, height and metadatas.
     583         */
     584        return apply_filters( 'bp_attachments_get_image_data', $image_data );
     585    }
     586
     587    /**
     588     * Edit an image file to resize it or rotate it
     589     *
     590     * @since  2.4.0
     591     *
     592     * @param  string $attachment_type The attachment type (eg: avatar or cover_image). Required.
     593     * @param  array  array $args {
     594     *     @type string $file     Absolute path to the image file (required).
     595     *     @type int    $max_w    Max width attribute for the editor's resize method (optional).
     596     *     @type int    $max_h    Max height attribute for the editor's resize method (optional).
     597     *     @type bool   $crop     Crop attribute for the editor's resize method (optional).
     598     *     @type float  $rotate   Angle for the editor's rotate method (optional).
     599     *     @type int    $quality  Compression quality on a 1-100% scale (optional).
     600     *     @type bool   $save     Whether to use the editor's save method or not (optional).
     601     * }
     602     *
     603     * @return string|WP_Image_Editor|WP_Error The edited image path or the WP_Image_Editor object in case of success,
     604     *                                         an WP_Error object otherwise.
     605     */
     606    public static function edit_image( $attachment_type, $args = array() ) {
     607        if ( empty( $attachment_type ) ) {
     608            return new WP_Error( 'missing_parameter' );
     609        }
     610
     611        $r = bp_parse_args( $args, array(
     612            'file'   => '',
     613            'max_w'   => 0,
     614            'max_h'   => 0,
     615            'crop'    => false,
     616            'rotate'  => 0,
     617            'quality' => 90,
     618            'save'    => true,
     619        ), 'attachment_' . $attachment_type . '_edit_image' );
     620
     621        // Make sure we have to edit the image.
     622        if ( empty( $r['max_w'] ) && empty( $r['max_h'] ) && empty( $r['rotate'] ) && empty( $r['file'] ) ) {
     623            return new WP_Error( 'missing_parameter' );
     624        }
     625
     626        // Get the image editor
     627        $editor = wp_get_image_editor( $r['file'] );
     628
     629        if ( is_wp_error( $editor ) ) {
     630            return $editor;
     631        }
     632
     633        $editor->set_quality( $r['quality'] );
     634
     635        if ( ! empty( $r['rotate'] ) ) {
     636            $rotated = $editor->rotate( $r['rotate'] );
     637
     638            // Stop in case of error
     639            if ( is_wp_error( $rotated ) ) {
     640                return $rotated;
     641            }
     642        }
     643
     644        if ( ! empty( $r['max_w'] ) || ! empty( $r['max_h'] ) ) {
     645            $resized = $editor->resize( $r['max_w'], $r['max_h'], $r['crop'] );
     646
     647            // Stop in case of error
     648            if ( is_wp_error( $resized ) ) {
     649                return $resized;
     650            }
     651        }
     652
     653        // Use the editor save method to get a path to the edited image
     654        if ( true === $r['save'] ) {
     655            return $editor->save( $editor->generate_filename() );
     656
     657        // Need to do some other edit actions or use a specific method to save file
     658        } else {
     659            return $editor;
     660        }
     661    }
    524662}
Note: See TracChangeset for help on using the changeset viewer.