OcrController.php 4.16 KB
<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Exception;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log;

class OcrController extends Controller
{
    public function index(Request $request){
        return view('dashboard',['ocr'=>[]]);
    }
    public function show_pdf(Request $request)
    {
        return response()->file($request->file_path);
    }
    public function preview_pdf(Request $request){
        return view('ocr.ocr_pdf', ['disable'=>$request->disable??0]);
    }
    public function import_pdf(Request $request){
        try {
            [$image_path,$js_path] = $this->ocr_process($request);
            $json_data = json_decode(file_get_contents($js_path), true);
            $ocr=[];
            $ocr['json_data']=$json_data;
            $ocr['js_path'] = $js_path;
            $ocr['image_path'] = $image_path;
            $ocr['mst_template'] = ['image'=>$json_data['image']];
            $ocr['dt_template_field'] = [];
            return response()->json(['status' => true,'data'   => $ocr,]);
        } catch (\Throwable $e){
            report($e);
            return response()->json(['status' => false]);
        }
    }
    public function ocr_process($request){
        set_time_limit(0);
        ini_set('max_execution_time', 0);
        $ocr_serve_path=env('OCR_SERVER_PATH','');
        $ocr_storage='ocr_results';
        if($ocr_serve_path) $ocr_storage='ocr_results_sftp';

        $now=Carbon::now()->format('YmdHis');
        umask(0002);
        Storage::disk($ocr_storage)->makeDirectory($now);
        $input_file_name=$request->file('attached_file')->getClientOriginalName();
        Log::info('upload ocr '.$input_file_name);
        Storage::disk($ocr_storage)->putFileAs($now, $request->file('attached_file'),$input_file_name);
        $file_path=Storage::disk($ocr_storage)->path($now.DIRECTORY_SEPARATOR.$input_file_name);
        $ch = curl_init();
        Storage::disk('ocr_results')->makeDirectory($now);
        chmod(Storage::disk('ocr_results')->path($now), 0777);
        $params = [
            'file_path' => $file_path,
        ];
        foreach (($request->option ?? []) as $key => $value) {
            $params[$key] = $value;
        }
        $url = env('PYTHON_SERVICE_ENDPOINT', "http://127.0.0.1:8000/"). '?'. http_build_query($params);

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        if(curl_errno($ch)){
            throw new Exception('Curl error: ' . curl_error($ch));
        }
        curl_close($ch);
        $data = json_decode($response, true);
        if (empty($data['success'])){
            throw new Exception($data['message']);
        }

        if($ocr_serve_path){
            $files = Storage::disk('ocr_results_sftp')->files($now);

            foreach ($files as $file) {
                Storage::disk('ocr_results')->put(
                    $file,
                    Storage::disk('ocr_results_sftp')->get($file)
                );
            }
        }


        if (!Storage::disk('ocr_results')->exists($now.DIRECTORY_SEPARATOR.'rs.json'))
            throw new Exception('file rs ocr not found');
        Log::info('read ocr successfully');

        return [Storage::disk('ocr_results')->path($now.DIRECTORY_SEPARATOR.'rs.jpg'), Storage::disk('ocr_results')->path($now.DIRECTORY_SEPARATOR.'rs.json')];
    }
    public function ocr_db(&$ocr){
        $json_data=json_decode(file_get_contents($ocr['js_path']), true);
        $json_data['chars']=[];
        $ocr['json_data']=$json_data;
        if(!empty($ocr['tpl'])){
            if(!empty($ocr['tpl']['tpl_id'])) $ocr['mst_template']=OcrMstTemplate::where('tpl_id', $ocr['tpl']['tpl_id'])->first()?->toArray()??[];
            else $ocr['mst_template']=$ocr['tpl'];
        } 
        
        if(empty($ocr['mst_template'])) $ocr['mst_template']=['tpl_text'=>'','tpl_xy'=>'','tpl_id'=>'','image'=>$json_data['image']];
        $ocr['mst_template_field']=OcrMstTemplateField::get()->keyBy('field_name');
        $ocr['dt_template_field']=!empty($ocr['tpl'])?OcrDtTemplate::where('tpl_id', $ocr['tpl']['tpl_id'])->get():[];
    }
}