diff --git a/lib/core/utils/image_utils.dart b/lib/core/utils/image_utils.dart index 445d065..1568dd8 100644 --- a/lib/core/utils/image_utils.dart +++ b/lib/core/utils/image_utils.dart @@ -75,4 +75,75 @@ class ImageUtils { return bytes; } } + + /// Ottimizza e comprime un'immagine per l'esportazione in PDF (es. portali TIM). + /// Normalizza l'orientamento EXIF, ridimensiona proporzionalmente se supera [maxDimension] + /// e codifica in JPEG con qualità [quality] per ridurre drasticamente il peso del PDF. + static Uint8List compressForPdf( + Uint8List bytes, { + int maxDimension = 1800, + int quality = 78, + }) { + try { + final decoded = img.decodeImage(bytes); + if (decoded == null) return bytes; + + var processed = decoded; + if (processed.exif.imageIfd.hasOrientation && + processed.exif.imageIfd.orientation != 1) { + processed = img.bakeOrientation(processed); + } + + if (processed.width > maxDimension || processed.height > maxDimension) { + processed = _resizeToMaxDimension(processed, maxDimension); + } + + return Uint8List.fromList(img.encodeJpg(processed, quality: quality)); + } catch (_) { + return bytes; + } + } + + /// Converte i pixel grezzi BGRA8888 (es. renderizzati da pdfrx) in un JPEG compresso + /// pronto per l'inserimento nel PDF, evitando il passaggio per PNG lossless. + static Uint8List encodeBgraPixelsToJpg( + Uint8List bgraPixels, { + required int width, + required int height, + int maxDimension = 1800, + int quality = 78, + }) { + var image = img.Image.fromBytes( + width: width, + height: height, + bytes: bgraPixels.buffer, + numChannels: 4, + order: img.ChannelOrder.bgra, + ); + + if (image.width > maxDimension || image.height > maxDimension) { + image = _resizeToMaxDimension(image, maxDimension); + } + + return Uint8List.fromList(img.encodeJpg(image, quality: quality)); + } + + static img.Image _resizeToMaxDimension(img.Image source, int maxDimension) { + final aspectRatio = source.width / source.height; + final int targetWidth; + final int targetHeight; + if (source.width >= source.height) { + targetWidth = maxDimension; + targetHeight = (maxDimension / aspectRatio).round(); + } else { + targetHeight = maxDimension; + targetWidth = (maxDimension * aspectRatio).round(); + } + return img.copyResize( + source, + width: targetWidth, + height: targetHeight, + interpolation: img.Interpolation.linear, + ); + } } diff --git a/lib/core/widgets/shared_forms/attachments_section.dart b/lib/core/widgets/shared_forms/attachments_section.dart index e9ef0fe..152b875 100644 --- a/lib/core/widgets/shared_forms/attachments_section.dart +++ b/lib/core/widgets/shared_forms/attachments_section.dart @@ -563,29 +563,26 @@ class _SharedAttachmentsSectionState extends State { sourceName: file.name, ); for (final page in document.pages) { + final maxSide = page.width > page.height ? page.width : page.height; + final scale = (maxSide * 2 > 1800) ? (1800 / maxSide) : 2.0; final pageImage = await page.render( - fullWidth: page.width * 2, - fullHeight: page.height * 2, + fullWidth: (page.width * scale).roundToDouble(), + fullHeight: (page.height * scale).roundToDouble(), ); if (pageImage != null) { - final uiImage = await pageImage.createImage(); - final byteData = await uiImage.toByteData( - format: ui.ImageByteFormat.png, + final jpgBytes = ImageUtils.encodeBgraPixelsToJpg( + pageImage.pixels, + width: pageImage.width, + height: pageImage.height, ); - if (byteData != null) { - allPagesAsImages.add(byteData.buffer.asUint8List()); - } - uiImage.dispose(); + allPagesAsImages.add(jpgBytes); pageImage.dispose(); } } await document.dispose(); } else { - // È un'immagine: normalizziamo l'orientamento EXIF per il PDF - final safeBytes = ImageUtils.normalizeOrientation( - fileBytes, - extension: file.extension, - ); + // È un'immagine: normalizziamo orientamento EXIF, ridimensioniamo e comprimiamo in JPEG per il PDF + final safeBytes = ImageUtils.compressForPdf(fileBytes); allPagesAsImages.add(safeBytes); } } @@ -714,38 +711,35 @@ class _SharedAttachmentsSectionState extends State { document.pages.length > 1; // Controlliamo se è multipagina! for (final page in document.pages) { + final maxSide = page.width > page.height ? page.width : page.height; + final scale = (maxSide * 2 > 1800) ? (1800 / maxSide) : 2.0; final pageImage = await page.render( - fullWidth: page.width * 2, - fullHeight: page.height * 2, + fullWidth: (page.width * scale).roundToDouble(), + fullHeight: (page.height * scale).roundToDouble(), ); if (pageImage != null) { - final uiImage = await pageImage.createImage(); - final byteData = await uiImage.toByteData( - format: ui.ImageByteFormat.png, + final jpgBytes = ImageUtils.encodeBgraPixelsToJpg( + pageImage.pixels, + width: pageImage.width, + height: pageImage.height, + ); + // Salviamo l'immagine compressa CON il suo contesto storico + itemsToExport.add( + _ExportItem( + bytes: jpgBytes, + sourceName: baseName, + isMultiPage: isMulti, + pageIndex: page.pageNumber, + ), ); - if (byteData != null) { - // Salviamo l'immagine CON il suo contesto storico - itemsToExport.add( - _ExportItem( - bytes: byteData.buffer.asUint8List(), - sourceName: baseName, - isMultiPage: isMulti, - pageIndex: page.pageNumber, - ), - ); - } - uiImage.dispose(); pageImage.dispose(); } } await document.dispose(); } else { - // SE È UN'IMMAGINE, la salviamo normalizzando l'orientamento - final safeBytes = ImageUtils.normalizeOrientation( - fileBytes, - extension: file.extension, - ); + // SE È UN'IMMAGINE, normalizziamo orientamento EXIF, ridimensioniamo e comprimiamo in JPEG + final safeBytes = ImageUtils.compressForPdf(fileBytes); itemsToExport.add( _ExportItem( bytes: safeBytes, diff --git a/pubspec.yaml b/pubspec.yaml index 92130a1..3b11114 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: flux description: "Gestione attività negozio di telefonia" publish_to: 'none' -version: 2.5.3+87 +version: 2.5.4+88 environment: sdk: ^3.11.3