🎥 Make videos programmatically with React
feat: add get-image-dimensions function
jkcs8aec0b7e834c
diff --git a/packages/docs/docs/get-image-dimensions.md b/packages/docs/docs/get-image-dimensions.mdnew file mode 100644index 0000000000..bdb5bf74bc--- /dev/null+++ b/packages/docs/docs/get-image-dimensions.md@@ -0,0 +1,69 @@+---+image: /generated/articles-docs-get-image-dimensions.png+title: getImageDimensions()+id: get-image-dimensions+crumb: "@remotion/media-utils"+---++_Part of the `@remotion/media-utils` package of helper functions.Available from v4.0.143._++Takes an image `src`, retrieves the dimensions of an image.++:::info+To avoid [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) problems, ensure that you have the proper permissions to access the image and that the image server is configured with appropriate CORS headers to allow requests from your domain.+:::++## Arguments++### `src`++A string that specifies the URL or path of the image.++## Return value++_`Promise<ImageDimensions>`_++An object with information about the image dimensions:++### `width`++_number_++The image width, in pixels (px).++### `height`++_number_++The image height, in pixels (px).++## Example++```ts twoslash+// @module: ESNext+// @target: ESNext+import { getImageDimensions } from "@remotion/media-utils";++await getImageDimensions("https://example.com/remote-image.png");+/* {+ width: 100,+ height: 100+} */++```++## Caching behavior++This function is memoizing the results it returns.++If you pass in the same argument to `src` multiple times, it will return a cached version from the second time on, regardless of if the file has changed.+To clear the cache, you have to reload the page.++## See also++- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/media-utils/src/get-audio-data.ts)+- [Using audio](/docs/using-audio)+- [Audio visualization](/docs/audio-visualization)+- [`<Audio/>`](/docs/audio)+- [`visualizeAudio()`](/docs/visualize-audio)+- [`useAudioData()`](/docs/use-audio-data)diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex cabf54aa5c..6c260dba26 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -197,6 +197,7 @@ module.exports = {"get-waveform-portion","use-audio-data","visualize-audio",+ "get-image-dimensions",],},diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.tsindex d7b28cef7f..2a4785cd1d 100644--- a/packages/docs/src/data/articles.ts+++ b/packages/docs/src/data/articles.ts@@ -384,13 +384,6 @@ export const articles = [compId: "articles-docs-cloudrun-generate-env",crumb: "Cloud Run",},- {- id: "getserviceinfo",- title: "getServiceInfo()",- relativePath: "docs/cloudrun/getServiceinfo.md",- compId: "articles-docs-cloudrun-getServiceinfo",- crumb: "Cloud Run API",- },{id: "getorcreatebucket",title: "getOrCreateBucket()",@@ -405,6 +398,13 @@ export const articles = [compId: "articles-docs-cloudrun-getregions",crumb: "Cloud Run API",},+ {+ id: "getserviceinfo",+ title: "getServiceInfo()",+ relativePath: "docs/cloudrun/getServiceinfo.md",+ compId: "articles-docs-cloudrun-getServiceinfo",+ crumb: "Cloud Run API",+ },{id: "getservices",title: "getServices()",@@ -632,7 +632,7 @@ export const articles = [{id: "delay-render",title: "delayRender() and continueRender()",- relativePath: "docs/delay-render.md",+ relativePath: "docs/delay-render.mdx",compId: "articles-docs-delay-render",crumb: "How to",},@@ -741,6 +741,13 @@ export const articles = [compId: "articles-docs-get-audio-duration-in-seconds",crumb: "@remotion/media-utils",},+ {+ id: "get-image-dimensions",+ title: "getImageDimensions()",+ relativePath: "docs/get-image-dimensions.md",+ compId: "articles-docs-get-image-dimensions",+ crumb: "@remotion/media-utils",+ },{id: "get-input-props",title: "getInputProps()",@@ -779,7 +786,7 @@ export const articles = [{id: "getting-started",title: "Creating a new project",- relativePath: "docs/getting-started.md",+ relativePath: "docs/getting-started.mdx",compId: "articles-docs-getting-started",crumb: "Let's begin!",},@@ -856,14 +863,14 @@ export const articles = [{id: "iframe",title: "<IFrame>",- relativePath: "docs/iframe.md",+ relativePath: "docs/iframe.mdx",compId: "articles-docs-iframe",crumb: "API",},{id: "img",title: "<Img>",- relativePath: "docs/img.md",+ relativePath: "docs/img.mdx",compId: "articles-docs-img",crumb: "API",},@@ -1017,7 +1024,7 @@ export const articles = [{id: "sites",title: "npx remotion lambda sites",- relativePath: "docs/lambda/cli/sites.md",+ relativePath: "docs/lambda/cli/sites.mdx",compId: "articles-docs-lambda-cli-sites",crumb: "Lambda CLI Reference",},@@ -1094,7 +1101,7 @@ export const articles = [{id: "deploysite",title: "deploySite()",- relativePath: "docs/lambda/deploysite.md",+ relativePath: "docs/lambda/deploysite.mdx",compId: "articles-docs-lambda-deploysite",crumb: "Lambda API",},@@ -1521,7 +1528,7 @@ export const articles = [{id: "loop",title: "<Loop>",- relativePath: "docs/loop.md",+ relativePath: "docs/loop.mdx",compId: "articles-docs-loop",crumb: "API",},@@ -1542,7 +1549,7 @@ export const articles = [{id: "lottie-comp",title: "<Lottie>",- relativePath: "docs/lottie/lottie-comp.md",+ relativePath: "docs/lottie/lottie-comp.mdx",compId: "articles-docs-lottie-lottie-comp",crumb: "@remotion/lottie",},@@ -2335,7 +2342,7 @@ export const articles = [{id: "get-compositions",title: "getCompositions()",- relativePath: "docs/renderer/get-compositions.md",+ relativePath: "docs/renderer/get-compositions.mdx",compId: "articles-docs-renderer-get-compositions",crumb: "@remotion/renderer",},@@ -2391,7 +2398,7 @@ export const articles = [{id: "select-composition",title: "selectComposition()",- relativePath: "docs/renderer/select-composition.md",+ relativePath: "docs/renderer/select-composition.mdx",compId: "articles-docs-renderer-select-composition",crumb: "@remotion/renderer",},@@ -2888,7 +2895,7 @@ export const articles = [{id: "timeout",title: "Debugging timeouts",- relativePath: "docs/timeout.md",+ relativePath: "docs/timeout.mdx",compId: "articles-docs-timeout",crumb: "Troubleshooting",},diff --git a/packages/docs/static/generated/articles-docs-get-image-dimensions.png b/packages/docs/static/generated/articles-docs-get-image-dimensions.pngnew file mode 100644index 0000000000..493f92abcdBinary files /dev/null and b/packages/docs/static/generated/articles-docs-get-image-dimensions.png differdiff --git a/packages/media-utils/src/get-image-dimensions.ts b/packages/media-utils/src/get-image-dimensions.tsnew file mode 100644index 0000000000..ac066b400e--- /dev/null+++ b/packages/media-utils/src/get-image-dimensions.ts@@ -0,0 +1,42 @@+import {pLimit} from './p-limit';+import type {ImageDimensions} from './types';++const imageDimensionsCache: {[key: string]: ImageDimensions} = {};++const limit = pLimit(3);++const fn = async (src: string): Promise<ImageDimensions> => {+ if (imageDimensionsCache[src]) {+ return imageDimensionsCache[src];+ }++ if (typeof document === 'undefined') {+ throw new Error('getImageDimensions() is only available in the browser.');+ }++ const imageDimensions = await new Promise<ImageDimensions>(+ (resolved, reject) => {+ const image = new Image();++ image.onload = () => {+ const {width, height} = image;+ resolved({width, height});+ };++ image.onerror = reject;++ image.src = src;+ },+ );++ imageDimensionsCache[src] = imageDimensions;+ return imageDimensions;+};++/**+ * @description Takes an image src, retrieves the dimensions of an image.+ * @see [Documentation](https://www.remotion.dev/docs/get-image-dimensions)+ */+export function getImageDimensions(src: string): Promise<ImageDimensions> {+ return limit(fn, src);+}diff --git a/packages/media-utils/src/index.ts b/packages/media-utils/src/index.tsindex d2b75156a1..2910bb0741 100644--- a/packages/media-utils/src/index.ts+++ b/packages/media-utils/src/index.ts@@ -10,3 +10,4 @@ export * from './types';export type {AudioData, VideoMetadata as VideoData} from './types';export {useAudioData} from './use-audio-data';export {visualizeAudio} from './visualize-audio';+export {getImageDimensions} from './get-image-dimensions';\ No newline at end of filediff --git a/packages/media-utils/src/types.ts b/packages/media-utils/src/types.tsindex b04cbf419d..9770809a43 100644--- a/packages/media-utils/src/types.ts+++ b/packages/media-utils/src/types.ts@@ -14,3 +14,5 @@ export type VideoMetadata = {aspectRatio: number;isRemote: boolean;};++export type ImageDimensions = {width: number; height: number};