-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Render GIF images #531
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
Closed
Closed
Render GIF images #531
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,69 @@ | ||
| /* global File */ | ||
|
|
||
| import jpegasus from 'jpegasus'; | ||
| import toArrayBuffer from 'to-arraybuffer'; | ||
| import toBufferCb from 'blob-to-buffer'; | ||
| import sharp from 'sharp'; | ||
| import JPEG from './jpeg'; | ||
| import PNG from './png'; | ||
|
|
||
| const toBuffer = blob => | ||
| new Promise((resolve, reject) => | ||
| toBufferCb(blob, (err, buffer) => (err ? reject(err) : resolve(buffer))), | ||
| ); | ||
|
|
||
| const renderGIF = async data => { | ||
| if (!GIF.isValid(data)) { | ||
| throw new TypeError( | ||
| 'Image passed to GIF decoder appears not to be in GIF format', | ||
| ); | ||
| } | ||
|
|
||
| if (!BROWSER) { | ||
| const pngBuffer = await sharp(data).toBuffer(); | ||
| return new PNG(pngBuffer); | ||
| } | ||
|
|
||
| const jpegBlob = await jpegasus.compress( | ||
| new File([toArrayBuffer(data)], 'image.gif', { | ||
| type: 'image/gif', | ||
| }), | ||
| { | ||
| quality: 0.8, | ||
| }, | ||
| ); | ||
| const jpegBuffer = await toBuffer(jpegBlob); | ||
| return new JPEG(jpegBuffer); | ||
| }; | ||
|
|
||
| // Instantiate a “thenable” object — an object that behaves like a Promise | ||
| // https://samdanielson.com/blog/2016/09/15/subclassing-promises-not.html | ||
| // by implementing the Promise prototype’s public methods, and applying them to a real Promise | ||
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#Promise_prototype | ||
| class GIF { | ||
| constructor(data) { | ||
| this.imagePromise = renderGIF(data); | ||
| } | ||
|
|
||
| then(...args) { | ||
| return this.imagePromise.then.apply(this.imagePromise, args); | ||
| } | ||
|
|
||
| catch(...args) { | ||
| return this.imagePromise.catch.apply(this.imagePromise, args); | ||
| } | ||
|
|
||
| finally(...args) { | ||
| return this.imagePromise.finally.apply(this.imagePromise, args); | ||
| } | ||
| } | ||
|
|
||
| GIF.isValid = function(data) { | ||
| try { | ||
| return data[0] === 71 && data[1] === 73 && data[2] === 70; | ||
| } catch (e) { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| export default GIF; | ||
This file contains hidden or 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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,24 @@ | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import GIF from '../src/utils/gif'; | ||
|
|
||
| const localGIFImage = fs.readFileSync(path.join(__dirname, 'assets/test.gif')); | ||
| const localPNGImage = fs.readFileSync(path.join(__dirname, 'assets/test.png')); | ||
|
|
||
| describe('gif', () => { | ||
| test('should return true for valid gif images', () => { | ||
| expect(GIF.isValid(localGIFImage)).toBeTruthy(); | ||
| }); | ||
|
|
||
| test('should return false for non gif images', () => { | ||
| expect(GIF.isValid(localPNGImage)).toBeFalsy(); | ||
| }); | ||
|
|
||
| test('should return false for empty argument', () => { | ||
| expect(GIF.isValid()).toBeFalsy(); | ||
| }); | ||
|
|
||
| test('should return false for non buffer argument', () => { | ||
| expect(GIF.isValid('some data')).toBeFalsy(); | ||
| }); | ||
| }); |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -16,8 +16,10 @@ let dummyRoot; | |
|
|
||
| const jpgImageUrl = 'https://react-pdf.org/static/images/quijote1.jpg'; | ||
| const pngImageUrl = 'https://react-pdf.org/static/images/quijote2.png'; | ||
| const gifImageUrl = 'https://react-pdf.org/static/images/quijote3.gif'; | ||
| const localJPGImage = fs.readFileSync(path.join(__dirname, 'assets/test.jpg')); | ||
| const localPNGImage = fs.readFileSync(path.join(__dirname, 'assets/test.png')); | ||
| const localGIFImage = fs.readFileSync(path.join(__dirname, 'assets/test.gif')); | ||
|
|
||
| jest.mock('../src/utils/warning'); | ||
|
|
||
|
|
@@ -141,6 +143,19 @@ describe('Image', () => { | |
| expect(dummyRoot.instance.image.mock.calls[0][0]).toBe(image.image.data); | ||
| }); | ||
|
|
||
| test('Should render a gif image over http', async () => { | ||
| fetch.once(localGIFImage); | ||
|
|
||
| const image = new Image(dummyRoot, { src: gifImageUrl }); | ||
|
|
||
| await image.fetch(); | ||
| await image.render(); | ||
|
|
||
| expect(image.image.data).toBeTruthy(); | ||
| expect(dummyRoot.instance.image.mock.calls).toHaveLength(1); | ||
| expect(dummyRoot.instance.image.mock.calls[0][0]).toBe(image.image.data); | ||
| }); | ||
|
|
||
| test('Should render a remote image from src object', async () => { | ||
| fetch.once(localJPGImage); | ||
|
|
||
|
|
@@ -254,6 +269,17 @@ describe('Image', () => { | |
| expect(dummyRoot.instance.image.mock.calls[0][0]).toBe(image.image.data); | ||
| }); | ||
|
|
||
| /* test('Should render a buffer gif image', async () => { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test currently makes the test runner hang, which is why I’ve commented it out. Haven’t worked out why it’s hanging yet 🤔 |
||
| const image = new Image(dummyRoot, { src: localGIFImage }); | ||
|
|
||
| await image.fetch(); | ||
| await image.render(); | ||
|
|
||
| expect(image.image.data).toBeTruthy(); | ||
| expect(dummyRoot.instance.image.mock.calls).toHaveLength(1); | ||
| expect(dummyRoot.instance.image.mock.calls[0][0]).toBe(image.image.data); | ||
| }); */ | ||
|
|
||
| test('Should correctly resolve objectFit styles given a flat `style` prop', async () => { | ||
| const resolveObjectFitSpy = jest.spyOn(objectFit, 'resolveObjectFit'); | ||
| resolveObjectFitSpy.mockClear(); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is an issue with the way Rollup bundles this — because it doesn’t do dead-code elimination exactly, the browser builds still import the server-side
sharpmodule, so — even though theBROWSERglobal is set totrue— when it’s consuming the build Webpack processes theimport sharp from 'sharp'statement and gives this error:WARNING: Critical dependency: the request of a dependency is an expression(expand for details)ERROR: Module not found: Error: Can't resolve 'child_process'(expand for details)Do you know of a way to shake the non-
BROWSERcode completely out of Rollup’s browser builds? :)