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