Skip to:
Content

BuddyPress.org

Ticket #5089: 5089.05.patch

File 5089.05.patch, 11.7 KB (added by imath, 11 years ago)
  • src/bp-core/classes/class-bp-attachment-avatar.php

    diff --git src/bp-core/classes/class-bp-attachment-avatar.php src/bp-core/classes/class-bp-attachment-avatar.php
    index 91cd15c..604ee23 100644
    class BP_Attachment_Avatar extends BP_Attachment { 
    119119         * @since 2.3.0
    120120         *
    121121         * @uses  bp_core_avatar_original_max_width()
    122          * @uses  wp_get_image_editor()
    123122         *
    124123         * @param string $file the absolute path to the file.
    125124         *
    class BP_Attachment_Avatar extends BP_Attachment { 
    127126         */
    128127        public static function shrink( $file = '' ) {
    129128                // Get image size
    130                 $size   = @getimagesize( $file );
    131                 $retval = false;
     129                $avatar_data = parent::get_image_data( $file );
    132130
    133                 // Check image size and shrink if too large
    134                 if ( $size[0] > bp_core_avatar_original_max_width() ) {
    135                         $editor = wp_get_image_editor( $file );
     131                // Init the edit args
     132                $edit_args = array();
    136133
    137                         if ( ! is_wp_error( $editor ) ) {
    138                                 $editor->set_quality( 100 );
     134                // Do we need to resize the image ?
     135                if ( isset( $avatar_data['width'] ) && $avatar_data['width'] > bp_core_avatar_original_max_width() ) {
     136                        $edit_args = array(
     137                                'max_w' => bp_core_avatar_original_max_width(),
     138                                'max_h' => bp_core_avatar_original_max_width(),
     139                        );
     140                }
    139141
    140                                 $resized = $editor->resize( bp_core_avatar_original_max_width(), bp_core_avatar_original_max_width(), false );
    141                                 if ( ! is_wp_error( $resized ) ) {
    142                                         $thumb = $editor->save( $editor->generate_filename() );
    143                                 } else {
    144                                         $retval = $resized;
    145                                 }
     142                // Do we need to rotate the image ?
     143                $angles = array(
     144                        3 => 180,
     145                        6 => -90,
     146                        8 =>  90,
     147                );
    146148
    147                                 // Check for thumbnail creation errors
    148                                 if ( ( false === $retval ) && is_wp_error( $thumb ) ) {
    149                                         $retval = $thumb;
    150                                 }
     149                if ( isset( $avatar_data['meta']['orientation'] ) && isset( $angles[ $avatar_data['meta']['orientation'] ] ) ) {
     150                        $edit_args['rotate'] = $angles[ $avatar_data['meta']['orientation'] ];
     151                }
    151152
    152                                 // Thumbnail is good so proceed
    153                                 if ( false === $retval ) {
    154                                         $retval = $thumb;
    155                                 }
     153                // No need to edit the avatar, original file will be used
     154                if ( empty( $edit_args ) ) {
     155                        return false;
    156156
    157                         } else {
    158                                 $retval = $editor;
    159                         }
     157                // Add the file to the edit arguments
     158                } else {
     159                        $edit_args['file'] = $file;
    160160                }
    161161
    162                 return $retval;
     162                return parent::edit_image( 'avatar', $edit_args );
    163163        }
    164164
    165165        /**
  • src/bp-core/classes/class-bp-attachment-cover-image.php

    diff --git src/bp-core/classes/class-bp-attachment-cover-image.php src/bp-core/classes/class-bp-attachment-cover-image.php
    index fc700f9..ed32cbc 100644
    class BP_Attachment_Cover_Image extends BP_Attachment { 
    138138                }
    139139
    140140                // Get image size
    141                 $size   = @getimagesize( $file );
    142                 $retval = false;
    143 
    144                 // Check image size and shrink if too large
    145                 if ( $size[0] > $dimensions['width'] || $size[1] > $dimensions['height'] ) {
    146                         $editor = wp_get_image_editor( $file );
    147 
    148                         if ( ! is_wp_error( $editor ) ) {
    149                                 $editor->set_quality( 100 );
    150 
    151                                 $resized = $editor->resize( $dimensions['width'], $dimensions['height'], true );
    152                                 if ( ! is_wp_error( $resized ) ) {
    153                                         $cover   = $editor->save( $this->generate_filename( $file ) );
    154                                 } else {
    155                                         $retval = $resized;
    156                                 }
    157 
    158                                 // Check for cover creation errors
    159                                 if ( ( false === $retval ) && is_wp_error( $cover ) ) {
    160                                         $retval = $cover;
    161                                 }
    162 
    163                                 // Cover is good so proceed
    164                                 if ( false === $retval ) {
    165                                         $retval = $cover;
    166                                 }
    167 
    168                         } else {
    169                                 $retval = $editor;
    170                         }
     141                $cover_data = parent::get_image_data( $file );
     142
     143                // Init the edit args
     144                $edit_args = array();
     145
     146                // Do we need to resize the image ?
     147                if ( ( isset( $cover_data['width'] ) && $cover_data['width'] > $dimensions['width'] ) ||
     148                ( isset( $cover_data['height'] ) && $cover_data['height'] > $dimensions['height'] ) ) {
     149                        $edit_args = array(
     150                                'max_w' => $dimensions['width'],
     151                                'max_h' => $dimensions['height'],
     152                                'crop'  => true,
     153                        );
     154                }
     155
     156                // Do we need to rotate the image ?
     157                $angles = array(
     158                        3 => 180,
     159                        6 => -90,
     160                        8 =>  90,
     161                );
     162
     163                if ( isset( $cover_data['meta']['orientation'] ) && isset( $angles[ $cover_data['meta']['orientation'] ] ) ) {
     164                        $edit_args['rotate'] = $angles[ $cover_data['meta']['orientation'] ];
     165                }
     166
     167                // No need to edit the avatar, original file will be used
     168                if ( empty( $edit_args ) ) {
     169                        return false;
     170
     171                // Add the file to the edit arguments
     172                } else {
     173                        $edit_args = array_merge( $edit_args, array( 'file' => $file, 'save' => false ) );
     174                }
     175
     176                // Get the editor so that we can use a specific save method
     177                $editor = parent::edit_image( 'cover_image', $edit_args );
     178
     179                if ( is_wp_error( $editor ) )  {
     180                        return $editor;
     181                } elseif ( ! is_a( $editor, 'WP_Image_Editor' ) ) {
     182                        return false;
    171183                }
    172184
    173                 return $retval;
     185                // Save the new image file
     186                return $editor->save( $this->generate_filename( $file ) );
    174187        }
    175188
    176189        /**
  • src/bp-core/classes/class-bp-attachment.php

    diff --git src/bp-core/classes/class-bp-attachment.php src/bp-core/classes/class-bp-attachment.php
    index 7ea8c50..7a3143d 100644
    abstract class BP_Attachment { 
    521521
    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}
  • tests/phpunit/testcases/core/class-bp-attachment.php

    diff --git tests/phpunit/testcases/core/class-bp-attachment.php tests/phpunit/testcases/core/class-bp-attachment.php
    index 35d0476..adacf38 100644
    class BP_Tests_BP_Attachment_TestCases extends BP_UnitTestCase { 
    364364                $_FILES = $reset_files;
    365365                $_POST = $reset_post;
    366366        }
     367
     368        /**
     369         * @ticket BP5089
     370         */
     371        public function test_bp_attachment_avatar_shrink() {
     372                $image = BP_TESTS_DIR . 'assets/upside-down.jpg';
     373
     374                $dir_copy = bp_upload_dir();
     375
     376                // in case cleaning files fails
     377                if ( ! is_dir( $dir_copy['basedir'] . '/shrink' ) ) {
     378                        mkdir( $dir_copy['basedir'] . '/shrink' );
     379                }
     380
     381                $abs_path_copy = $dir_copy['basedir'] . '/shrink/upside-down.jpg';
     382
     383                copy( $image, $abs_path_copy );
     384
     385                add_filter( 'bp_core_avatar_original_max_width', array( $this, 'limit_to_50px' ) );
     386
     387                $shrink = BP_Attachment_Avatar::shrink( $abs_path_copy );
     388
     389                remove_filter( 'bp_core_avatar_original_max_width', array( $this, 'limit_to_50px' ) );
     390
     391                $this->assertTrue( 50 === $shrink['width'] && 50 === $shrink['height'] );
     392
     393                // Cleanup
     394                $this->clean_files( 'shrink' );
     395        }
     396
     397        public function limit_to_50px( $max_width ) {
     398                return 50;
     399        }
     400
     401        /**
     402         * @ticket BP5089
     403         */
     404        public function test_bp_attachment_cover_image_fit() {
     405                $image = BP_TESTS_DIR . 'assets/upside-down.jpg';
     406
     407                $cover_image_class = new BP_Attachment_Cover_Image();
     408
     409                $abs_path_copy = $cover_image_class->upload_path . '/upside-down.jpg';
     410
     411                copy( $image, $abs_path_copy );
     412
     413                $fit = $cover_image_class->fit( $abs_path_copy, array( 'width' => 50, 'height' => 50 ) );
     414
     415                $this->assertTrue( 50 === $fit['width'] && 50 === $fit['height'] );
     416
     417                // Cleanup
     418                $this->clean_files( 'buddypress' );
     419        }
     420
     421        /**
     422         * @ticket BP5089
     423         */
     424        public function test_bp_attachment_get_image_data() {
     425                $image_data = BP_Attachment::get_image_data( BP_TESTS_DIR . 'assets/upside-down.jpg' );
     426
     427                $this->assertTrue( 3 === $image_data['meta']['orientation'] );
     428        }
    367429}