CustomerRewardsRESTAPI/Controller/API/ImageController.php

150 lines
4.6 KiB
PHP
Raw Normal View History

2024-05-09 16:40:42 -05:00
<?php
header("Content-Type: application/json");
/**
* Description of ImageController
*
* @author SCTN4
*/
class ImageController extends BaseController {
public $imageModel;
2024-05-09 16:40:42 -05:00
public $action;
function __construct() {
parent::__construct();
$this->basename = "ImageController";
2024-05-09 16:40:42 -05:00
}
/*
* http://localhost/CustomerRewardsRESTAPI/index.php/image/process/upload
*/
public function processAction() {
$this->strErrorDesc = '';
$this->strErrorHeader = '';
try {
$this->requestMethod = $this->getServerRequestMethod();
2024-05-09 16:40:42 -05:00
switch($this->action) {
case "upload":
$response = $this->uploadImage();
break;
case "update":
$response = $this->updateImage();
break;
case "delete":
$response = $this->deleteImage();
break;
default:
$response = (object) ['Result' => 'Default'];
$this->strErrorDesc = 'Controller Method not supported for processAction: ' . $this->action;
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
break;
}
$responseData = json_encode($response);
} catch (Error $e) {
$this->strErrorDesc = $e->getMessage().' Something went wrong in processAction! Please contact support.';
$this->strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
}
// send output
if (!$this->strErrorDesc) {
$this->sendOutput(
$responseData,
array('Content-Type: application/json', 'HTTP/1.1 200 OK')
);
} else {
$this->sendOutput(json_encode(array('error' => $this->strErrorDesc)),
array('Content-Type: application/json', $this->strErrorHeader)
);
}
}
private function uploadImage(){
2024-05-10 21:37:50 -05:00
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");
header("Content-Type: application/json");
2024-05-09 16:40:42 -05:00
if ($this->checkRequestType('POST') == 'false') {
$this->strErrorDesc = 'Request Method not supported for processAction';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
return;
}
2024-05-13 23:12:50 -05:00
$this->imageModel = new ImageModel();
2024-05-09 16:40:42 -05:00
// reads the raw POST data and returns it as a string.
//base64_decode($_POST['content']);
2024-05-10 21:37:50 -05:00
$imagePayload = file_get_contents($_FILES['your_image_name']['tmp_name']);
if (! $this->validateImage($imagePayload)) {
2024-05-09 16:40:42 -05:00
return $this->unprocessableEntityResponse();
}
2024-05-09 20:25:56 -05:00
$response = $this->insertImage();
2024-05-09 16:40:42 -05:00
return $response;
}
2024-05-09 20:25:56 -05:00
private function insertImage() {
if (isset($_FILES['image'])) {
$uploadImage = $this->processImage();
$success = false;
if (!$uploadImage['error']) {
}
}
}
2024-05-09 16:40:42 -05:00
private function processImage() {
$error = false;
$msg = null;
$uploadTo = "public/images/";
$allowFileType = array('jpg','png','jpeg');
$fileName = $_FILES['image']['name'];
$tempPath = $_FILES["image"]["tmp_name"];
$basename = basename($fileName);
$originalPath = $uploadTo.$basename;
$fileType = pathinfo($originalPath, PATHINFO_EXTENSION);
if(!empty($fileName)){
if(in_array($fileType, $allowFileType)){
if(!move_uploaded_file($tempPath, $originalPath)){
$msg = 'Image Not uploaded ! try again';
$error = true;
}
} else {
$msg = 'Image type is not allowed';
$error = true;
}
} else {
$msg = 'Image is required';
$error = true;
}
$imageInfo = [
"error" => $error,
"msg" => $msg,
"filename" => $fileName,
"filepath" => $originalPath
];
return $imageInfo;
}
2024-05-10 21:37:50 -05:00
private function validateImage($imageData){
$validtion = false;
if($imageData['customer_name_first'] != null){
$validtion = true;
if($imageData['customer_name_last'] == null) {
$validtion = false;
}
}
return $validtion;
}
2024-05-09 16:40:42 -05:00
}