Skip to content

Commit

Permalink
Merge pull request #108 from TonyBrobston/scale-image-by
Browse files Browse the repository at this point in the history
Scale image by
  • Loading branch information
TonyBrobston authored Jul 8, 2019
2 parents 52c5219 + e8d077f commit a6772ae
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module.exports = {
* `maxWidth`: the max width in pixels of the compressed output File, defaults to `image's original width`.
* `quality`: degrades quality of File, values is between `0.01` and `1.00`, defaults to `0.50`.
* `returnOriginalOnFailure`: a boolean that determines if `jpegasus` should return the original `File` on failure or if it should throw and the let the consumer handle the failure, defaults to `true`.
* `scaleImageBy`: a decimal to scale up or down the image width and height by, defaults to `1.00`.

## Contributors
Thanks goes to these wonderful people:
Expand Down
1 change: 1 addition & 0 deletions src/services/optionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const override = (inputOptions: Options): Options => {
allowCrossOriginResourceSharing: false,
quality: 0.5,
returnOriginalOnFailure: true,
scaleImageBy: 1.00,
...inputOptions,
};
};
Expand Down
25 changes: 12 additions & 13 deletions src/services/scaleService.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import {Options} from '../types/Options';
import canvasService from './canvasService';
import imageService from './imageService';

