I’m currently extending the wp rest API to centralize everything and be able to move from an webapp to a real app (I litteraly only miss the Update and delete of the avatar + social login to complete the API)
I’ve been through a lot and learned many things through this job, but I’ve just passed a day without succeeding in uploading and avatar through the API, thus making no progress
I get a “true” value from this : bp_core_avatar_handle_upload($_FILES['avatar'],'xprofile_avatar_upload_dir');
but nothing else happens (not even an upload)
I’m still trying to find my way through, but I’d like an helping hand on this one
Thx
EDIT: Since I couldn't make it work and the avatar is picked based on the last update of a '-bp-full' and '-bp-thumb' file, I'll just put the file in the /uploads/avatars/{id}/
Now I'm trying to understand how to resize to make the thumbnail My current code looks like this
$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';
}
Am I doing something wrong ? Is there some best practice I should follow ? Thx :)
In case anyone needs to do the same, you need to know 2 things:
So if you don't get how to user the bp_core_avatar_handle_upload function, here's my work around : (my script is supposed to receive only squared image as shown by 'if($file_meta[0] == $file_meta[1])', take it out if you need )
$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';
}