mono/packages/media/docs/Examples.md
2025-02-13 23:57:33 +01:00

5.3 KiB

General usage

osr-media sw --src=(FOLDER||FILE)/GLOB --dst=EXPRESSION||FILE||FOLDER/GLOB

Parameters

src : The source directory or file. This can be a glob pattern.

dst : The source directory or file. This can be a glob pattern with expressions.

width : The image's new width

height : The image's new height

minWidth : Perform only when the image's width exceeds this threshold

minHeight : Perform only when the image's height exceeds this threshold

minSize : Perform only when the image's size exceeds this threshold (bytes)

Sharp-JS resize options

Param Type Default Description
[width] number How many pixels wide the resultant image should be. Use null or undefined to auto-scale the width to match the height.
[height] number How many pixels high the resultant image should be. Use null or undefined to auto-scale the height to match the width.
[width] number An alternative means of specifying width. If both are present this takes priority.
[height] number An alternative means of specifying height. If both are present this takes priority.
[fit] String 'cover' How the image should be resized/cropped to fit the target dimension(s), one of cover, contain, fill, inside or outside.
[position] String 'centre' A position, gravity or strategy to use when fit is cover or contain.
[background] String | Object {r: 0, g: 0, b: 0, alpha: 1} background colour when fit is contain, parsed by the color module, defaults to black without transparency.
[withoutEnlargement] Boolean false Do not scale up if the width or height are already less than the target dimensions, equivalent to GraphicsMagick's > geometry option. This may result in output dimensions smaller than the target dimensions.
[withoutReduction] Boolean false Do not scale down if the width or height are already greater than the target dimensions, equivalent to GraphicsMagick's < geometry option. This may still result in a crop to reach the target dimensions.
[fastShrinkOnLoad] Boolean true Take greater advantage of the JPEG and WebP shrink-on-load feature, which can lead to a slight moiré pattern or round-down of an auto-scaled dimension.

When both a width and height are provided, the possible methods by which the image should fit these are:

  • cover: (default) Preserving aspect ratio, attempt to ensure the image covers both provided dimensions by cropping/clipping to fit.
  • contain: Preserving aspect ratio, contain within both provided dimensions using "letterboxing" where necessary.
  • fill: Ignore the aspect ratio of the input and stretch to both provided dimensions.
  • 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.
  • 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.

Variables

SRC_DIR : The directory of the current file being converted

SRC_NAME : The file name of the current file being converted

SRC_FILE_EXT : The file extension of the current file being converted

Resize

Resize all JPGs by 80%

osr-media resize --src='./tests/images/in/*.+(jpg)' --dst='./tests/images/out/' --percent=80

Resize all JPGs by 80%, recursive

osr-media resize --src='./tests/images/in/**/*.+(jpg)' --dst='./tests/images/out/' --percent=80

Resize all JPGs by 80% and save as WEBP

osr-media resize --src='./tests/images/in/*.+(jpg)' --dst='./tests/images/out/*.+(webp)' --percent=80

Resize a JPG file by 80%, adding a filename suffix (_medium)

osr-media resize --src='./tests/images/in/DSC01177.JPG' --dst='&{SRC_DIR}/&{SRC_NAME}_medium.+(jpg)' --percent=80 

Resize all JPG files to a 1000px square (eg: Instagram)

osr-media resize --src='./tests/images/in/*.+(jpg)' --dst='./tests/images/out_square/.+(jpg)' --fit=contain --width=1000 --height=1000

Resize all image types to width = 2000, in place & recursive

osr-media resize --src='./tests/images/in/**/*.+(${IMAGES})' --percent=80

Resize all image types to width = 2000, only when the image's width is larger than 2000

osr-media resize --src='./tests/images/in/**/*.+(${IMAGES})' --width=2000 --minWidth=2000

Convert

Convert all WEBPs to JPGs

osr-media convert --src='./tests/images/in_webp/*.+(webp)' --dst='./tests/images/out_jpg/*.+(jpg)' 

Convert a WEBP to a JPG file

osr-media convert --src='./tests/images/in_webp/DSC01177.webp' --dst='./tests/images/out_jpg/DSC01177.jpg' 

Convert a WEBP to a JPG and a PNG file

osr-media convert --src='./tests/images/in_webp/DSC01177.webp' --dst='${SRC_DIR}/${SRC_NAME}.+(jpg|png)'