GWM : megre pdf

This commit is contained in:
Gowtham M 2026-05-11 18:17:21 +05:30
parent 1520db22a7
commit d516a66c94

View File

@ -62,7 +62,6 @@ if (! function_exists('merge_ticket_pdfs')) {
$rows = $claimFiles
->where('ticket_id', $ticket_master_id)
->where('is_active', 1)
->whereIn('mime_type', $opts['include_mime_types'])
->whereIn('file_type', $opts['include_file_types'])
->orderBy('id', 'ASC')
->findAll();
@ -86,9 +85,15 @@ if (! function_exists('merge_ticket_pdfs')) {
// url column may sometimes hold a full URL; we only care about the file basename on disk.
$full = $uploadDir . basename($name);
if (is_file($full) && is_readable($full)) {
$mime = merge_ticket_pdf_resolve_mime($full, (string) ($row['mime_type'] ?? ''));
if (! in_array($mime, $opts['include_mime_types'], true)) {
log_message('error', "merge_ticket_pdfs | unsupported source mime {$mime} | claim_file_id={$row['id']} | path={$full}");
continue;
}
$sourceFiles[] = [
'path' => $full,
'mime' => strtolower((string) ($row['mime_type'] ?? '')),
'mime' => $mime,
'id' => $row['id'] ?? null,
];
} else {
@ -205,6 +210,47 @@ if (! function_exists('merge_ticket_pdfs')) {
}
}
if (! function_exists('merge_ticket_pdf_resolve_mime')) {
/**
* Resolve file type from stored MIME, filesystem MIME, and extension.
* Some uploads can be saved as image/jpg, empty MIME, or octet-stream in DB.
*/
function merge_ticket_pdf_resolve_mime(string $path, string $storedMime = ''): string
{
$storedMime = strtolower(trim($storedMime));
if ($storedMime === 'image/jpg') {
return 'image/jpeg';
}
if (in_array($storedMime, ['application/pdf', 'image/jpeg', 'image/png'], true)) {
return $storedMime;
}
$detectedMime = '';
if (function_exists('mime_content_type')) {
$detectedMime = strtolower((string) @mime_content_type($path));
if ($detectedMime === 'image/jpg') {
return 'image/jpeg';
}
if (in_array($detectedMime, ['application/pdf', 'image/jpeg', 'image/png'], true)) {
return $detectedMime;
}
}
$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
if ($ext === 'pdf') {
return 'application/pdf';
}
if (in_array($ext, ['jpg', 'jpeg'], true)) {
return 'image/jpeg';
}
if ($ext === 'png') {
return 'image/png';
}
return $detectedMime ?: ($storedMime ?: 'application/octet-stream');
}
}
if (! function_exists('merge_ticket_pdf_add_image_page')) {
/**
* Add an uploaded image as a single PDF page, preserving portrait/landscape