forked from cyrilwanner/optimized-images-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
649dc09
commit c37714a
Showing
10 changed files
with
86 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,16 +1,45 @@ | ||
import sharp from 'sharp'; | ||
import encode from '@wasm-codecs/gifsicle'; | ||
import { LoaderOptions } from '../options'; | ||
import { ImageOptions } from '../parseQuery'; | ||
|
||
/** | ||
* Optimize a gif image using gifsicle | ||
* | ||
* @async | ||
* @param {Buffer} image Input image | ||
* @param {ImageOptions} imageOptions Image options | ||
* @param {LoaderOptions['gifsicle']} [options] Gifsicle options | ||
* @returns {Buffer} Optimized image | ||
*/ | ||
const optimizeGif = async (image: Buffer, options?: LoaderOptions['gifsicle']): Promise<Buffer> => { // eslint-disable-line | ||
const optimizeGif = async ( | ||
image: Buffer, | ||
imageOptions: ImageOptions, | ||
options?: LoaderOptions['gifsicle'], | ||
): Promise<Buffer> => { | ||
const encodeOptions = options || {}; | ||
|
||
if (imageOptions.resize) { | ||
if (imageOptions.width) { | ||
encodeOptions.width = imageOptions.width; | ||
} | ||
|
||
if (imageOptions.height) { | ||
encodeOptions.height = imageOptions.height; | ||
} | ||
} | ||
|
||
// optimize the image using gifsicle | ||
return image; | ||
const encodedImage = await encode(image, encodeOptions); | ||
|
||
// fill missing resize values in case the image was resized | ||
if (imageOptions.resize) { | ||
const imageData = await sharp(encodedImage).metadata(); | ||
imageOptions.width = imageData.width; // eslint-disable-line no-param-reassign | ||
imageOptions.height = imageData.height; // eslint-disable-line no-param-reassign | ||
} | ||
|
||
return encodedImage; | ||
}; | ||
|
||
export default optimizeGif; |
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
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