Skip to content

Fix image corruption with safer defaults, error handling, and integrity checks #395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

aditya305
Copy link

Issue: Images (e.g., PNG, JPG) were being corrupted during optimization, likely due to aggressive compression settings or unhandled errors in the gulp-imagemin plugin. I found out this issue and observed in a custom implementation and potentially the main plugin.

Fixes:

  • Safer Defaults: Adjusted plugin settings to reduce corruption risk:
    • imagemin-mozjpeg: Set quality: 90 (from a lower value) and enabled progressive: true for better compatibility.
    • imagemin-optipng: Set optimizationLevel: 3 to avoid over-compression of PNGs.
    • imagemin-gifsicle: Reduced optimizationLevel: 2 to prevent aggressive optimization.
    • imagemin-svgo: Limited plugins to preserve viewBox and avoid unintended changes.
  • Error Handling: Added try-catch blocks around imagemin.buffer to catch and log optimization errors, returning the original file if optimization fails to prevent corruption.
  • Integrity Check: Implemented a basic size check (skipping files < 100 bytes) to detect and avoid corrupt outputs.
  • Enhanced Logging: Improved verbose output with chalk for better debugging, showing success, warnings, and errors.

Testing:

  • Tested on April 29, 2025, with a sample set of PNG, JPG, GIF, and SVG files.
  • Ran gulp with --verbose to verify logging and ensured optimized images rendered correctly in a browser and image viewer.
  • Confirmed no corruption by comparing original and optimized files using file command (e.g., file dist/assets/image.png).

Impact:

  • Reduces the likelihood of image corruption during optimization.
  • Maintains reasonable file size reduction while prioritizing integrity.
  • Users can enable verbose mode (--verbose) to debug issues.

Additional Notes:

  • The changes are backward-compatible but may result in slightly larger file sizes due to safer settings.
  • Consider adding unit tests for the integrity check and error handling if the project supports it (e.g., Mocha).
  • Open to feedback on adjusting default settings or adding configuration options for advanced users.

Checklist:

  • Code compiles successfully.
  • Tests pass (if applicable).
  • Documentation updated (if needed, e.g., README or plugin docs).
  • Changes reviewed locally.

Please review and let me know if further adjustments are needed!

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.

1 participant