createPlaceholder static method

Future<void> createPlaceholder(
  1. String inputPath,
  2. String outputPath,
  3. int maxSize
)

Создание placeholder изображения

Implementation

static Future<void> createPlaceholder(
  String inputPath,
  String outputPath,
  int maxSize,
) async {
  final file = File(inputPath);
  final bytes = await file.readAsBytes();
  final image = img.decodeImage(bytes);

  if (image == null) {
    throw Exception('Не удалось декодировать изображение: $inputPath');
  }

  // Если изображение уже меньше maxSize, просто копируем
  if (image.width <= maxSize && image.height <= maxSize) {
    await file.copy(outputPath);
    return;
  }

  // Вычисляем новые размеры с сохранением пропорций
  double scale;
  if (image.width > image.height) {
    scale = maxSize / image.width;
  } else {
    scale = maxSize / image.height;
  }

  final newWidth = (image.width * scale).round();
  final newHeight = (image.height * scale).round();

  final resized = img.copyResize(
    image,
    width: newWidth,
    height: newHeight,
    interpolation: img.Interpolation.cubic,
  );

  final outputFile = File(outputPath);
  await outputFile.writeAsBytes(img.encodePng(resized));
}