BP_Attachment_Cover_Image::fit( string $file = '', array $dimensions = array() )

Adjust the cover photo to fit with advised width & height.

Description

Parameters

$file

(Optional) The absolute path to the file.

Default value: ''

$dimensions

(Optional) Array of dimensions for the cover photo.

Default value: array()

Return

(mixed)

Source

File: bp-core/classes/class-bp-attachment-cover-image.php

112
113
114
115
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
public function fit( $file = '', $dimensions = array() ) {
    if ( empty( $dimensions['width'] ) || empty( $dimensions['height'] ) ) {
        return false;
    }
 
    // Get image size.
    $cover_data = parent::get_image_data( $file );
 
    // Init the edit args.
    $edit_args = array();
 
    // Do we need to resize the image?
    if ( ( isset( $cover_data['width'] ) && $cover_data['width'] > $dimensions['width'] ) ||
    ( isset( $cover_data['height'] ) && $cover_data['height'] > $dimensions['height'] ) ) {
        $edit_args = array(
            'max_w' => $dimensions['width'],
            'max_h' => $dimensions['height'],
            'crop'  => true,
        );
    }
 
    // Do we need to rotate the image?
    $angles = array(
        3 => 180,
        6 => -90,
        8 =>  90,
    );
 
    if ( isset( $cover_data['meta']['orientation'] ) && isset( $angles[ $cover_data['meta']['orientation'] ] ) ) {
        $edit_args['rotate'] = $angles[ $cover_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 = array_merge( $edit_args, array( 'file' => $file, 'save' => false ) );
    }
 
    // Get the editor so that we can use a specific save method.
    $editor = parent::edit_image( 'cover_image', $edit_args );
 
    if ( is_wp_error( $editor ) )  {
        return $editor;
    } elseif ( ! is_a( $editor, 'WP_Image_Editor' ) ) {
        return false;
    }
 
    // Save the new image file.
    return $editor->save( $this->generate_filename( $file ) );
}

Changelog

Changelog
Version Description
BuddyPress 2.4.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.