Gathering detailed insights and metrics for unpic
Gathering detailed insights and metrics for unpic
Gathering detailed insights and metrics for unpic
Gathering detailed insights and metrics for unpic
npm install unpic
99.1
Supply Chain
100
Quality
88.5
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
302 Stars
143 Commits
36 Forks
2 Watching
33 Branches
21 Contributors
Updated on 29 Nov 2024
Minified
Minified + Gzipped
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
-10.1%
5,972
Compared to previous day
Last week
-5.3%
31,536
Compared to previous week
Last month
-18.2%
140,820
Compared to previous month
Last year
12.9%
1,901,471
Compared to previous year
4
Universal image CDN URL translator
There are many image CDNs that provide a URL API for transforming images. There is little consistency in these APIs, and it's often unclear what the API is for a given URL. This library aims to provide a consistent interface for detecting image CDN URLs and transforming them.
If you'd like to use this on the web, you might want to try Unpic img, a multi-framework image component, powered by Unpic.
It designed to work with image URLs from sources such as CMSs and other user-generated content, where the source image may or may not be from an image CDN, and may already have transforms applied. This allow different transforms to be applied for display on a website. A web framework may need to transform an image for display on a site. Rather than doing this by downloading and resizing it locally or re-processing it with a separate image service, this library can be used to transform the URL to use the original image CDN, which will then transform the image on the fly.
This library is available via NPM as unpic
and JSR as
@unpic/lib
.
To use it in Node, install it from npm:
1npm install unpic
Then import it in your code:
1import { transformUrl } from "unpic";
To use it in Deno, import the module from JSR:
1import { transformUrl } from "jsr:@unpic/lib";
If you previously installed the library from deno.land/x, you should update to use JSR instead as the deno.land/x version is no longer updated.
You can then use the transformUrl
function to transform a URL:
1const url = transformUrl(
2 {
3 url:
4 "https://cdn.shopify.com/static/sample-images/bath_grande_crop_center.jpeg",
5 width: 800,
6 height: 600,
7 },
8);
9
10console.log(url.toString());
11
12// https://cdn.shopify.com/static/sample-images/bath.jpeg?width=800&height=600&crop=center
You can also use the parseUrl
function to parse a URL and get the CDN and any
params:
1const parsedUrl = parseUrl( 2 "https://cdn.shopify.com/static/sample-images/bath_800x600_crop_center.jpeg", 3); 4 5console.log(parsedUrl); 6// { 7// cdn: "shopify", 8// width: 800, 9// height: 600, 10// base: "https://cdn.shopify.com/static/sample-images/bath.jpeg", 11// params: { 12// crop: "center", 13// }, 14// }
You can bypass auto-detection by specifying the CDN:
1const url = transformUrl( 2 { 3 url: 4 "https://cdn.shopify.com/static/sample-images/bath_grande_crop_center.jpeg", 5 width: 800, 6 height: 600, 7 cdn: "shopify", 8 }, 9);
This is particularly useful if you are using the CDN with a custom domain which is not auto-detected.
Some transformers support URL delegation. This means that the source image URL
is also checked, and if it matches a CDN then the transform is applied directly
to the source image. For example: consider a next/image
URL that points to an
image on Shopify. The URL is detected as a nextjs
URL because it starts with
/_next/image
. The nextjs
transformer supports delegation, so the source
image URL is then checked. As it matches a Shopify domain, the transform is
applied directly to the Shopify URL. This means that the image is transformed on
the fly by Shopify, rather than by Next.js. However if the source image is not a
supported CDN, or is a local image then the nextjs
transformer will return a
/_next/image
URL.
transformUrl
and parseUrl
.transformUrl
, the library will remove any format set in the
source image, changing it to auto-format. In most cases, this is what you
want. Almost all browsers now support modern formats such as WebP, and setting
auto-format will allow the CDN to serve the best format for the browser. If
you want to force a specific format, you can set it in transformUrl
.transformUrl
, it will return undefined
, as it will for any
URL that is not recognised as an image CDN URL. It is up to you to handle this
case.See the contributing guide.
No vulnerabilities found.
No security vulnerabilities found.