bp_caver_avatar_handle_upload

The BuddyPress cover bp caver avatar handle upload function.

Description

bp_caver_avatar_handle_upload(); 

Usage

  1. if ( !function_exists( 'bp_caver_avatar_handle_upload' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress-cover/bp-cover.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bp_caver_avatar_handle_upload(); 
  7.  

Defined (1)

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

/bp-cover.php  
  1. function bp_caver_avatar_handle_upload() { 
  2. global $bp; 
  3. if( $_POST['encodedimg'] ) { 
  4. $user_id = !empty( $_POST['user_id'] ) ? $_POST['user_id'] : bp_displayed_user_id() ;  
  5. $imgresponse = array(); 
  6. $uploaddir = bp_core_avatar_upload_path() . '/avatars';  
  7. if( ! file_exists( $uploaddir ) ) {  
  8. mkdir( $uploaddir );  
  9. }  
  10. $img = $_POST['encodedimg']; 
  11. $img = str_replace('data:'.$_POST['imgtype'].';base64, ', '', $img); 
  12. $img = str_replace(' ', '+', $img); 
  13. $data = base64_decode($img);  
  14. $filepath = $uploaddir . '/' . $user_id; 
  15. if( ! file_exists( $filepath ) ) {  
  16. mkdir( $filepath );  
  17. $imgname = wp_unique_filename( $uploaddir, $_POST['imgname'] );  
  18. $fileurl = $filepath . '/' . $imgname; 
  19. $siteurl = trailingslashit( get_blog_option( 1, 'siteurl' ) ); 
  20. $url = str_replace(ABSPATH, $siteurl, $fileurl);  
  21. $success = file_put_contents($fileurl, $data); 
  22. $file = $_POST['imgsize'] ; 
  23. $max_upload_size=bp_cover_get_max_media_size();  
  24. if( $max_upload_size > $file) {  
  25. if($success) { 
  26. $imgresponse[0] = "1"; 
  27. $imgresponse[1] = $fileurl; 
  28.  
  29. $size = getimagesize( $fileurl ); 
  30.  
  31. /** Check image size and shrink if too large */ 
  32. if ( $size[0] > 150 ) { 
  33. $original_file = image_resize( $fileurl, 150, 150, true ); 
  34. //$ava_file = image_resize( $fileurl, 250, 250, true ); 
  35. /** Check for thumbnail creation errors */ 
  36. if ( is_wp_error( $original_file ) ) { 
  37. $imgresponse[0] = "0"; 
  38. $imgresponse[1] = sprintf( __( 'Upload Failed! Error was: %s', 'bp-cover' ), $original_file->get_error_message() ); 
  39. die(); 
  40.  
  41. $avatar_to_crop = str_replace( bp_core_avatar_upload_path(), '', $original_file ); 
  42. bp_core_delete_existing_avatar( array( 'item_id' => $user_id, 'avatar_path' => bp_core_avatar_upload_path() .'/avatars/' . $user_id ) ); 
  43. $crop_args = array( 'item_id' => $user_id, 'original_file' => $avatar_to_crop, 'crop_w' => 0, 'crop_h' => 0);  
  44. bp_core_avatar_handle_crop( $crop_args ) ;  
  45. //$url = str_replace(ABSPATH, $siteurl, $ava_file);  
  46. update_user_meta(bp_loggedin_user_id(), 'profile_avatar', $url); 
  47. } else{  
  48.  
  49. $imgresponse[0] = "0"; 
  50. $imgresponse[1] = __('Upload Failed! Your photo must be larger than 150px', 'bp-cover'); 
  51.  
  52.  
  53. } else { 
  54. $imgresponse[0] = "0"; 
  55. $imgresponse[1] = __('Upload Failed! Unable to write the image on server', 'bp-cover'); 
  56.  
  57. } else { 
  58. $imgresponse[0] = "0"; 
  59. $imgresponse[1] = sprintf( __( 'The file you uploaded is too big. Please upload a file under %s', 'bp-cover'), size_format($max_upload_size) ); 
  60.  
  61. }  
  62. }else { 
  63. $imgresponse[0] = "0"; 
  64. $imgresponse[1] = __('Upload Failed! No image sent', 'bp-cover'); 
  65.  
  66. /** if everything is ok, we send back url to thumbnail and to full image */ 
  67. echo json_encode( $imgresponse ); 
  68. die();