resizeToFit static method

ImageData resizeToFit(
  1. ImageData image,
  2. PrinterModel printer,
  3. Size labelSize
)

Implementation

static ImageData resizeToFit(
  ImageData image,
  PrinterModel printer,
  Size labelSize,
) {
  final imgRatio = image.width / image.height;
  final maxHeight = labelSize.height;
  final maxWidth =
      labelSize.width.isInfinite
          ? labelSize.height * imgRatio
          : labelSize.width;

  // resize image to fit label size
  final overflowX = maxWidth / (image.width / printer.dpmm);
  final overflowY = maxHeight / (image.height / printer.dpmm);

  final scaleFactor = overflowX < overflowY ? overflowX : overflowY;
  //if (scaleFactor == 1) return image; // no need to resize

  //throw "${scaleFactor}, ${image.width * scaleFactor}, ${image.height * scaleFactor}";

  return img.copyResize(
    image,
    width: (image.width * scaleFactor).round(),
    height: (image.height * scaleFactor).round(),
  );
}