-
-
Notifications
You must be signed in to change notification settings - Fork 924
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): On-the-fly image transformations
Relates to #22.
- Loading branch information
1 parent
d98cc5b
commit c123019
Showing
5 changed files
with
129 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Request } from 'express'; | ||
import * as sharp from 'sharp'; | ||
|
||
import { ImageTransformMode, ImageTransformPreset } from './default-asset-server-plugin'; | ||
|
||
/** | ||
* Applies transforms to the gifen image according to the query params passed. | ||
*/ | ||
export async function transformImage( | ||
originalImage: Buffer, | ||
queryParams: Record<string, string>, | ||
presets: ImageTransformPreset[], | ||
): Promise<sharp.SharpInstance> { | ||
let width = +queryParams.w || undefined; | ||
let height = +queryParams.h || undefined; | ||
let mode = queryParams.mode || 'crop'; | ||
if (queryParams.preset) { | ||
const matchingPreset = presets.find(p => p.name === queryParams.preset); | ||
if (matchingPreset) { | ||
width = matchingPreset.width; | ||
height = matchingPreset.height; | ||
mode = matchingPreset.mode; | ||
} | ||
} | ||
const image = sharp(originalImage).resize(width, height); | ||
if (mode === 'crop') { | ||
image.crop(sharp.strategy.entropy); | ||
} else { | ||
image.max(); | ||
} | ||
return image; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters