bp_avatar_handle_capture

Handle avatar webcam capture.

Description

bp_avatar_handle_capture( (string) $data = '', (int) $item_id = 0 ); 

Parameters (2)

0. $data — Optional. (string) => ''
Base64 encoded image.
1. $item_id — Optional. (int)
Item to associate.

Usage

  1. if ( !function_exists( 'bp_avatar_handle_capture' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-avatars.php'; 
  3.  
  4. // Base64 encoded image. 
  5. $data = ''; 
  6.  
  7. // Item to associate. 
  8. $item_id = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bp_avatar_handle_capture($data, $item_id); 
  12.  

Defined (1)

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

/bp-core/bp-core-avatars.php  
  1. function bp_avatar_handle_capture( $data = '', $item_id = 0 ) { 
  2. if ( empty( $data ) || empty( $item_id ) ) { 
  3. return false; 
  4.  
  5. /** 
  6. * Filters whether or not to handle avatar webcam capture. 
  7. * If you want to override this function, make sure you return false. 
  8. * @since 2.5.1 
  9. * @param bool $value Whether or not to crop. 
  10. * @param string $data Base64 encoded image. 
  11. * @param int $item_id Item to associate. 
  12. */ 
  13. if ( ! apply_filters( 'bp_avatar_pre_handle_capture', true, $data, $item_id ) ) { 
  14. return true; 
  15.  
  16. $avatar_dir = bp_core_avatar_upload_path() . '/avatars'; 
  17.  
  18. // It's not a regular upload, we may need to create this folder. 
  19. if ( ! file_exists( $avatar_dir ) ) { 
  20. if ( ! wp_mkdir_p( $avatar_dir ) ) { 
  21. return false; 
  22.  
  23. /** 
  24. * Filters the Avatar folder directory. 
  25. * @since 2.3.0 
  26. * @param string $avatar_dir Directory for storing avatars. 
  27. * @param int $item_id ID of the item being acted on. 
  28. * @param string $value Avatar type. 
  29. * @param string $value Avatars word. 
  30. */ 
  31. $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', $avatar_dir . '/' . $item_id, $item_id, 'user', 'avatars' ); 
  32.  
  33. // It's not a regular upload, we may need to create this folder. 
  34. if( ! is_dir( $avatar_folder_dir ) ) { 
  35. if ( ! wp_mkdir_p( $avatar_folder_dir ) ) { 
  36. return false; 
  37.  
  38. $original_file = $avatar_folder_dir . '/webcam-capture-' . $item_id . '.png'; 
  39.  
  40. if ( file_put_contents( $original_file, $data ) ) { 
  41. $avatar_to_crop = str_replace( bp_core_avatar_upload_path(), '', $original_file ); 
  42.  
  43. // Crop to default values. 
  44. $crop_args = array( 'item_id' => $item_id, 'original_file' => $avatar_to_crop, 'crop_x' => 0, 'crop_y' => 0 ); 
  45.  
  46. return bp_core_avatar_handle_crop( $crop_args ); 
  47. } else { 
  48. return false;