71 lines
1.7 KiB
PHP
71 lines
1.7 KiB
PHP
<?php
|
||
// api_scan_omr.php
|
||
// RS Learning Lab – OMR Scan API (Silent Mode)
|
||
|
||
$pythonPath = "python";
|
||
$scriptPath = __DIR__ . "/omr_read_sheet.py";
|
||
$uploadDir = __DIR__ . "/uploads/";
|
||
|
||
// Create uploads folder
|
||
if (!is_dir($uploadDir)) {
|
||
mkdir($uploadDir, 0777, true);
|
||
}
|
||
|
||
// Student context
|
||
$roll_no = $_GET['roll'] ?? $_POST['roll'] ?? null;
|
||
$student_name = $_GET['name'] ?? $_POST['name'] ?? null;
|
||
|
||
// Validate upload
|
||
if (!isset($_FILES['omr_image'])) {
|
||
return ""; // 🔥 changed
|
||
}
|
||
|
||
$tmpName = $_FILES['omr_image']['tmp_name'];
|
||
$originalName = basename($_FILES['omr_image']['name']);
|
||
$ext = pathinfo($originalName, PATHINFO_EXTENSION);
|
||
|
||
// Allow only images
|
||
$allowed = ['jpg', 'jpeg', 'png'];
|
||
if (!in_array(strtolower($ext), $allowed)) {
|
||
return ""; // 🔥 changed
|
||
}
|
||
|
||
// Save image
|
||
$filename = time() . "_" . uniqid() . "." . $ext;
|
||
$targetPath = $uploadDir . $filename;
|
||
|
||
if (!move_uploaded_file($tmpName, $targetPath)) {
|
||
return ""; // 🔥 changed
|
||
}
|
||
|
||
// Call Python script
|
||
$command = escapeshellcmd("$pythonPath \"$scriptPath\" \"$targetPath\"");
|
||
$raw_output = shell_exec($command);
|
||
|
||
// Clean output
|
||
if (!$raw_output) {
|
||
return ""; // 🔥 changed
|
||
}
|
||
|
||
$raw_output = trim($raw_output);
|
||
$raw_output = str_replace(["\n", "\r", " "], "", $raw_output);
|
||
|
||
$clean_answers = preg_replace("/[^A-D,]/", "", strtoupper($raw_output));
|
||
$clean_answers = rtrim($clean_answers, ",");
|
||
|
||
// Logging (optional)
|
||
$logEntry = [
|
||
'timestamp' => date("Y-m-d H:i:s"),
|
||
'roll_no' => $roll_no,
|
||
'student_name' => $student_name,
|
||
'answers' => $clean_answers
|
||
];
|
||
|
||
file_put_contents(
|
||
__DIR__ . "/omr_log.txt",
|
||
json_encode($logEntry) . PHP_EOL,
|
||
FILE_APPEND
|
||
);
|
||
|
||
// 🔥 FINAL CHANGE: RETURN instead of echo
|
||
return $clean_answers;
|