通过WP_REST_API更换BP头像

Change BP Avatar through WP_REST_API

我目前正在扩展 wp rest API 以集中所有内容并能够从网络应用程序转移到真正的应用程序 (我就懒得更新删除头像+社交登录就完成了API)

这份工作经历了很多,也学到了很多东西,但是我才过了一天API上传和头像都没有成功,所以没有任何进展

我从中得到了一个“真”值:bp_core_avatar_handle_upload($_FILES['avatar'],'xprofile_avatar_upload_dir'); 但没有其他事情发生(甚至没有上传)

我仍在努力寻找出路,但我想在这件事上伸出援手

感谢

编辑: 由于我无法让它工作并且头像是根据“-bp-full”和“-bp-thumb”文件的最后更新选择的,我将把文件放在 /uploads/avatars/ {id}/

现在我正在尝试了解如何调整大小以制作缩略图 我当前的代码如下所示

$file = $_FILES['file'];
        $file_meta = getimagesize($file["tmp_name"]);
        if($file_meta !== false){

          if($file_meta[0] == $file_meta[1]){

            $file_path = $file['tmp_name'];
            $FILE_EXTENSION = pathinfo($path, PATHINFO_EXTENSION);

            // use 'avatar-bp....' instead of default wp_hash($file['name'].time())
            // to avoid having multiple image for each user
            $full_filename  = 'avatar-bpfull.'  . $FILE_EXTENSION;
            $thumb_filename = 'avatar-bpthumb.' . $FILE_EXTENSION;
            $target_dir = wp_get_upload_dir()['basedir'].'/avatars/'.$user_id.'/';

            for($i = 0; $i++; $i<=1){
              if($i == 0){
                $width = 150;
              }else{
                $width = 80;
              }

              $height = $width;

              $image_p = imagecreatetruecolor($width, $height);
              $image = imagecreatefromjpeg($file_path);
              imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
              if($i==0){
                $image_full = $image_p;
              }else{
                $image_thumb = $image_p;
              }
            }
            return $image_full;

            // move from /tmp
            if (rename($file_path, $target_dir.$thumb_filename) && rename($file_path, $target_dir.$full_filename)) {
                return "The file has been uploaded.";
            } else {
                return "Sorry, there was an error uploading your file.";
            }
          }else{
            return 'not a squared image';
          }
        }else{
          return 'not an image';
        }

我做错了什么吗?我应该遵循一些最佳实践吗? 谢谢 :)

如果有人需要这样做,您需要了解两件事:

  1. buddypress 将图像存储在 uploads/avatars/{user_id}
  2. buddypress select 图片基于 'last_modification' 文件 meta

因此,如果您不知道如何使用 bp_core_avatar_handle_upload 函数,这是我的解决方法: (我的脚本应该只接收方形图像,如 'if($file_meta[0] == $file_meta[1])' 所示,如果需要,请将其取出)

$image_full;
$file = $_FILES['file'];
$file_path = $file['tmp_name'];
$file_meta = getimagesize($file_path);

if($file_meta !== false){

  if($file_meta[0] == $file_meta[1]){
    // use 'avatar-bp....' instead of default wp_hash($file['name'].time())
    // to avoid having multiple image for each user
    $full_filename  = 'avatar-bpfull.'  . $FILE_EXTENSION;
    $thumb_filename = 'avatar-bpthumb.' . $FILE_EXTENSION;
    $target_dir = wp_get_upload_dir()['basedir'].'/avatars/'.$user_id.'/';

    $source = imagecreatefromstring(file_get_contents($file_path)); // La photo est la source

    $full = imagecreatetruecolor(150, 150);
    $thumb = imagecreatetruecolor(80, 80);

    imagecopyresampled($full, $source, 0, 0, 0, 0, imagesx($full), imagesy($full), imagesx($source), imagesy($source));
    imagecopyresampled($thumb, $source, 0, 0, 0, 0, imagesx($thumb), imagesy($thumb), imagesx($source), imagesy($source));

    if(imagejpeg($thumb, $target_dir.$thumb_filename.'jpeg') && imagejpeg($full, $target_dir.$full_filename.'jpeg')){
      return "The file has been uploaded.";
    } else {
      return "Sorry, there was an error uploading your file.";
    }
  }else{
    return 'not a squared image';
  }
}else{
  return 'not an image';
}