URL Builder For Sharp Image Transformations
npm install sharp-image-url-builderA lightweight utility for building Base64 encoded image transformation URLs for Sharp.
---
``bash`
npm install sharp-image-url-builder
---
`ts
import { ImageUrlBuilder } from 'sharp-image-url-builder';
const builder = new ImageUrlBuilder('https://cdn.example.com', 'my-bucket');
const url = builder
.setKey('images/photo.jpg')
.resize({
width: 800,
height: 600,
fit: 'cover',
position: 'center'
})
.blur(5)
.toURL();
console.log(url);
// https://cdn.example.com/eyJidWNrZXQiOiJteS1idWNrZXQiLCJrZXkiOiJpbWFnZXMvcGhvdG8uanBnIiwiZWRpdHMiOnsicmVzaXplIjp7IndpZHRoIjo4MDAsImhlaWdodCI6NjAwLCJmaXQiOiJjb3ZlciIsInBvc2l0aW9uIjoiY2VudGVyIn0sImJsdXIiOjV9fQ==
`
---
Create a new builder instance.
- baseUrl – your CDN or image service base URL bucket
- – bucket/container name
---
Set the image key (path to your file in the bucket).
---
Apply resize options.
`ts`
builder.resize({
width: 400,
height: 300,
fit: 'contain',
position: 'top',
gravity: 'north-west',
withoutEnlargement: true
});
#### Resize Options
- width?: numberheight?: number
- fit?: 'cover' | 'contain' | 'fill' | 'inside' | 'outside'
- position?: 'top' | 'bottom' | 'left' | 'right' | 'center' | 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
- gravity?: 'north' | 'south' | 'east' | 'west' | 'center' | 'north-east' | 'north-west' | 'south-east' | 'south-west'
- entropy?: 'entropy' | 'attention'
- withoutEnlargement?: boolean
-
---
Apply a blur effect.
---
Generate the final transformation URL. Encodes the transformObject` as Base64 JSON.
---
- Generate Base64 encoded URLs for serverless image transformers
- Apply simple effects (blur, crop) without heavy backend logic