BP_Attachment_Avatar::shrink( string $file = '', int $ui_available_width )

Maybe shrink the attachment to fit maximum allowed width.

Description

Parameters

$file

(Optional) The absolute path to the file.

Default value: ''

$ui_available_width

(Required) Available width for the UI.

Return

(false|string|WP_Image_Editor|WP_Error)

Source

File: bp-core/classes/class-bp-attachment-avatar.php

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
public static function shrink( $file = '', $ui_available_width = 0 ) {
    // Get image size.
    $avatar_data = parent::get_image_data( $file );
 
    // Init the edit args.
    $edit_args = array();
 
    // Defaults to the Avatar original max width constant.
    $original_max_width = bp_core_avatar_original_max_width();
 
    // The ui_available_width is defined and it's smaller than the Avatar original max width.
    if ( ! empty( $ui_available_width ) && $ui_available_width < $original_max_width ) {
        /**
         * In this case, to make sure the content of the image will be fully displayed
         * during the cropping step, let's use the Avatar UI Available width.
         */
        $original_max_width = $ui_available_width;
 
        // $original_max_width has to be larger than the avatar's full width
        if ( $original_max_width < bp_core_avatar_full_width() ) {
            $original_max_width = bp_core_avatar_full_width();
        }
    }
 
    // Do we need to resize the image?
    if ( isset( $avatar_data['width'] ) && $avatar_data['width'] > $original_max_width ) {
        $edit_args = array(
            'max_w' => $original_max_width,
            'max_h' => $original_max_width,
        );
    }
 
    // Do we need to rotate the image?
    $angles = array(
        3 => 180,
        6 => -90,
        8 =>  90,
    );
 
    if ( isset( $avatar_data['meta']['orientation'] ) && isset( $angles[ $avatar_data['meta']['orientation'] ] ) ) {
        $edit_args['rotate'] = $angles[ $avatar_data['meta']['orientation'] ];
    }
 
    // No need to edit the avatar, original file will be used.
    if ( empty( $edit_args ) ) {
        return false;
 
    // Add the file to the edit arguments.
    } else {
        $edit_args['file'] = $file;
    }
 
    return parent::edit_image( 'avatar', $edit_args );
}

Changelog

Changelog
Version Description
BuddyPress 2.4.0 Add the $ui_available_width parameter, to inform about the Avatar UI width. BuddyPress 2.4.0 Add the $ui_available_width parameter, to inform about the Avatar UI width.
BuddyPress 2.3.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.