modified logic in ocr upload button
This commit is contained in:
@@ -80,77 +80,171 @@ class OcrValidationResource extends Resource
|
||||
// 'x-ref' => 'hiddenInput', // so Alpine can target it
|
||||
// ]),
|
||||
|
||||
Forms\Components\TextInput::make('camera_capture_file')
|
||||
->label('Captured Photo')
|
||||
->reactive()
|
||||
->hidden()
|
||||
->extraAttributes([
|
||||
'x-ref' => 'hiddenInput', // so Alpine can reference it
|
||||
]),
|
||||
// Forms\Components\TextInput::make('camera_capture_file')
|
||||
// ->label('Captured Photo')
|
||||
// ->reactive()
|
||||
// ->hidden()
|
||||
// ->extraAttributes([
|
||||
// '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([
|
||||
// Action::make('uploadNow1')
|
||||
// ->label('Upload OCR')
|
||||
// ->action(function ($get, callable $set) {
|
||||
// $uploadedFiles = $get('ocrSerials');
|
||||
|
||||
// if (is_array($uploadedFiles) && count($uploadedFiles) > 0)
|
||||
// {
|
||||
// $uploaded = reset($uploadedFiles);
|
||||
|
||||
// if ($uploaded instanceof TemporaryUploadedFile) {
|
||||
// $grNumber = $get('gr_number');
|
||||
// $safeName = preg_replace('/[^A-Za-z0-9_\-]/', '_', $grNumber);
|
||||
// // $originalName = $uploaded->getClientOriginalName();
|
||||
// // $path = 'uploads/GRNumber/' . $originalName;
|
||||
// $finalFileName = $safeName . '.jpg';
|
||||
// $finalPath = 'uploads/OCR/' . $finalFileName;
|
||||
|
||||
// $storedPath = $uploaded->storeAs(
|
||||
// 'uploads/OCR',
|
||||
// $finalFileName,
|
||||
// 'local'
|
||||
// );
|
||||
|
||||
// // $storedPath = $uploaded->storeAs('uploads/OCR', $finalFileName, 'local');
|
||||
// // $fullPath = storage_path('app/' . $storedPath);
|
||||
// $storedPath = $uploaded->storeAs('uploads/OCR', $finalFileName, 'local');
|
||||
|
||||
// $fullPath = storage_path('app/private/' . $storedPath);
|
||||
|
||||
// $text = (new TesseractOCR($fullPath))->lang('eng')->run();
|
||||
|
||||
// $rawText = $text;
|
||||
|
||||
// preg_match_all('/\d+/', $rawText, $matches);
|
||||
|
||||
// $serialNumbers = $matches[0];
|
||||
|
||||
// $serialNumbers = array_slice($serialNumbers, 0, 4);
|
||||
|
||||
// //dd($serialNumbers);
|
||||
|
||||
// $processOrder = $get('gr_number');
|
||||
|
||||
// $itemId = $get('item_id');
|
||||
|
||||
// $plant = $get('plant_id');
|
||||
|
||||
// $item = Item::find($itemId);
|
||||
|
||||
// $plant = Plant::find($plant);
|
||||
|
||||
// $templatePath = storage_path('app/private/uploads/StickerTemplateOcr/multi.pdf');
|
||||
|
||||
// $outputPath = storage_path('app/private/uploads/StickerTemplateOcr/multi_filled.pdf');
|
||||
|
||||
// $storedPath = $uploaded->storeAs(
|
||||
// 'uploads/GRNumber',
|
||||
// $finalFileName,
|
||||
// 'local'
|
||||
// );
|
||||
|
||||
// $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);
|
||||
|
||||
// $slots = [
|
||||
// ['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' => 5.7, 'y' => 60, 'w' => 46.5, 'h' => 3.5], // 3rd serial
|
||||
// ['x' => 50, 'y' => 60, 'w' => 46.6, 'h' => 3.5], // 4rd serial
|
||||
// ];
|
||||
|
||||
// $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 completely (slightly larger)
|
||||
// $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
|
||||
// $pdf->Image($qrPath, $qrSlots[$i]['x'], $qrSlots[$i]['y'], $qrSlots[$i]['size'], $qrSlots[$i]['size']);
|
||||
|
||||
// // Erase old serial
|
||||
// $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 final PDF
|
||||
// $pdf->Output('F', $outputPath);
|
||||
|
||||
// // Download
|
||||
// return response()->download($outputPath);
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Notification::make()
|
||||
// ->title('No file selected to upload')
|
||||
// ->warning()
|
||||
// ->send();
|
||||
// return;
|
||||
// }
|
||||
// }),
|
||||
|
||||
Action::make('uploadNow1')
|
||||
->label('Upload OCR')
|
||||
->action(function ($get, callable $set) {
|
||||
$uploadedFiles = $get('photo');
|
||||
$serialNumbers = $get('ocrSerials'); // Get serials from Livewire state
|
||||
|
||||
if (is_array($uploadedFiles) && count($uploadedFiles) > 0)
|
||||
{
|
||||
$uploaded = reset($uploadedFiles);
|
||||
if (empty($serialNumbers) || !is_array($serialNumbers)) {
|
||||
$this->notify('danger', 'No serial numbers found!');
|
||||
return;
|
||||
}
|
||||
|
||||
if ($uploaded instanceof TemporaryUploadedFile) {
|
||||
$grNumber = $get('gr_number');
|
||||
$safeName = preg_replace('/[^A-Za-z0-9_\-]/', '_', $grNumber);
|
||||
// $originalName = $uploaded->getClientOriginalName();
|
||||
// $path = 'uploads/GRNumber/' . $originalName;
|
||||
$finalFileName = $safeName . '.jpg';
|
||||
$finalPath = 'uploads/OCR/' . $finalFileName;
|
||||
|
||||
$storedPath = $uploaded->storeAs(
|
||||
'uploads/OCR',
|
||||
$finalFileName,
|
||||
'local'
|
||||
);
|
||||
|
||||
// $storedPath = $uploaded->storeAs('uploads/OCR', $finalFileName, 'local');
|
||||
// $fullPath = storage_path('app/' . $storedPath);
|
||||
$storedPath = $uploaded->storeAs('uploads/OCR', $finalFileName, 'local');
|
||||
|
||||
$fullPath = storage_path('app/private/' . $storedPath);
|
||||
|
||||
$text = (new TesseractOCR($fullPath))->lang('eng')->run();
|
||||
|
||||
$rawText = $text;
|
||||
|
||||
preg_match_all('/\d+/', $rawText, $matches);
|
||||
|
||||
$serialNumbers = $matches[0];
|
||||
|
||||
$serialNumbers = array_slice($serialNumbers, 0, 4);
|
||||
|
||||
//dd($serialNumbers);
|
||||
|
||||
$processOrder = $get('gr_number');
|
||||
|
||||
$itemId = $get('item_id');
|
||||
$plantId = $get('plant_id');
|
||||
|
||||
$plant = $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($plant);
|
||||
$plant = Plant::find($plantId);
|
||||
|
||||
$templatePath = storage_path('app/private/uploads/StickerTemplateOcr/multi.pdf');
|
||||
|
||||
$outputPath = storage_path('app/private/uploads/StickerTemplateOcr/multi_filled.pdf');
|
||||
|
||||
$storedPath = $uploaded->storeAs(
|
||||
'uploads/GRNumber',
|
||||
$finalFileName,
|
||||
'local'
|
||||
);
|
||||
|
||||
$pdf = new Fpdi('P', 'mm', [90, 90]);
|
||||
|
||||
$templateId = $pdf->setSourceFile($templatePath);
|
||||
$templatePage = $pdf->importPage(1);
|
||||
|
||||
@@ -160,13 +254,15 @@ class OcrValidationResource extends Resource
|
||||
$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], // 1st 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' => 50, 'y' => 60, 'w' => 46.6, 'h' => 3.5], // 4rd serial
|
||||
['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],
|
||||
@@ -177,7 +273,7 @@ class OcrValidationResource extends Resource
|
||||
foreach ($serialNumbers as $i => $serial) {
|
||||
if (!isset($slots[$i]) || !isset($qrSlots[$i])) continue;
|
||||
|
||||
// Erase old QR completely (slightly larger)
|
||||
// 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');
|
||||
|
||||
@@ -187,10 +283,10 @@ class OcrValidationResource extends Resource
|
||||
if (!file_exists($qrDir)) mkdir($qrDir, 0777, true);
|
||||
QrCode::format('png')->size(100)->generate($serial, $qrPath);
|
||||
|
||||
// Place QR code
|
||||
// Place QR code in PDF
|
||||
$pdf->Image($qrPath, $qrSlots[$i]['x'], $qrSlots[$i]['y'], $qrSlots[$i]['size'], $qrSlots[$i]['size']);
|
||||
|
||||
// Erase old serial
|
||||
// Erase old serial text
|
||||
$pdf->SetFillColor(255, 255, 255);
|
||||
$pdf->Rect($slots[$i]['x'], $slots[$i]['y'], $slots[$i]['w'], $slots[$i]['h'], 'F');
|
||||
|
||||
@@ -199,77 +295,13 @@ class OcrValidationResource extends Resource
|
||||
$pdf->Cell($slots[$i]['w'], $slots[$i]['h'], $serial, 0, 0, 'L');
|
||||
}
|
||||
|
||||
// Save the final PDF
|
||||
// Save the PDF
|
||||
$pdf->Output('F', $outputPath);
|
||||
|
||||
// Download
|
||||
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
|
||||
// $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()
|
||||
// ->title('OCR Read Successfully')
|
||||
// ->body('Serial Numbers: ' . implode(', ', $serialNumbers))
|
||||
// ->success()
|
||||
// ->send();
|
||||
|
||||
// }),
|
||||
|
||||
Action::make('downloadAttachment')
|
||||
->label('Download PDF')
|
||||
->action(function ($get) {
|
||||
|
||||
Reference in New Issue
Block a user