Files
html/api/ambre-tool-ocr.php
2026-04-22 00:15:04 +02:00

37 lines
1.2 KiB
PHP

<?php
header("Content-Type: application/json; charset=utf-8");
$in = json_decode(file_get_contents("php://input"), true) ?: $_POST ?: $_GET;
$url = trim($in["url"] ?? $in["image_url"] ?? "");
if (!$url) { echo json_encode(["error"=>"image url required"]); exit; }
$t0 = microtime(true);
// Download image temporarily
$tmp = tempnam(sys_get_temp_dir(), "wevia_ocr_") . ".png";
$ctx = stream_context_create(["http"=>["timeout"=>20]]);
$img = @file_get_contents($url, false, $ctx);
if (!$img || strlen($img) < 100) { echo json_encode(["error"=>"image fetch failed"]); exit; }
file_put_contents($tmp, $img);
// Try tesseract OCR
$tess = @shell_exec("which tesseract 2>/dev/null");
$text = "";
if (trim($tess)) {
$cmd = "tesseract " . escapeshellarg($tmp) . " - -l fra+eng 2>/dev/null";
$text = @shell_exec($cmd);
}
@unlink($tmp);
if (!trim($text)) {
echo json_encode(["error"=>"OCR extraction failed, text too short or tesseract missing", "image_url"=>$url]);
exit;
}
echo json_encode([
"success"=>true, "image_url"=>$url,
"text"=>trim($text),
"char_count"=>strlen(trim($text)),
"elapsed_ms"=>round((microtime(true)-$t0)*1000),
"provider"=>"WEVIA OCR",
]);