CustomerRewardsRESTAPI/Controller/API/ImageController.php

179 lines
6.2 KiB
PHP
Raw Normal View History

2024-05-09 16:40:42 -05:00
<?php
header("Content-Type: multipart/form-data");
2024-05-14 11:38:57 -05:00
header("Access-Control-Allow-Origin: *");
require_once PD . "/Model/ImageModel.php";
2024-05-09 16:40:42 -05:00
/**
* Description of ImageController
*
* @author Mike Howard
2024-05-09 16:40:42 -05:00
*/
class ImageController extends BaseController {
public $imageModel;
2024-05-09 16:40:42 -05:00
public $action;
2024-05-14 11:38:57 -05:00
private $imagePayload;
2024-05-09 16:40:42 -05:00
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-14 11:38:57 -05:00
$this->imageModel = new ImageModel();
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)
);
}
}
2024-05-14 11:38:57 -05:00
/* Upload the image and store on server as file */
2024-05-09 16:40:42 -05:00
private function uploadImage(){
if ($this->checkRequestType('POST') == 'false') {
2024-05-14 11:38:57 -05:00
return $this->unprocessableRequestResponse();
2024-05-09 16:40:42 -05:00
}
2024-05-13 23:12:50 -05:00
2024-05-29 01:59:54 -05:00
/*
* array(1) {
["MemberImageModel"]=>
string(240) "{"CustomerID":1,"ImagePath":"C:\\DEV\\CustomerRewardsAdminPortal\\Resources\\Images\\headshot.jpg","ImageName":"headshot.jpg","ImageType":"image/jpg","ImageBlob":{"Headers":[{"Key":"Content-Type","Value":["image/jpg"]}]},"SourceImage":null}"
}
*/
// reads the raw POST data
$jsonPayload = filter_input_array(INPUT_POST);
$input = json_decode($jsonPayload['MemberImageModel'], TRUE);
//get the member data
2024-05-29 01:59:54 -05:00
$this->imageModel->memberId = $input['CustomerID'];
$this->imageModel->imagePath = $input['ImagePath'];
$this->imageModel->imageName = $input['ImageName'];
$this->imageModel->imageType = $input['ImageType'];
//get the file data
$this->imagePayload = $_FILES['image'];
$this->imageModel->fileName = $this->imagePayload['name'];
2024-05-14 11:38:57 -05:00
if(empty($this->imageModel->fileName)){
return $this->notFoundResponse();
}
$this->imageModel->tempPath = $this->imagePayload['tmp_name'];
2024-05-14 11:38:57 -05:00
$this->imageModel->basename = basename($this->imageModel->fileName);
$this->imageModel->originalPath = $this->imageModel->uploadTo.$this->imageModel->basename;
$this->imageModel->fileType = pathinfo($this->imageModel->originalPath, PATHINFO_EXTENSION);
2024-05-29 01:59:54 -05:00
if (!is_readable($this->imageModel->tempPath)) {
return $this->notFoundResponse();
}
$this->imageModel->imageBlob = base64_encode(file_get_contents($this->imageModel->tempPath));
2024-05-13 23:12:50 -05:00
2024-05-14 11:38:57 -05:00
if (! $this->validateImage()) {
2024-05-09 16:40:42 -05:00
return $this->unprocessableEntityResponse();
}
2024-05-14 11:38:57 -05:00
if (! $this->handleImage()) {
return $this->notFoundResponse();
}
2024-05-29 01:59:54 -05:00
$response = $this->imageModel->insertImage();
2024-05-09 16:40:42 -05:00
return $response;
}
2024-05-14 11:38:57 -05:00
private function handleImage() {
$moved = false;
if(move_uploaded_file($this->imageModel->tempPath, $this->imageModel->uploadTo . $this->imageModel->fileName)) {
2024-05-14 11:38:57 -05:00
$moved = true;
2024-05-09 20:25:56 -05:00
}
2024-05-14 11:38:57 -05:00
return $moved;
2024-05-09 20:25:56 -05:00
}
2024-05-29 01:59:54 -05:00
private function transferImage() {
$src = $this->imageModel->uploadTo;
$dest = "/server/location/upload/" . $this->imageModel->fileName;
$check = file_put_contents($dest, file_get_contents($src));
if($check != false){
$check = true;
}
/* Transfer between web servers
if ( isset($_FILES['uploadedfile']) ) {
$filename = $_FILES['uploadedfile']['tmp_name'];
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
$POST_DATA = array(
'file' => base64_encode($data)
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, '<span style="color: red;">http://extserver.com/handle.php</span>');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);
$response = curl_exec($curl);
curl_close ($curl);
}
*/
/* Separate file running on a different web server
$encoded_file = $_POST['file'];
$decoded_file = base64_decode($encoded_file);
//Now you can copy the uploaded file to your server.
file_put_contents('<span style="color: red;">subins</span>', $decoded_file);
*/
return $check;
}
2024-05-14 11:38:57 -05:00
private function validateImage(){
2024-05-10 21:37:50 -05:00
$validtion = false;
2024-05-14 11:38:57 -05:00
if(in_array($this->imageModel->fileType, $this->imageModel->allowFileType)){
2024-05-10 21:37:50 -05:00
$validtion = true;
}
return $validtion;
}
2024-05-09 16:40:42 -05:00
}