Begin work on image processing

This commit is contained in:
sctn4elk 2024-05-09 16:40:42 -05:00
parent f0a4699807
commit 97207ea536
4 changed files with 238 additions and 3 deletions

View File

@ -12,7 +12,7 @@ header("Content-Type: application/json");
* http://localhost:8000/index.php/customer/process/insert?name=Mike%20Howard&email=sctn4elk@msn.com&phone=208-841-4159&birthday=05/07/1965&loyalty=1&city=Winnsboro&state=TX&zip=75494
* http://localhost:8000/index.php/customer/process/select?limit=20
*/
class CustomerController extends BaseController{
class CustomerController extends BaseController {
/**
* "/customer/list" Endpoint - Get list of users
*/
@ -49,7 +49,7 @@ class CustomerController extends BaseController{
case "delete":
$response = $this->deleteCustomer();
break;
default:
$response = (object) ['Result' => 'Default'];
$this->strErrorDesc = 'Controller Method not supported for processAction: ' . $this->action;
@ -182,7 +182,7 @@ class CustomerController extends BaseController{
}
return $response;
}
private function checkRequestType($request)
{
$response = 'false';

View File

@ -0,0 +1,169 @@
<?php
header("Content-Type: application/json");
/**
* Description of ImageController
*
* @author SCTN4
*/
class ImageController extends BaseController {
private $imageModel;
private $requestMethod;
private $arrQueryStringParams;
private $strErrorDesc;
private $strErrorHeader;
public $action;
function __construct() {
$this->imageModel = new ImageModel();
$this->requestMethod = $this->getServerRequestMethod();
}
/*
* http://localhost/CustomerRewardsRESTAPI/index.php/image/process/upload
*/
public function processAction() {
$this->strErrorDesc = '';
$this->strErrorHeader = '';
try {
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(){
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.
$jsonPayload = file_get_contents('php://input');
$input = json_decode($jsonPayload, TRUE);
if (! $this->validatePerson($input)) {
return $this->unprocessableEntityResponse();
}
//remove customer_id field so it doesn't break
unset($input['customer_id']);
$response = $this->customerModel->insertCustomer($input);
return $response;
}
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 insert() {
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");
header("Content-Type: application/json");
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_FILES['image'])) {
$uploadImage = $this->processImage();
$success = false;
if (!$uploadImage['error']) {
// table name for admin profiles
$query = "INSERT INTO " . $this->imageTable;
$query .= " (filename, filepath) VALUES (?,?)";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("ss", $uploadImage['filename'], $uploadImage['filepath']);
if ($stmt->execute()) {
$success = true;
$stmt->close();
}
}
$data = [
'Errormsg' => $uploadImage['msg'] ?? '',
'success' => $success
];
return json_encode($data);
}
}
}
private function notFoundResponse()
{
$response['status_code_header'] = 'HTTP/1.1 404 Entity Not Found';
$response['body'] = null;
$this->strErrorDesc = 'Request Entity Not Found';
$this->strErrorHeader = 'HTTP/1.1 422 Entity Not Found';
return $response;
}
}

61
Model/ImageModel.php Normal file
View File

@ -0,0 +1,61 @@
<?php
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
*/
/**
* Description of ImageModel
*
* @author SCTN4
*/
class ImageModel extends Database {
private $params = array();
/*
* @assert ('name', 'value')
*/
public function __set($name, $value)
{
//echo "Setting '$name' to '$value'\n";
$this->params[$name] = $value;
}
/*
* @assert ('name') == 'value'
*/
public function __get($name)
{
//echo "Getting '$name'\n";
if (array_key_exists($name, $this->params)) {
return $this->params[$name];
}
$trace = debug_backtrace();
trigger_error(
'Undefined property via __get(): ' . $name .
' in ' . $trace[0]['file'] .
' on line ' . $trace[0]['line'],
E_USER_NOTICE);
return null;
}
/*
* @assert ('name') == 'true'
* @assert ('test') == 'false'
*/
public function __isset($name)
{
//echo "Is '$name' set?\n";
return isset($this->params[$name]);
}
public function __unset($name)
{
//echo "Unsetting '$name'\n";
unset($this->params[$name]);
}
}

View File

@ -50,6 +50,11 @@ switch($uri[$uri_pos + 1]) {
$objFeedController = new CustomerController();
break;
case "image":
require PROJECT_ROOT_PATH . "/Controller/Api/ImageController.php";
$objFeedController = new ImageController();
break;
default:
header("HTTP/1.1 404 Module Not Found");
exit();