Create save_post_image function.

This commit is contained in:
Pierre 2018-01-31 06:29:04 +01:00
parent b1dd002942
commit 6f792886e4
2 changed files with 32 additions and 13 deletions

View File

@ -154,21 +154,13 @@ class postsController {
if(!check_post_file("image"))
Rest_fatal_error(400, "An error occured while receiving image !");
//Generate target file name
$target_userdata_folder = prepareFileCreation(userID, "imgpost");
$target_file_path = $target_userdata_folder.generateNewFileName(path_user_data($target_userdata_folder, true), "png");
$target_file_sys_path = path_user_data($target_file_path, true);
//Try to resize, convert image and put it in its new location
if(!reduce_image($_FILES['image']["tmp_name"], $target_file_sys_path, 2000, 2000, "image/png")){
//Returns error
Rest_fatal_error(500, "Couldn't resize sent image !");
}
//Save post image
$file_path = save_post_image("image", userID, "imgpost", 2000, 2000);
//Save image information
$file_type = "image/png";
$file_size = filesize($target_file_sys_path);
$file_path = $target_file_path;
$file_sys_path = path_user_data($file_path, true);
$file_type = mime_content_type($file_sys_path);
$file_size = filesize($file_sys_path);
}
//For YouTube posts

View File

@ -418,3 +418,30 @@ function getPostContent($name){
return $content;
}
/**
* Save an image in user data directory from a POST request
*
* @param string $fieldName The name of the POST field
* @param int $userID The ID of the user making the request
* @param string $folder The target folder in the user data directory
* @param int $maxw The maximum width of the image
* @param int $maxh The maximum height of the image
* @return string The path of the image (quit the script in case of failure)
*/
function save_post_image(string $fieldName, int $userID, string $folder, int $maxw, int $maxh) : string {
$target_userdata_folder = prepareFileCreation($userID, $folder);
$target_file_path = $target_userdata_folder.generateNewFileName(path_user_data($target_userdata_folder, true), "png");
$target_file_sys_path = path_user_data($target_file_path, true);
//Try to resize, convert image and put it in its new location
if(!reduce_image($_FILES[$fieldName]["tmp_name"], $target_file_sys_path, $maxw, $maxh, "image/png")){
//Returns error
Rest_fatal_error(500, "Couldn't resize sent image !");
}
//Return image path
return $target_file_path;
}