Skip to content

Commit

Permalink
fix: proper height data for animated gifs
Browse files Browse the repository at this point in the history
  • Loading branch information
denolfe committed Apr 17, 2023
1 parent 6ceb791 commit 1427d1f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/uploads/generateFileData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,13 @@ export const generateFileData = async <T>({
}

if (sharpFile) {
const metadata = await sharpFile.metadata();
fileBuffer = await sharpFile.toBuffer({ resolveWithObject: true });
({ mime, ext } = await fromBuffer(fileBuffer.data));
({ mime, ext } = await fromBuffer(fileBuffer.data)); // This is getting an incorrect gif height back.
fileData.width = fileBuffer.info.width;
fileData.height = fileBuffer.info.height;

// Animated GIFs aggregate the height from every frame, so we need to use divide by number of pages
fileData.height = sharpOptions.animated ? (fileBuffer.info.height / metadata.pages) : fileBuffer.info.height;
fileData.filesize = fileBuffer.data.length;
} else {
mime = file.mimetype;
Expand Down
33 changes: 32 additions & 1 deletion test/uploads/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,43 @@ export default buildConfig({
},
],
},
{
slug: 'gif-resize',
upload: {
staticURL: '/media-gif',
staticDir: './media-gif',
mimeTypes: ['image/gif'],
resizeOptions: {
position: 'center',
width: 200,
height: 200,
},
formatOptions: {
format: 'gif',
},
imageSizes: [
{
name: 'small',
width: 100,
height: 100,
formatOptions: { format: 'gif', options: { quality: 90 } },
},
{
name: 'large',
width: 1000,
height: 1000,
formatOptions: { format: 'gif', options: { quality: 90 } },
},
],
},
fields: [],
},
{
slug: mediaSlug,
upload: {
staticURL: '/media',
staticDir: './media',
mimeTypes: ['image/png', 'image/jpg', 'image/jpeg', 'image/svg+xml', 'audio/mpeg'],
mimeTypes: ['image/png', 'image/jpg', 'image/jpeg', 'image/gif', 'image/svg+xml', 'audio/mpeg'],
resizeOptions: {
width: 1280,
height: 720,
Expand Down

0 comments on commit 1427d1f

Please sign in to comment.