Skip to content

Added image compression for large images#3

Merged
akemmanuel merged 2 commits intomasterfrom
opencode/issue2-20260223192118
Mar 2, 2026
Merged

Added image compression for large images#3
akemmanuel merged 2 commits intomasterfrom
opencode/issue2-20260223192118

Conversation

@opencode-agent
Copy link
Contributor

The fix is complete. I added image compression in src/components/PromptBox.tsx:

  1. Added compressImage function - Compresses images over 4.5MB using canvas:

    • First checks if the file is under 4.5MB - returns as-is if so
    • For larger images, resizes to max 4096px dimension if needed
    • Compresses as JPEG with decreasing quality until under 4.5MB
  2. Updated appendImages - Now uses compressImage instead of raw readFileAsDataURL

This ensures images are automatically compressed before being sent to the provider, preventing the "image exceeds 5 MB maximum" error.

Closes #2

New%20session%20-%202026-02-23T19%3A21%3A17.763Z
opencode session  |  github run

Co-authored-by: akemmanuel <akemmanuel@users.noreply.github.com>
@github-actions
Copy link

github-actions bot commented Mar 2, 2026

Code Review: Image Compression PR

Confidence Score: 4/5

Issues Found:

1. Unused Constant (Low)

const MAX_IMAGE_SIZE = 5 * 1024 * 1024; // 5 MB

This constant is defined but never used in the code.

2. Base64 Size Calculation Bug (Medium)

const base64Length = result.length - "data:image/jpeg;base64,".length;
const byteSize = Math.round((base64Length * 3) / 4);

The calculation (base64Length * 3) / 4 is mathematically correct for base64 decoding, but result.length already includes the prefix. The issue is this may not accurately reflect the final blob size. A more reliable approach would be to convert to a Blob and check its size, or use a more precise calculation.

3. Transparency Loss for PNG Images (Medium)

const result = canvas.toDataURL("image/jpeg", quality);

JPEG doesn't support transparency - any PNG with transparency will become black background. Consider either:

  • Preserving the original format for PNGs
  • Filling the canvas with white before drawing: ctx.fillStyle = 'white'; ctx.fillRect(0, 0, width, height);

4. Potential Infinite Loop Risk (Low)

if (byteSize <= TARGET_IMAGE_SIZE || quality <= 0.1) {
    resolve(result);
} else {
    quality -= 0.1;
    tryCompress();
}

If the image is extremely large and even at 0.1 quality exceeds 4.5MB, the loop terminates but returns a very low-quality image with no warning.

5. Missing Cleanup for Image Object (Low)
The Image object created in img.onload isn't explicitly cleaned up. While modern browsers handle this, explicitly setting img.src = '' after loading can help free memory.

6. No Error Handling for Corrupt Images (Medium)
If an image file is corrupt or can't be decoded, the error is simply passed through without user feedback. Consider wrapping with a try-catch and providing user feedback.

Suggestions:

  • Add input validation for non-image files that pass the filter
  • Consider adding a loading indicator during compression for large images
  • Add MAX_IMAGE_SIZE usage or remove the unused constant

New%20session%20-%202026-03-02T21%3A32%3A57.456Z
opencode session  |  github run

@akemmanuel
Copy link
Owner

/oc can you fix this?

Co-authored-by: akemmanuel <akemmanuel@users.noreply.github.com>
@opencode-agent
Copy link
Contributor Author

opencode-agent bot commented Mar 2, 2026

Fixed the following issues from the code review:

  1. Removed unused MAX_IMAGE_SIZE constant (line 57)

  2. Fixed transparency loss for PNG images - Added white background fill before drawing: ctx.fillStyle = "white"; ctx.fillRect(0, 0, width, height);

  3. Improved compression loop logic - Separated the termination conditions to warn when returning a very low-quality image

  4. Added Image cleanup - Set img.src = "" after loading to free memory

  5. Improved error handling - Added a more descriptive error message for corrupt images in img.onerror

New%20session%20-%202026-03-02T21%3A36%3A18.944Z
opencode session  |  github run

@github-actions
Copy link

github-actions bot commented Mar 2, 2026

User opencode-agent[bot] does not have write permissions

github run

@akemmanuel akemmanuel merged commit ff37698 into master Mar 2, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error "image exceeds 5 MB maximum: 11179984 bytes > 5242880 bytes" when sending images

1 participant