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
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
| 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.