bp_core_avatar_handle_crop

Crop an uploaded avatar.

Description

bp_core_avatar_handle_crop( (string) $args = '' ); 

Parameters (1)

0. $args — Optional. (string) => ''
Array of function parameters.

Options

  • object (string) => ''

    Object type of the item whose avatar you're handling. user,, group., blog, or custom. Default: user,.

  • avatar_dir (string) => ''

    Subdirectory where avatar should be stored. Default: avatars..

  • item_id (bool|int) => 0

    ID of the item that the avatar belongs to.

  • original_file (bool|string) => ''

    Absolute path to the original avatar file.

  • crop_w (int) => 0

    Crop width. Default: the global full avatar width, as retrieved by bp_core_avatar_full_width().

  • crop_h (int) => 0

    Crop height. Default: the global full avatar height, as retrieved by bp_core_avatar_full_height().

  • crop_x (int) => 0

    The horizontal starting point of the crop. Default: 0.

array(

    /**
     * Object type of the item whose avatar you're handling. 'user', 'group', 'blog', or custom.
     * Default: 'user'.
     *
     * @type string
     * @default ''
     */
    'object' => '',

    /**
     * Subdirectory where avatar should be stored. Default: 'avatars'.
     *
     * @type string
     * @default ''
     */
    'avatar_dir' => '',

    /**
     * ID of the item that the avatar belongs to.
     *
     * @type bool|int
     */
    'item_id' => 0,

    /**
     * Absolute path to the original avatar file.
     *
     * @type bool|string
     * @default ''
     */
    'original_file' => '',

    /**
     * Crop width. Default: the global 'full' avatar width, as retrieved by
     * bp_core_avatar_full_width().
     *
     * @type int
     */
    'crop_w' => 0,

    /**
     * Crop height. Default: the global 'full' avatar height, as retrieved by
     * bp_core_avatar_full_height().
     *
     * @type int
     */
    'crop_h' => 0,

    /**
     * The horizontal starting point of the crop. Default: 0.
     *
     * @type int
     */
    'crop_x' => 0
);        


Usage

  1. if ( !function_exists( 'bp_core_avatar_handle_crop' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-avatars.php'; 
  3.  
  4. // Array of function parameters. 
  5. $args = array( 
  6. 'object' => '', 
  7. 'avatar_dir' => '', 
  8. 'item_id' => 0, 
  9. 'original_file' => '', 
  10. 'crop_w' => 0, 
  11. 'crop_h' => 0, 
  12. 'crop_x' => 0 
  13. ); 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = bp_core_avatar_handle_crop($args); 
  17.  

Defined (1)

The function is defined in the following location(s).

/bp-core/bp-core-avatars.php  
  1. function bp_core_avatar_handle_crop( $args = '' ) { 
  2.  
  3. $r = wp_parse_args( $args, array( 
  4. 'object' => 'user',  
  5. 'avatar_dir' => 'avatars',  
  6. 'item_id' => false,  
  7. 'original_file' => false,  
  8. 'crop_w' => bp_core_avatar_full_width(),  
  9. 'crop_h' => bp_core_avatar_full_height(),  
  10. 'crop_x' => 0,  
  11. 'crop_y' => 0 
  12. ) ); 
  13.  
  14. /** 
  15. * Filters whether or not to handle cropping. 
  16. * If you want to override this function, make sure you return false. 
  17. * @since 1.2.4 
  18. * @param bool $value Whether or not to crop. 
  19. * @param array $r Array of parsed arguments for function. 
  20. */ 
  21. return true; 
  22.  
  23. // Crop the file. 
  24. $avatar_attachment = new BP_Attachment_Avatar(); 
  25. $cropped = $avatar_attachment->crop( $r ); 
  26.  
  27. // Check for errors. 
  28. if ( empty( $cropped['full'] ) || empty( $cropped['thumb'] ) || is_wp_error( $cropped['full'] ) || is_wp_error( $cropped['thumb'] ) ) { 
  29. return false; 
  30.  
  31. return true;