From b9f9a7bea6e3da63097d9c13e5b8725ea8582877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Kr=C3=BCger?= Date: Mon, 17 Nov 2025 12:25:31 +0100 Subject: [PATCH] fix: correct ImageMagick write API to produce valid WebP data MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous implementation was producing invalid image data with wrong magic bytes (d0 e5 67 00 instead of 52 49 46 46 for WebP). Root cause: Incorrect usage of ImageMagick write API. Changes: - Set image.format BEFORE writing (tells ImageMagick the output format) - Simplified write() call to: image.write((data) => data) - This returns the correctly formatted image data The proper pattern is: 1. Set image.format = outputFormatEnum 2. Apply transformations (quality, resize) 3. Call image.write() which returns the encoded data This should now produce valid WebP files with correct RIFF header (52 49 46 46) and allow the preview to display properly. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- lib/converters/imagemagickService.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/converters/imagemagickService.ts b/lib/converters/imagemagickService.ts index 1d12160..5bb3735 100644 --- a/lib/converters/imagemagickService.ts +++ b/lib/converters/imagemagickService.ts @@ -39,6 +39,9 @@ export async function convertWithImageMagick( let result: Uint8Array | undefined; await ImageMagick.read(inputData, (image) => { + // Set the output format first + image.format = outputFormatEnum; + // Apply quality setting if specified if (options.imageQuality !== undefined) { image.quality = options.imageQuality; @@ -65,10 +68,8 @@ export async function convertWithImageMagick( if (onProgress) onProgress(70); - // Write to output format - image.write(outputFormatEnum, (data) => { - result = data; - }); + // Write the image data + result = image.write((data) => data); if (onProgress) onProgress(90); });