Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: quality option, image transformation #145

Merged
merged 1 commit into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion infra/storage/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
FROM supabase/storage-api:v0.27.1
FROM supabase/storage-api:v0.28.0

RUN apk add curl --no-cache
4 changes: 4 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ export interface TransformOptions {
* Fill resizes the image to fill the entire width and height. If the object's aspect ratio does not match the width and height, the image will be stretched to fit.
*/
resize?: 'cover' | 'contain' | 'fill'
/**
* Set the quality of the returned image, this is percentage based, default 80
*/
quality?: number
/**
* Specify the format of the image requested.
*
Expand Down
4 changes: 4 additions & 0 deletions src/packages/StorageFileApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,10 @@ export default class StorageFileApi {
params.push(`format=${transform.format}`)
}

if (transform.quality) {
params.push(`quality=${transform.quality}`)
}

return params.join('&')
}
}
6 changes: 4 additions & 2 deletions test/storageFileApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,11 @@ describe('Object API', () => {
transform: {
width: 200,
height: 300,
quality: 70,
},
})
expect(res.data.publicUrl).toEqual(
`${URL}/render/image/public/${bucketName}/${uploadPath}?width=200&height=300`
`${URL}/render/image/public/${bucketName}/${uploadPath}?width=200&height=300&quality=70`
)
})

Expand Down Expand Up @@ -295,6 +296,7 @@ describe('Object API', () => {
transform: {
width: 200,
height: 200,
quality: 60,
},
})

Expand All @@ -306,7 +308,7 @@ describe('Object API', () => {
expect(parseInt(imageResp.headers.get('content-length') || '')).toBeGreaterThan(0)
expect(imageResp.status).toEqual(200)
expect(imageResp.headers.get('x-transformations')).toEqual(
'height:200,width:200,resizing_type:fill'
'height:200,width:200,resizing_type:fill,quality:60'
)
})
})