Skip to content

Commit e9c5c79

Browse files
authored
allow using the image path without the component
1 parent 9b15c4f commit e9c5c79

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

example/pages/index.tsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { NextPage } from 'next';
22
import Head from 'next/head';
3-
import { Fragment as div } from 'react';
43

5-
import ProxyImage, { ImgProxyParamBuilder } from '../../dist';
4+
import ProxyImage, {
5+
ImgProxyParamBuilder,
6+
buildProxyImagePath,
7+
} from '../../dist';
68

79
const demoContent: {
810
label: string;
@@ -91,6 +93,28 @@ const Home: NextPage = () => {
9193
</div>
9294
))}
9395
</div>
96+
97+
<div>
98+
<div>
99+
<h2>Background Image</h2>
100+
<div
101+
style={{
102+
backgroundImage: `url(${buildProxyImagePath(
103+
'test-bucket/test-image.png',
104+
new ImgProxyParamBuilder().blur(10).build(),
105+
)})`,
106+
backgroundSize: 'cover',
107+
color: 'white',
108+
width: '200px',
109+
}}
110+
>
111+
Lorem ipsum dolor sit amet consectetur, adipisicing elit.
112+
Consectetur ipsam voluptates velit, perferendis alias maiores
113+
atque rem accusantium culpa vero doloremque repellat porro fugiat
114+
nam ad veniam accusamus aliquid molestias.
115+
</div>
116+
</div>
117+
</div>
94118
</main>
95119
</>
96120
);

src/index.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ const imageOptimizer = (
7171
req.end();
7272
};
7373

74+
const buildProxyImagePath = (file: string, proxyParams?: string): string => {
75+
const urlParams = new URLSearchParams();
76+
urlParams.append('src', file);
77+
if (proxyParams) urlParams.append('params', proxyParams);
78+
return `${IMGPROXY_ENDPOINT}?${urlParams.toString()}`;
79+
};
80+
7481
type ProxyImageProps = {
7582
file: string;
7683
proxyParams?: string;
@@ -98,4 +105,9 @@ const ProxyImage = ({
98105
};
99106

100107
export default ProxyImage;
101-
export { imageOptimizer, IMGPROXY_ENDPOINT, ImgProxyParamBuilder };
108+
export {
109+
buildProxyImagePath,
110+
imageOptimizer,
111+
IMGPROXY_ENDPOINT,
112+
ImgProxyParamBuilder,
113+
};

src/param-builder.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ type ForwardType = Partial<ImgProxyParamBuilder> & {
1515
modifiers: string[];
1616
};
1717

18+
// See https://github.com/imgproxy/imgproxy/blob/master/docs/generating_the_url_advanced.md
19+
// for possible transformations
20+
1821
class ImgProxyParamBuilder {
1922
public readonly fileName: string;
2023
public readonly modifiers: string[] = [];
@@ -42,7 +45,6 @@ class ImgProxyParamBuilder {
4245
};
4346
},
4447
): Omit<T, 'resize'> {
45-
// Signature: resize:%resizing_type:%width:%height:%enlarge:%extend
4648
const { type, width, height, enlarge, extend, gravity } = options ?? {};
4749
this.modifiers.push(
4850
options

0 commit comments

Comments
 (0)