modified logic in ocr upload button

This commit is contained in:
dhanabalan
2025-10-23 16:05:12 +05:30
parent a983d116cf
commit 5af67e2ae9

View File

@@ -80,195 +80,227 @@ class OcrValidationResource extends Resource
// 'x-ref' => 'hiddenInput', // so Alpine can target it // 'x-ref' => 'hiddenInput', // so Alpine can target it
// ]), // ]),
Forms\Components\TextInput::make('camera_capture_file') // Forms\Components\TextInput::make('camera_capture_file')
->label('Captured Photo') // ->label('Captured Photo')
->reactive() // ->reactive()
->hidden() // ->hidden()
->extraAttributes([ // ->extraAttributes([
'x-ref' => 'hiddenInput', // so Alpine can reference it // 'x-ref' => 'hiddenInput', // so Alpine can reference it
]), // ]),
Forms\Components\Hidden::make('camera_capture_file')
->label('Captured File')
->default(null)
->reactive(),
Forms\Components\Actions::make([ Forms\Components\Actions::make([
Action::make('uploadNow1') // Action::make('uploadNow1')
->label('Upload OCR') // ->label('Upload OCR')
->action(function ($get, callable $set) { // ->action(function ($get, callable $set) {
$uploadedFiles = $get('photo'); // $uploadedFiles = $get('ocrSerials');
if (is_array($uploadedFiles) && count($uploadedFiles) > 0) // if (is_array($uploadedFiles) && count($uploadedFiles) > 0)
{ // {
$uploaded = reset($uploadedFiles); // $uploaded = reset($uploadedFiles);
if ($uploaded instanceof TemporaryUploadedFile) { // if ($uploaded instanceof TemporaryUploadedFile) {
$grNumber = $get('gr_number'); // $grNumber = $get('gr_number');
$safeName = preg_replace('/[^A-Za-z0-9_\-]/', '_', $grNumber); // $safeName = preg_replace('/[^A-Za-z0-9_\-]/', '_', $grNumber);
// $originalName = $uploaded->getClientOriginalName(); // // $originalName = $uploaded->getClientOriginalName();
// $path = 'uploads/GRNumber/' . $originalName; // // $path = 'uploads/GRNumber/' . $originalName;
$finalFileName = $safeName . '.jpg'; // $finalFileName = $safeName . '.jpg';
$finalPath = 'uploads/OCR/' . $finalFileName; // $finalPath = 'uploads/OCR/' . $finalFileName;
$storedPath = $uploaded->storeAs( // $storedPath = $uploaded->storeAs(
'uploads/OCR', // 'uploads/OCR',
$finalFileName, // $finalFileName,
'local' // 'local'
); // );
// $storedPath = $uploaded->storeAs('uploads/OCR', $finalFileName, 'local'); // // $storedPath = $uploaded->storeAs('uploads/OCR', $finalFileName, 'local');
// $fullPath = storage_path('app/' . $storedPath); // // $fullPath = storage_path('app/' . $storedPath);
$storedPath = $uploaded->storeAs('uploads/OCR', $finalFileName, 'local'); // $storedPath = $uploaded->storeAs('uploads/OCR', $finalFileName, 'local');
$fullPath = storage_path('app/private/' . $storedPath); // $fullPath = storage_path('app/private/' . $storedPath);
$text = (new TesseractOCR($fullPath))->lang('eng')->run(); // $text = (new TesseractOCR($fullPath))->lang('eng')->run();
$rawText = $text; // $rawText = $text;
preg_match_all('/\d+/', $rawText, $matches); // preg_match_all('/\d+/', $rawText, $matches);
$serialNumbers = $matches[0]; // $serialNumbers = $matches[0];
$serialNumbers = array_slice($serialNumbers, 0, 4); // $serialNumbers = array_slice($serialNumbers, 0, 4);
//dd($serialNumbers); // //dd($serialNumbers);
$processOrder = $get('gr_number'); // $processOrder = $get('gr_number');
$itemId = $get('item_id'); // $itemId = $get('item_id');
$plant = $get('plant_id'); // $plant = $get('plant_id');
$item = Item::find($itemId); // $item = Item::find($itemId);
$plant = Plant::find($plant); // $plant = Plant::find($plant);
$templatePath = storage_path('app/private/uploads/StickerTemplateOcr/multi.pdf'); // $templatePath = storage_path('app/private/uploads/StickerTemplateOcr/multi.pdf');
$outputPath = storage_path('app/private/uploads/StickerTemplateOcr/multi_filled.pdf'); // $outputPath = storage_path('app/private/uploads/StickerTemplateOcr/multi_filled.pdf');
$storedPath = $uploaded->storeAs( // $storedPath = $uploaded->storeAs(
'uploads/GRNumber', // 'uploads/GRNumber',
$finalFileName, // $finalFileName,
'local' // 'local'
); // );
$pdf = new Fpdi('P', 'mm', [90, 90]); // $pdf = new Fpdi('P', 'mm', [90, 90]);
$templateId = $pdf->setSourceFile($templatePath); // $templateId = $pdf->setSourceFile($templatePath);
$templatePage = $pdf->importPage(1); // $templatePage = $pdf->importPage(1);
$pdf->AddPage(); // $pdf->AddPage();
$pdf->useTemplate($templatePage, 0, 0, 90, 90); // $pdf->useTemplate($templatePage, 0, 0, 90, 90);
$pdf->SetFont('Helvetica', '', 10); // $pdf->SetFont('Helvetica', '', 10);
$pdf->SetTextColor(0, 0, 0); // $pdf->SetTextColor(0, 0, 0);
$slots = [ // $slots = [
['x' => 5.7, 'y' => 41.9, 'w' => 46.5, 'h' => 3.5], // 1st serial // ['x' => 5.7, 'y' => 41.9, 'w' => 46.5, 'h' => 3.5], // 1st serial
['x' => 50, 'y' => 41.5, 'w' => 46.6, 'h' => 3.9], // 2nd serial // ['x' => 50, 'y' => 41.5, 'w' => 46.6, 'h' => 3.9], // 2nd serial
['x' => 5.7, 'y' => 60, 'w' => 46.5, 'h' => 3.5], // 3rd serial // ['x' => 5.7, 'y' => 60, 'w' => 46.5, 'h' => 3.5], // 3rd serial
['x' => 50, 'y' => 60, 'w' => 46.6, 'h' => 3.5], // 4rd serial // ['x' => 50, 'y' => 60, 'w' => 46.6, 'h' => 3.5], // 4rd serial
]; // ];
$qrSlots = [ // $qrSlots = [
['x' => 17.3, 'y' => 29.2, 'size' => 11.4], // ['x' => 17.3, 'y' => 29.2, 'size' => 11.4],
['x' => 61.5, 'y' => 29, 'size' => 11.5], // ['x' => 61.5, 'y' => 29, 'size' => 11.5],
['x' => 17.7, 'y' => 46.7, 'size' => 11.4], // ['x' => 17.7, 'y' => 46.7, 'size' => 11.4],
['x' => 61.7, 'y' => 46.7, 'size' => 11.4], // ['x' => 61.7, 'y' => 46.7, 'size' => 11.4],
]; // ];
foreach ($serialNumbers as $i => $serial) { // foreach ($serialNumbers as $i => $serial) {
if (!isset($slots[$i]) || !isset($qrSlots[$i])) continue; // if (!isset($slots[$i]) || !isset($qrSlots[$i])) continue;
// Erase old QR completely (slightly larger) // // Erase old QR completely (slightly larger)
$pdf->SetFillColor(255, 255, 255); // $pdf->SetFillColor(255, 255, 255);
$pdf->Rect($qrSlots[$i]['x']-1, $qrSlots[$i]['y']-1, $qrSlots[$i]['size']+2, $qrSlots[$i]['size']+2, 'F'); // $pdf->Rect($qrSlots[$i]['x']-1, $qrSlots[$i]['y']-1, $qrSlots[$i]['size']+2, $qrSlots[$i]['size']+2, 'F');
// Generate new QR code // // Generate new QR code
$qrPath = storage_path("app/private/uploads/QR/qr_$serial.png"); // $qrPath = storage_path("app/private/uploads/QR/qr_$serial.png");
$qrDir = storage_path('app/private/uploads/QR'); // $qrDir = storage_path('app/private/uploads/QR');
if (!file_exists($qrDir)) mkdir($qrDir, 0777, true); // if (!file_exists($qrDir)) mkdir($qrDir, 0777, true);
QrCode::format('png')->size(100)->generate($serial, $qrPath); // QrCode::format('png')->size(100)->generate($serial, $qrPath);
// Place QR code // // Place QR code
$pdf->Image($qrPath, $qrSlots[$i]['x'], $qrSlots[$i]['y'], $qrSlots[$i]['size'], $qrSlots[$i]['size']); // $pdf->Image($qrPath, $qrSlots[$i]['x'], $qrSlots[$i]['y'], $qrSlots[$i]['size'], $qrSlots[$i]['size']);
// Erase old serial // // Erase old serial
$pdf->SetFillColor(255, 255, 255); // $pdf->SetFillColor(255, 255, 255);
$pdf->Rect($slots[$i]['x'], $slots[$i]['y'], $slots[$i]['w'], $slots[$i]['h'], 'F'); // $pdf->Rect($slots[$i]['x'], $slots[$i]['y'], $slots[$i]['w'], $slots[$i]['h'], 'F');
// Write new serial // // Write new serial
$pdf->SetXY($slots[$i]['x'], $slots[$i]['y']); // $pdf->SetXY($slots[$i]['x'], $slots[$i]['y']);
$pdf->Cell($slots[$i]['w'], $slots[$i]['h'], $serial, 0, 0, 'L'); // $pdf->Cell($slots[$i]['w'], $slots[$i]['h'], $serial, 0, 0, 'L');
} // }
// Save the final PDF // // Save the final PDF
$pdf->Output('F', $outputPath); // $pdf->Output('F', $outputPath);
// Download // // Download
return response()->download($outputPath); // return response()->download($outputPath);
}
}
else
{
Notification::make()
->title('No file selected to upload')
->warning()
->send();
return;
}
}),
// Action::make('verifyOcr')
// ->label('Verify OCR')
// ->action(function ($get) {
// $base64Image = $get('camera_capture_file');
// // $fileName = 'capture_1760680702.png';
// // //dd($base64Image);
// if (!$base64Image) {
// Notification::make()
// ->title('No captured image found')
// ->warning()
// ->send();
// return;
// } // }
// }
// // Decode and store the base64 image // else
// $image = str_replace('data:image/jpeg;base64,', '', $base64Image); // {
// $image = str_replace(' ', '+', $image);
// $grNumber = $get('gr_number') ?: 'Unknown';
// $safeName = preg_replace('/[^A-Za-z0-9_\-]/', '_', $grNumber);
// $fileName = $safeName . '.jpg';
// $filePath = storage_path("app/private/uploads/OCR/{$fileName}");
// if (!file_exists(dirname($filePath))) {
// mkdir(dirname($filePath), 0777, true);
// }
// file_put_contents($filePath, base64_decode($image));
// // 🧠 Run OCR
// $text = (new TesseractOCR($filePath))
// ->lang('eng')
// ->run();
// preg_match_all('/\d+/', $text, $matches);
// $serialNumbers = array_slice($matches[0], 0, 4);
// if (empty($serialNumbers)) {
// Notification::make()
// ->title('No numbers found in image')
// ->danger()
// ->send();
// return;
// }
// Notification::make() // Notification::make()
// ->title('OCR Read Successfully') // ->title('No file selected to upload')
// ->body('Serial Numbers: ' . implode(', ', $serialNumbers)) // ->warning()
// ->success()
// ->send(); // ->send();
// return;
// }
// }),
// }), Action::make('uploadNow1')
->label('Upload OCR')
->action(function ($get, callable $set) {
$serialNumbers = $get('ocrSerials'); // Get serials from Livewire state
if (empty($serialNumbers) || !is_array($serialNumbers)) {
$this->notify('danger', 'No serial numbers found!');
return;
}
$grNumber = $get('gr_number');
$itemId = $get('item_id');
$plantId = $get('plant_id');
$safeName = preg_replace('/[^A-Za-z0-9_\-]/', '_', $grNumber);
$finalFileName = $safeName . '.pdf';
$outputPath = storage_path('app/private/uploads/StickerTemplateOcr/' . $finalFileName);
$item = Item::find($itemId);
$plant = Plant::find($plantId);
$templatePath = storage_path('app/private/uploads/StickerTemplateOcr/multi.pdf');
$pdf = new Fpdi('P', 'mm', [90, 90]);
$templateId = $pdf->setSourceFile($templatePath);
$templatePage = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($templatePage, 0, 0, 90, 90);
$pdf->SetFont('Helvetica', '', 10);
$pdf->SetTextColor(0, 0, 0);
// PDF slots for serials
$slots = [
['x' => 5.7, 'y' => 41.9, 'w' => 46.5, 'h' => 3.5],
['x' => 50, 'y' => 41.5, 'w' => 46.6, 'h' => 3.9],
['x' => 5.7, 'y' => 60, 'w' => 46.5, 'h' => 3.5],
['x' => 50, 'y' => 60, 'w' => 46.6, 'h' => 3.5],
];
// QR code positions
$qrSlots = [
['x' => 17.3, 'y' => 29.2, 'size' => 11.4],
['x' => 61.5, 'y' => 29, 'size' => 11.5],
['x' => 17.7, 'y' => 46.7, 'size' => 11.4],
['x' => 61.7, 'y' => 46.7, 'size' => 11.4],
];
foreach ($serialNumbers as $i => $serial) {
if (!isset($slots[$i]) || !isset($qrSlots[$i])) continue;
// Erase old QR
$pdf->SetFillColor(255, 255, 255);
$pdf->Rect($qrSlots[$i]['x']-1, $qrSlots[$i]['y']-1, $qrSlots[$i]['size']+2, $qrSlots[$i]['size']+2, 'F');
// Generate new QR code
$qrPath = storage_path("app/private/uploads/QR/qr_$serial.png");
$qrDir = storage_path('app/private/uploads/QR');
if (!file_exists($qrDir)) mkdir($qrDir, 0777, true);
QrCode::format('png')->size(100)->generate($serial, $qrPath);
// Place QR code in PDF
$pdf->Image($qrPath, $qrSlots[$i]['x'], $qrSlots[$i]['y'], $qrSlots[$i]['size'], $qrSlots[$i]['size']);
// Erase old serial text
$pdf->SetFillColor(255, 255, 255);
$pdf->Rect($slots[$i]['x'], $slots[$i]['y'], $slots[$i]['w'], $slots[$i]['h'], 'F');
// Write new serial
$pdf->SetXY($slots[$i]['x'], $slots[$i]['y']);
$pdf->Cell($slots[$i]['w'], $slots[$i]['h'], $serial, 0, 0, 'L');
}
// Save the PDF
$pdf->Output('F', $outputPath);
// Download
return response()->download($outputPath);
}),
Action::make('downloadAttachment') Action::make('downloadAttachment')
->label('Download PDF') ->label('Download PDF')