我终于找到了一个解决方案,我想发布它,因为它可能对某人有用!
public function update_user_avatar()
{
global $json_api;
if (!$json_api->query->user_id)
{
$json_api->error("Missing \'user_id\' parameter.");
}
if (!$json_api->query->image)
{
$json_api->error("Missing \'image\' parameter.");
}
$user_id = $json_api->query->user_id;
$base64 = $json_api->query->image;
$imgdata = base64_decode($base64);
$f = finfo_open();
$mime_type = finfo_buffer($f, $imgdata, FILEINFO_MIME_TYPE);
$type_file = explode(\'/\', $mime_type);
$time = time();
$avatar_thumb = $time . \'-bpthumb.\' . $type_file[1];
$avatar_full = $time . \'-bpfull.\' . $type_file[1];
$uploaddir = wp_upload_dir();
$myDirPath = $uploaddir["path"];
$myDirUrl = $uploaddir["url"];
$upload_path = \'/home/<folder>/public_html/wp-content/uploads/avatars/\'.$user_id;
// Create user id folder if not exist for avatar image
if (!file_exists($upload_path))
{
mkdir($upload_path, 0755, true);
}
require_once(ABSPATH . \'/wp-load.php\');
require_once(ABSPATH . \'wp-admin\' . \'/includes/file.php\');
require_once(ABSPATH . \'wp-admin\' . \'/includes/image.php\');
// Remove files into user\'s folder
$files = list_files($upload_path);
for($i = 0; $i<count($files); $i++)
{
wp_delete_file($files[$i]);
}
file_put_contents($upload_path.\'/\'.$avatar_thumb,$imgdata);
file_put_contents($upload_path.\'/\'.$avatar_full,$imgdata);
switch ($type_file[1]) {
case \'jpeg\':
# code...
$img_full = $this->resize_imagejpg($upload_path.\'/\'.$avatar_full, 150, 150);
$img_thumb = $this->resize_imagejpg($upload_path.\'/\'.$avatar_thumb, 50, 50);
break;
case \'jpg\':
# code...
$img_full = $this->resize_imagejpg($upload_path.\'/\'.$avatar_full, 150, 150);
$img_thumb = $this->resize_imagejpg($upload_path.\'/\'.$avatar_thumb, 50, 50);
break;
case \'png\':
# code...
$img_full = $this->resize_imagepng($upload_path.\'/\'.$avatar_full, 150, 150);
$img_thumb = $this->resize_imagepng($upload_path.\'/\'.$avatar_thumb, 50, 50);
break;
case \'gif\':
# code...
$img_full = $this->resize_imagegif($upload_path.\'/\'.$avatar_full, 150, 150);
$img_thumb = $this->resize_imagegif($upload_path.\'/\'.$avatar_thumb, 50, 50);
break;
default:
return array(
"success" => false,
"avatar_url" => get_avatar_url($user_id));
break;
}
// Override previous image
imagejpeg($img_thumb, $$upload_path.\'/\'.$avatar_full, 100);
imagejpeg($img_full, $upload_path.\'/\'.$avatar_thumb);
$filename = $myDirUrl.\'/\'.basename( $avatar_thumb );
$wp_filetype = wp_check_filetype(basename($filename), null );
$uploadfile = $upload_path.\'/\'. basename( $filename );
$attachment = array(
"post_mime_type" => $wp_filetype["type"],
"post_title" => preg_replace("/\\.[^.]+$/", "" , basename( $filename )),
"post_content" => "",
"post_status" => "inherit",
\'guid\' => $uploadfile,
);
$attachment_id = wp_insert_attachment( $attachment, $uploadfile );
update_post_meta($attachment_id,\'_wp_attachment_wp_user_avatar\',$user_id);
update_user_meta($user_id, \'wp_user_avatar\', $attachment_id);
return array(
"success" => true,
"avatar_url_thumb" => get_avatar_url($user_id),
"avatar_url_full" => $this->get_avatar_url_full($user_id));
}
以下是调整图像大小的方法(此答案中的代码
https://stackoverflow.com/questions/14649645/resize-image-in-php):
// for jpg
private function resize_imagejpg($file, $w, $h) {
list($width, $height) = getimagesize($file);
$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($w, $h);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $w, $h, $width, $height);
return $dst;
}
// for png
private function resize_imagepng($file, $w, $h) {
list($width, $height) = getimagesize($file);
$src = imagecreatefrompng($file);
$dst = imagecreatetruecolor($w, $h);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $w, $h, $width, $height);
return $dst;
}
// for gif
private function resize_imagegif($file, $w, $h) {
list($width, $height) = getimagesize($file);
$src = imagecreatefromgif($file);
$dst = imagecreatetruecolor($w, $h);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $w, $h, $width, $height);
return $dst;
}
这是我编写的用于检索完整化身图像url的函数:
private function get_avatar_url_full($user_id)
{
require_once(ABSPATH . \'/wp-load.php\');
require_once(ABSPATH . \'wp-admin\' . \'/includes/file.php\');
require_once(ABSPATH . \'wp-admin\' . \'/includes/image.php\');
$upload_path = \'/home/<folder>/public_html/wp-content/uploads/avatars/\'.$user_id;
$files = list_files($upload_path);
if (count($files) == 0 || count($files) == 1)
{
return get_avatar_url($user_id);
}
else
{
for($i = 0; $i<count($files); $i++)
{
if(strpos($files[$i], \'-bpfull.\'))
{
$avatar_full_name = explode(\'/\', $files[$i]);
$url = htmlspecialchars("https://www.example.com/wp-content/uploads/avatars/");
return $url.$user_id.\'/\'.$avatar_full_name[count($avatar_full_name)-1];
}
}
return get_avatar_url($user_id);
}
}