Skip to content

Commit 101cfe3

Browse files
authored
allow specifying the output format
1 parent 3a7c5c9 commit 101cfe3

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

example/pages/index.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const demoContent: {
1111
file: string;
1212
proxyParams?: string;
1313
layout?: 'fill';
14+
format?: string;
1415
width?: number;
1516
height?: number;
1617
}[] = [
@@ -19,6 +20,12 @@ const demoContent: {
1920
file: 'test-bucket/test-image.png',
2021
layout: 'fill',
2122
},
23+
{
24+
label: 'Changing filetype',
25+
file: 'test-bucket/test-image.png',
26+
layout: 'fill',
27+
format: 'jpg',
28+
},
2229
{
2330
label: 'Blurring',
2431
file: 'test-bucket/test-image.png',
@@ -101,7 +108,9 @@ const Home: NextPage = () => {
101108
style={{
102109
backgroundImage: `url(${buildProxyImagePath(
103110
'test-bucket/test-image.png',
104-
new ImgProxyParamBuilder().blur(10).build(),
111+
{
112+
proxyParams: new ImgProxyParamBuilder().blur(10).build(),
113+
},
105114
)})`,
106115
backgroundSize: 'cover',
107116
color: 'white',

src/index.tsx

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import * as React from 'react';
22
import Image, { ImageLoaderProps, ImageProps } from 'next/image';
33

44
import { createHmac } from 'crypto';
5-
import { ServerResponse, request as httpRequest } from 'http';
6-
import { request as httpsRequest } from 'https';
75
import { ParsedUrlQuery } from 'node:querystring';
6+
import { request as httpsRequest } from 'https';
7+
import { ServerResponse, request as httpRequest } from 'http';
88

99
import ImgProxyParamBuilder from './param-builder';
1010

@@ -26,15 +26,17 @@ const imageOptimizer = (
2626
salt: string;
2727
},
2828
) => {
29-
const { src, params } = query;
29+
const { src, params, format } = query;
3030
if (!src) {
3131
res.statusCode = 400;
3232
res.end();
3333
return;
3434
}
3535

36+
const fileFormat = format ? `@${format}` : '';
3637
const paramString = params ? `${params}/` : '';
37-
const requestPath = `/${paramString}plain/s3://${src}@png`;
38+
const requestPath = `/${paramString}plain/s3://${src}${fileFormat}`;
39+
3840
const signature = signatureParams
3941
? generateSignature(signatureParams.key, signatureParams.salt, requestPath)
4042
: '';
@@ -71,27 +73,38 @@ const imageOptimizer = (
7173
req.end();
7274
};
7375

74-
const buildProxyImagePath = (file: string, proxyParams?: string): string => {
76+
type ProxyImageProps = {
77+
file: string;
78+
format?: string;
79+
proxyParams?: string;
80+
};
81+
82+
const buildProxyImagePath = (
83+
file: string,
84+
options?: Omit<ProxyImageProps, 'file'>,
85+
): string => {
86+
const { proxyParams, format } = options ?? {};
87+
7588
const urlParams = new URLSearchParams();
89+
7690
urlParams.append('src', file);
7791
if (proxyParams) urlParams.append('params', proxyParams);
78-
return `${IMGPROXY_ENDPOINT}?${urlParams.toString()}`;
79-
};
92+
if (format) urlParams.append('format', format);
8093

81-
type ProxyImageProps = {
82-
file: string;
83-
proxyParams?: string;
94+
return `${IMGPROXY_ENDPOINT}?${urlParams.toString()}`;
8495
};
8596

8697
const ProxyImage = ({
8798
file,
8899
proxyParams,
100+
format,
89101
...props
90102
}: ProxyImageProps & Omit<ImageProps, 'src' | 'quality' | 'unoptimized'>) => {
91103
const imageLoader = ({ src, width }: ImageLoaderProps): string => {
92104
const urlParams = new URLSearchParams();
93105
urlParams.append('src', src);
94106
if (proxyParams) urlParams.append('params', proxyParams);
107+
if (format) urlParams.append('format', format);
95108

96109
// This doesn't actually do anything, it's just to suppress
97110
// this error https://nextjs.org/docs/messages/next-image-missing-loader-width

src/param-builder.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ type ForwardType = Partial<ImgProxyParamBuilder> & {
1919
// for possible transformations
2020

2121
class ImgProxyParamBuilder {
22-
public readonly fileName: string;
2322
public readonly modifiers: string[] = [];
2423

2524
constructor() {}

0 commit comments

Comments
 (0)