henrygd/optimize

By henrygd

Updated 9 months ago

Dockerized script to bulk optimize images using libvips / sharp / bun.

Image
Developer Tools
1

715

Dockerized script to bulk optimize images using libvips / sharp / bun.

example terminal command output: Total: 5.36MB saved from 4 images

Modes

overwrite: Overwrite existing images (default). Scans the directory mounted to /images.

docker run --rm -v ./images:/images -v ./backup:/backup henrygd/optimize

restore: Restore original images from backup (reverses last overwrite operation).

docker run --rm -v ./images:/images -v ./backup:/backup -e MODE=restore henrygd/optimize

copy: Write images to different directory. This example converts all images to WEBP.

docker run --rm -v ./images:/images -v ./optimized:/optimized -e MODE=copy -e FORMAT=webp henrygd/optimize

Environment Variables

NameModeDescriptionDefault
EXTENSIONS*Extensions to optimize1jpg,jpeg,png,webp,tif,tiff
FIT*Fit methodinside
FORMATcopyOutput format2unset
JOBS*Number of parallel conversion jobsBased on available CPU cores3
MAX_AGE*Age threshold in hours4unset
MAX_HEIGHT*Max height of output image4000
MAX_WIDTH*Max width of output image4000
MIN_SIZE*Size threshold in kilobytes5unset
MODE*Modeoverwrite
OWNER*Ownership of new files6root:root
QUALITY*Output quality80
QUIET*Log only errors, not every fileunset

Fit Methods

  • inside: Preserving aspect ratio, resize the image to be as large as possible while ensuring its dimensions are less than or equal to both those specified.
  • cover: Crop to cover both provided dimensions.
  • contain: Embed within both provided dimensions.
  • fill: Ignore the aspect ratio of the input and stretch to both provided dimensions.
  • outside: Preserving aspect ratio, resize the image to be as small as possible while ensuring its dimensions are greater than or equal to both those specified.

Footnotes

  1. Uppercase versions of extensions are added automatically.

  2. This will force all optimized images to be converted to the specified format. Possible values: webp, avif.

  3. Default JOBS value is one fewer than half of your available cores. If you have 16 cores, it's 7 jobs. If you have 4 cores or fewer, it's only one job.

  4. Images are only optimized if they were created in the last MAX_AGE hours. For example, 24 would only optimize images created in the last 24 hours.

  5. Images are only optimized if they are larger than MIN_SIZE. For example, 800 would only optimize images larger than 800kB.

  6. This applies only to newly created files. Overwritten files should maintain existing permissions. Value should use IDs. For example: -e OWNER=1000:1000, or -e OWNER="$(id -u):$(id -g)".

Docker Pull Command

docker pull henrygd/optimize