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

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


Parameters Parameters

$file

(string) (Optional) The absolute path to the file.

Default value: ''

$dimensions

(array) (Optional) Array of dimensions for the cover image.

Default value: array()


Top ↑

Return Return

(mixed)


Source 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 ) );
	}


Top ↑

Changelog Changelog

Changelog
Version Description
2.4.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.