import {Options} from '../types/Options';
const determineScale = ({height, width}: HTMLImageElement, {maxHeight, maxWidth, scaleImageBy}: Options): number => {
if (scaleImageBy) {
const scaledHeight = scaleImageBy * height;
const scaledWidth = scaleImageBy * width;
const heightIsGreaterThanWidth = height > width;

const determineScale = (image: HTMLImageElement, options: Options): number => {
const height = image.height;
const width = image.width;
const maxHeight = options.maxHeight;
const maxWidth = options.maxWidth;
const heightCanBeScaled = maxHeight && height > maxHeight;
const heightIsLargest = height > width;
const widthCanBeScaled = maxWidth && width > maxWidth;
if (heightIsGreaterThanWidth && maxHeight && (scaledHeight > maxHeight)) {
return maxHeight / height;
} else if (maxWidth && (scaledWidth > maxWidth)) {
return maxWidth / width;
}

if (maxHeight && heightCanBeScaled && heightIsLargest) {
return maxHeight / height;
} else if (maxWidth && widthCanBeScaled) {
return maxWidth / width;
return scaleImageBy;
}

return 1.00;
Expand Down
1 change: 1 addition & 0 deletions src/types/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface Options {
maxHeight?: number;
maxWidth?: number;
returnOriginalOnFailure?: boolean;
scaleImageBy?: number;
}
1 change: 1 addition & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('index', () => {
maxWidth: 1000,
quality: 0.05,
returnOriginalOnFailure: true,
scaleImageBy: 1.00,
};
const canvas = document.createElement('canvas');
fileService.validate = jest.fn(() => true);
Expand Down
4 changes: 4 additions & 0 deletions tests/services/optionService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('optionService', () => {
allowCrossOriginResourceSharing: false,
quality: 0.5,
returnOriginalOnFailure: true,
scaleImageBy: 1.00,
},
inputOptions: {},
name: 'should not override any inputOptions',
Expand All @@ -18,13 +19,15 @@ describe('optionService', () => {
maxWidth: 4,
quality: 0.75,
returnOriginalOnFailure: false,
scaleImageBy: 0.49,
},
inputOptions: {
allowCrossOriginResourceSharing: true,
maxHeight: 5,
maxWidth: 4,
quality: 0.75,
returnOriginalOnFailure: false,
scaleImageBy: 0.49,
},
name: 'should override all inputOptions',
},
Expand All @@ -33,6 +36,7 @@ describe('optionService', () => {
allowCrossOriginResourceSharing: true,
quality: 0.5,
returnOriginalOnFailure: true,
scaleImageBy: 1.00,
},
inputOptions: {
allowCrossOriginResourceSharing: true,
Expand Down
95 changes: 87 additions & 8 deletions tests/services/scaleService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Chance} from 'chance';
import canvasService from '../../src/services/canvasService';
import imageService from '../../src/services/imageService';
import scaleService from '../../src/services/scaleService';
import {Options} from '../../src/types/Options';

jest.mock('../../src/services/imageService');
jest.mock('../../src/services/canvasService');
Expand All @@ -20,18 +21,31 @@ describe('scaleService', () => {
height: chance.natural(),
width: chance.natural(),
},
inputOptions: {},
inputOptions: {
} as Options,
name: 'no inputOptions',
scale: 1.00,
},
{
image: {
height: chance.natural(),
width: chance.natural(),
},
inputOptions: {
scaleImageBy: 1.00,
} as Options,
name: 'default scaleImageBy inputOptions',
scale: 1.00,
},
{
image: {
height: 4000,
width: 3000,
},
inputOptions: {
maxHeight: 1200,
},
scaleImageBy: 1.00,
} as Options,
name: 'maxHeight scale',
scale: 0.30,
},
Expand All @@ -42,7 +56,8 @@ describe('scaleService', () => {
},
inputOptions: {
maxWidth: 1200,
},
scaleImageBy: 1.00,
} as Options,
name: 'maxWidth scale',
scale: 0.40,
},
Expand All @@ -53,7 +68,8 @@ describe('scaleService', () => {
},
inputOptions: {
maxHeight: 1200,
},
scaleImageBy: 1.00,
} as Options,
name: 'no scale, height < maxHeight',
scale: 1.00,
},
Expand All @@ -64,7 +80,8 @@ describe('scaleService', () => {
},
inputOptions: {
maxWidth: 1200,
},
scaleImageBy: 1.00,
} as Options,
name: 'no scale, width < maxWidth',
scale: 1.00,
},
Expand All @@ -76,7 +93,8 @@ describe('scaleService', () => {
inputOptions: {
maxHeight: 600,
maxWidth: 600,
},
scaleImageBy: 1.00,
} as Options,
name: 'maxHeight scale, width < height',
scale: 0.50,
},
Expand All @@ -88,18 +106,79 @@ describe('scaleService', () => {
inputOptions: {
maxHeight: 600,
maxWidth: 600,
},
scaleImageBy: 1.00,
} as Options,
name: 'maxWidth scale, height < width',
scale: 0.50,
},
{
image: {
height: 400,
scaleImageBy: 1.00,
width: 400,
},
inputOptions: {
scaleImageBy: 0.5,
} as Options,
name: 'scale dimensions by',
scale: 0.5,
},
{
image: {
height: 1000,
width: 500,
},
inputOptions: {
maxHeight: 1200,
scaleImageBy: 2.00,
} as Options,
name: 'scale dimensions by, limit to maxHeight',
scale: 1.2,
},
{
image: {
height: 500,
width: 1000,
},
inputOptions: {
maxWidth: 1200,
scaleImageBy: 2.00,
} as Options,
name: 'scale dimensions by, limit to maxWidth',
scale: 1.2,
},
{
image: {
height: 400,
width: 400,
},
inputOptions: {
maxHeight: 900,
scaleImageBy: 0.5,
} as Options,
name: 'scale dimensions by, do not limit to maxHeight',
scale: 0.5,
},
{
image: {
height: 400,
width: 400,
},
inputOptions: {
maxWidth: 900,
scaleImageBy: 0.5,
} as Options,
name: 'scale dimensions by, do not limit to maxWidth',
scale: 0.5,
},
];

scenarios.forEach((scenario: {
image: {
height: number,
width: number,
},
inputOptions: {},
inputOptions: Options,
name: string,
scale: number,
}) => {
Expand Down

0 comments on commit a6772ae

Please sign in to comment.