/** * Adjust the cover image to fit with advised width & height. * * @since 2.4.0 * * @param string $file the absolute path to the file. * @return mixed */ 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)); }
/** * Maybe shrink the attachment to fit maximum allowed width. * * @since 2.3.0 * @since 2.4.0 Add the $ui_available_width parameter, to inform about the Avatar UI width. * * @uses bp_core_avatar_original_max_width() * * @param string $file The absolute path to the file. * @param int $ui_available_width Available width for the UI. * @return mixed */ 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); }
/** * @group avatars * @group cover_images */ public function test_bp_attachment_get_image_data() { $image_data = BP_Attachment::get_image_data(BP_TESTS_DIR . 'assets/upside-down.jpg'); $this->assertTrue(3 === $image_data['meta']['orientation']); }