Line data Source code
1 : import 'dart:ui' show Rect;
2 : import 'package:flutter/foundation.dart';
3 : import '../worker.dart';
4 :
5 : /// Image output formats.
6 : enum ImageFormat {
7 : jpeg('jpeg'),
8 : png('png'),
9 : webp('webp');
10 :
11 : const ImageFormat(this.value);
12 : final String value;
13 : }
14 :
15 : /// Image processing worker configuration.
16 : ///
17 : /// Resizes, compresses, and converts images natively for optimal performance.
18 : @immutable
19 : final class ImageProcessWorker extends Worker {
20 4 : const ImageProcessWorker({
21 : required this.inputPath,
22 : required this.outputPath,
23 : this.maxWidth,
24 : this.maxHeight,
25 : this.maintainAspectRatio = true,
26 : this.quality = 85,
27 : this.outputFormat,
28 : this.cropRect,
29 : this.deleteOriginal = false,
30 : });
31 :
32 : /// Path to input image file.
33 : final String inputPath;
34 :
35 : /// Path where processed image will be saved.
36 : final String outputPath;
37 :
38 : /// Maximum width in pixels (null = no width limit).
39 : final int? maxWidth;
40 :
41 : /// Maximum height in pixels (null = no height limit).
42 : final int? maxHeight;
43 :
44 : /// Whether to maintain aspect ratio when resizing.
45 : final bool maintainAspectRatio;
46 :
47 : /// Output quality (0-100, higher is better quality).
48 : /// Only applies to JPEG and WEBP formats.
49 : final int quality;
50 :
51 : /// Output format (null = same as input).
52 : final ImageFormat? outputFormat;
53 :
54 : /// Optional crop rectangle (x, y, width, height).
55 : final Rect? cropRect;
56 :
57 : /// Whether to delete original image after processing.
58 : final bool deleteOriginal;
59 :
60 3 : @override
61 : String get workerClassName => 'ImageProcessWorker';
62 :
63 3 : @override
64 : Map<String, dynamic> toMap() {
65 3 : final map = <String, dynamic>{
66 : 'workerType': 'imageProcess',
67 3 : 'inputPath': inputPath,
68 3 : 'outputPath': outputPath,
69 3 : 'maintainAspectRatio': maintainAspectRatio,
70 3 : 'quality': quality,
71 3 : 'deleteOriginal': deleteOriginal,
72 : };
73 :
74 9 : if (maxWidth != null) map['maxWidth'] = maxWidth;
75 9 : if (maxHeight != null) map['maxHeight'] = maxHeight;
76 9 : if (outputFormat != null) map['outputFormat'] = outputFormat!.value;
77 3 : if (cropRect != null) {
78 4 : map['cropRect'] = {
79 6 : 'x': cropRect!.left.toInt(),
80 6 : 'y': cropRect!.top.toInt(),
81 6 : 'width': cropRect!.width.toInt(),
82 6 : 'height': cropRect!.height.toInt(),
83 : };
84 : }
85 :
86 : return map;
87 : }
88 : }
|