Skip to content
This repository was archived by the owner on Mar 6, 2025. It is now read-only.

Commit ed69719

Browse files
committed
Improvement(ImageCropper): scale image down
After cropping, the avatar editor would upload the full-size image even though these images are used at much smaller dimensions. This change uses a version of the image scaled down to the size of the canvas which results in considerable file size reduction for large images.
1 parent 204d0cd commit ed69719

File tree

4 files changed

+18
-25
lines changed

4 files changed

+18
-25
lines changed

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"@types/jest": "^23.3.14",
7373
"@types/lodash.debounce": "^4.0.6",
7474
"@types/node": "^11.9.3",
75+
"@types/react-avatar-editor": "^10.3.5",
7576
"@types/react-dom": "^16.0.7",
7677
"@types/react-router": "^4.0.30",
7778
"@types/react-router-dom": "^4.3.0",

src/components/__tests__/image_cropper.test.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
// Copyright (c) 2018 Ultimaker B.V.
22
import * as React from 'react';
33
import { shallow } from 'enzyme';
4-
5-
// component
4+
import AvatarEditor from 'react-avatar-editor';
65
import RangeSlider from '../range_slider';
76
import ImageCropper from '../image_cropper';
87

9-
let AvatarEditor = require('react-avatar-editor');
10-
11-
if ('default' in AvatarEditor) {
12-
/* istanbul ignore next */ // ignores coverage for this line.
13-
AvatarEditor = AvatarEditor.default;
14-
}
15-
168
describe('The Image Cropper component', () => {
179
let props;
1810
const onImageChanged = jest.fn();
1911
const onCropCancel = jest.fn();
20-
const editor = { getImage: () => ({ toDataURL: () => 'imageData' }) };
12+
const mockEditor = { getImageScaledToCanvas: () => ({ toDataURL: () => 'imageData' }) };
2113

2214
beforeEach(() => {
2315
onImageChanged.mockReset();
@@ -31,7 +23,7 @@ describe('The Image Cropper component', () => {
3123
const createWrapper = () => {
3224
const wrapper = shallow(<ImageCropper {...props} />);
3325
// @ts-ignore
34-
wrapper.instance()._editor = editor;
26+
wrapper.instance()._editor = mockEditor;
3527
return wrapper;
3628
};
3729

src/components/image_cropper.tsx

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
// Copyright (c) 2018 Ultimaker B.V.
22
import * as React from 'react';
3-
3+
import debounce from 'lodash.debounce';
4+
import AvatarEditor from 'react-avatar-editor';
45
import { ImageShape } from './image';
56
import RangeSlider from './range_slider';
67
import CloseButton from './close_button';
78

8-
// needs to be imported this way to keep jest happy
9-
let AvatarEditor = require('react-avatar-editor');
10-
11-
if ('default' in AvatarEditor) {
12-
/* istanbul ignore next */ // ignores coverage for this line.
13-
AvatarEditor = AvatarEditor.default;
14-
}
15-
16-
const debounce = require('lodash.debounce');
17-
189
export interface ImageCropperProps {
1910
/** Size of the image. Include size unit */
2011
size?: string;
@@ -38,10 +29,10 @@ export interface ImageCropperProps {
3829
imageURL?: string;
3930

4031
/** Callback for when the image is changed */
41-
onImageChanged: (data: string) => any;
32+
onImageChanged: (data: string) => void;
4233

4334
/** If given, the user is allowed to cancel cropping. This callback is then called. */
44-
onCropCancel?: () => any;
35+
onCropCancel?: () => void;
4536
}
4637

4738
export interface ImageCropperState {
@@ -81,7 +72,7 @@ export class ImageCropper extends React.Component<ImageCropperProps, ImageCroppe
8172
*/
8273
_onImageChanged = debounce(() => {
8374
const { onImageChanged } = this.props;
84-
const canvas = this._editor.getImage();
75+
const canvas = this._editor.getImageScaledToCanvas();
8576
const imageData = canvas.toDataURL();
8677
onImageChanged(imageData);
8778
}, 100);

0 commit comments

Comments
 (0)