Files
BE_IBL/application/controllers/mockup/ktpscanner/Receive.php
2026-04-15 15:24:12 +07:00

197 lines
6.4 KiB
PHP

<?php
class Receive extends MY_Controller
{
var $db_onedev;
public function index()
{
echo "Dummy API Untuk Menerima Hasil Scan KTP QR1000";
}
public function __construct()
{
parent::__construct();
$this->db_onedev = $this->load->database("onedev", true);
}
public function receive()
{
try {
// Receive all payload from POST request json
$data = json_decode(file_get_contents('php://input'), true);
if (empty($data)) {
$this->sys_error("Tidak ada data yang diterima");
exit;
}
$this->db_onedev->trans_begin();
$now = date('Y-m-d H:i:s');
$sql = "INSERT INTO temp_ocrktp (dariQr1000, createdAt, updatedAt)
VALUES (?, ?, ?)";
$params = [
json_encode($data), // Store complete payload as JSON in dariQr1000
$now, // createdAt
$now // updatedAt
];
$query = $this->db_onedev->query($sql, $params);
if (!$query) {
throw new Exception("Database error: " . json_encode($this->db_onedev->error()));
}
if ($this->db_onedev->trans_status() === FALSE) {
$this->db_onedev->trans_rollback();
throw new Exception("Transaction failed");
}
$this->db_onedev->trans_commit();
$this->sys_ok("Berhasil menerima data");
} catch (Exception $e) {
$this->db_onedev->trans_rollback();
$this->sys_error($e->getMessage());
exit;
}
}
public function to_qwen() {
try {
// Validate input
$prm = $this->sys_input;
$fileDataBase64 = $prm['fileDataBase64'] ?? null;
if (empty($fileDataBase64)) {
throw new Exception("No image data received");
}
// Start database transaction
$this->db_onedev->trans_begin();
// Store base64 to temp_ocrktp.base64img
$sql = "UPDATE temp_ocrktp
SET base64img = ?
WHERE id = (
SELECT id FROM (
SELECT MAX(id) as id
FROM temp_ocrktp
) as latest
)";
$params = [
$fileDataBase64
];
$query = $this->db_onedev->query($sql, $params);
if (!$query) {
throw new Exception("Database error: " . json_encode($this->db_onedev->error()));
}
// Validate base64 format
if (!preg_match('/^data:image\/(\w+);base64,/', $fileDataBase64)) {
throw new Exception("Invalid image format");
}
// Clean base64 string
$fileDataBase64 = preg_replace('/^data:image\/(\w+);base64,/', '', $fileDataBase64);
$file = base64_decode($fileDataBase64);
if ($file === false) {
throw new Exception("Failed to decode base64 image");
}
// Store base64 to temp_ocrktp.base64img
$sql = "UPDATE temp_ocrktp
SET base64imgCleaned = ?
WHERE id = (
SELECT id FROM (
SELECT MAX(id) as id
FROM temp_ocrktp
) as latest
)";
$params = [
$fileDataBase64
];
$query = $this->db_onedev->query($sql, $params);
if (!$query) {
throw new Exception("Database error: " . json_encode($this->db_onedev->error()));
}
$uploadDir = "/home/one/project/one/one-media/scan-ktp/";
// Generate filename and save image
$date = date('YmdHis');
$newFilename = "ktp-" . $date . ".jpg";
$filePath = $uploadDir . $newFilename;
if (file_put_contents($filePath, $file) === false) {
throw new Exception("Failed to save image file");
}
// Perform OCR
$this->load->library('Ocr_oroute');
$this->ocr_oroute->modelName = "qwen/qwen-vl-plus";
$image_path = $uploadDir . $newFilename;
$result = $this->ocr_oroute->extract_ocr($image_path);
$data = $result['data'] ?? null;
if (empty($result)) {
throw new Exception("OCR process failed or returned empty result");
}
$now = date('Y-m-d H:i:s');
// Update the latest record with OCR results
$sql = "UPDATE temp_ocrktp
SET dariQwen = ?,
updatedAt = ?
WHERE id = (
SELECT id FROM (
SELECT MAX(id) as id
FROM temp_ocrktp
) as latest
)";
$params = [
json_encode($data), // Store OCR result as JSON
$now
];
$query = $this->db_onedev->query($sql, $params);
if (!$query) {
throw new Exception("Database error: " . json_encode($this->db_onedev->error()));
}
if ($this->db_onedev->affected_rows() === 0) {
throw new Exception("No records found to update");
}
if ($this->db_onedev->trans_status() === FALSE) {
throw new Exception("Transaction failed");
}
$this->db_onedev->trans_commit();
// Return success response with OCR results
$this->sys_ok($result);
} catch (Exception $e) {
// Rollback transaction if active
if ($this->db_onedev->trans_status() !== NULL) {
$this->db_onedev->trans_rollback();
}
// Delete uploaded file if it exists
if (isset($filePath) && file_exists($filePath)) {
unlink($filePath);
}
$this->sys_error($e->getMessage());
exit;
}
}
}