fipamo/brain/api/v1/ImagesAPI.php
Are0h 63eaba08e2 Added config for PHP formatting (PSR2)
I needed some consistent php formatting, so I plugged in a php fixer
config and then reformatted all PHP files so it's all consistent.

Fixed an ID issue with the page-edit template that was causing page
editing to fail.
2022-05-16 17:41:15 -07:00

79 lines
2.5 KiB
PHP

<?php
namespace brain\api\v1;
use brain\data\Member;
use brain\data\Settings;
use brain\utility\FileUploader;
class ImagesAPI
{
public function __construct()
{
}
public static function uploadImage($request, $type = null)
{
$file = $request->getUploadedFiles();
$uploadPath = '';
$path = date('Y') . '/' . date('m');
$response = [];
switch ($type) {
case 'avatar':
$image = $file['avatar_upload'];
$uploadPath = '../public/assets/images/user/' . $path;
break;
case 'background':
$image = $file['background_upload'];
$uploadPath = '../public/assets/images/user/' . $path;
break;
default:
$image = $file['post_image'];
$path = date('Y') . '/' . date('m');
$uploadPath = '../public/assets/images/blog/' . $path;
break;
}
$result = FileUploader::uploadFile($uploadPath, $image);
switch ($type) {
case 'avatar':
$response = [
'message' => 'Avatar Added. You look great!',
'type' => 'avatarUploaded',
'url' => '/assets/images/user/' . $path . '/' . $image->getClientFileName(),
];
//update member data
Member::updateData(
'avi',
'/assets/images/user/' . $path . '/' . $image->getClientFileName()
);
break;
case 'background':
$response = [
'message' => "Background plugged in. That's nice!",
'type' => 'siteBackgroundUploaded',
'url' => '/assets/images/user/' . $path . '/' . $image->getClientFileName(),
];
//update settings file
Settings::updateGlobalData(
'background',
'/assets/images/user/' . $path . '/' . $image->getClientFileName()
);
break;
default:
$response = [
'message' => 'Image Added. Very slick',
'type' => 'postImageAdded',
'url' => '/assets/images/blog/' . $path . '/' . $image->getClientFileName(),
];
break;
}
return $response;
}
}