buildImage method
Implementation
@override
PdfImage buildImage(Context context, {int? width, int? height}) {
if (width == null) {
return PdfImage.file(context.document, bytes: bytes);
}
final image = im.decodeImage(bytes);
if (image == null) {
throw PdfException('Unable decode the image');
}
// The decoded (orientation-baked) pixels are the ground truth for the
// no-upscale rule: for rotated images the metadata bound checked by
// resolve() cannot know which axis copyResize will scale.
if (width >= image.width) {
return PdfImage.file(context.document, bytes: bytes);
}
final resized = im.copyResize(image, width: width);
if (im.JpegDecoder().isValidFile(bytes)) {
// Do not carry the source metadata over: EXIF can hold sensitive data
// (GPS position, device serial numbers) and its orientation is already
// baked into the decoded pixels.
resized.exif = im.ExifData();
// Keep DCT (JPEG) encoding for resampled JPEG images: embedding the
// raw pixels with Flate compression would inflate the file size.
return PdfImage.jpeg(
context.document,
image: im.encodeJpg(resized, quality: 90),
);
}
return PdfImage.fromImage(context.document, image: resized);
}