processImage method
Implementation
@override
Future<String?> processImage(String imagePath, MediaOptions options) async {
try {
// For web, the imagePath is typically a data URL
if (!imagePath.startsWith('data:image')) {
return imagePath; // Return as-is if not a data URL
}
// Create an image element to process the data URL
final img = web.HTMLImageElement();
final completer = Completer<String?>();
img.onLoad.listen((_) async {
try {
final canvas = web.HTMLCanvasElement();
final ctx = canvas.getContext('2d') as web.CanvasRenderingContext2D;
// Apply crop options if provided
if (options.cropOptions?.enableCrop == true && options.cropOptions?.cropRect != null) {
final cropDimensions = _applyCropToImage(img, ctx, canvas, options.cropOptions!);
canvas.width = cropDimensions['width']!;
canvas.height = cropDimensions['height']!;
} else {
// No cropping, use original dimensions
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
ctx.drawImage(img, 0, 0);
}
// Apply watermark if provided
if (options.watermark != null && options.watermark!.isNotEmpty) {
_drawWatermark(ctx, canvas.width, canvas.height, options);
}
// Convert to data URL with quality settings
final dataUrl = canvas.toDataURL('image/jpeg');
completer.complete(dataUrl);
} catch (e) {
completer.complete(imagePath); // Return original on error
}
});
img.onError.listen((_) {
completer.complete(imagePath); // Return original on error
});
img.src = imagePath;
return await completer.future;
} catch (e) {
return imagePath; // Return original on error
}
}