mirror of
https://github.com/sctn4elk/CustomerRewardsRESTAPI.git
synced 2025-01-09 23:34:30 -06:00
149 lines
4.6 KiB
PHP
149 lines
4.6 KiB
PHP
<?php
|
|
header("Content-Type: application/json");
|
|
|
|
/**
|
|
* Description of ImageController
|
|
*
|
|
* @author SCTN4
|
|
*/
|
|
class ImageController extends BaseController {
|
|
|
|
public $imageModel;
|
|
public $action;
|
|
|
|
#[\Override]
|
|
function __construct() {
|
|
parent::__construct();
|
|
$this->basename = "ImageController";
|
|
}
|
|
|
|
/*
|
|
* http://localhost/CustomerRewardsRESTAPI/index.php/image/process/upload
|
|
*/
|
|
public function processAction() {
|
|
$this->strErrorDesc = '';
|
|
$this->strErrorHeader = '';
|
|
try {
|
|
$this->imageModel = new ImageModel();
|
|
$this->requestMethod = $this->getServerRequestMethod();
|
|
|
|
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(){
|
|
header("Access-Control-Allow-Origin: *");
|
|
header("Access-Control-Allow-Methods: POST");
|
|
header("Content-Type: application/json");
|
|
|
|
if ($this->checkRequestType('POST') == 'false') {
|
|
$this->strErrorDesc = 'Request Method not supported for processAction';
|
|
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
|
|
return;
|
|
}
|
|
// reads the raw POST data and returns it as a string.
|
|
//base64_decode($_POST['content']);
|
|
$imagePayload = file_get_contents($_FILES['your_image_name']['tmp_name']);
|
|
if (! $this->validateImage($imagePayload)) {
|
|
return $this->unprocessableEntityResponse();
|
|
}
|
|
|
|
$response = $this->insertImage();
|
|
return $response;
|
|
}
|
|
|
|
private function insertImage() {
|
|
if (isset($_FILES['image'])) {
|
|
$uploadImage = $this->processImage();
|
|
$success = false;
|
|
|
|
if (!$uploadImage['error']) {
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
private function validateImage($imageData){
|
|
$validtion = false;
|
|
if($imageData['customer_name_first'] != null){
|
|
$validtion = true;
|
|
if($imageData['customer_name_last'] == null) {
|
|
$validtion = false;
|
|
}
|
|
}
|
|
return $validtion;
|
|
}
|
|
}
|