Skip to content

Commit

Permalink
Fix for proper resizing if the filename or path contains letter x wit…
Browse files Browse the repository at this point in the history
…h digits surrounding it eg: 8x8
  • Loading branch information
Arpee Ong committed Jul 27, 2019
1 parent ec97cac commit 7dbf592
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
11 changes: 6 additions & 5 deletions source/image-handler/test/test-thumbor-mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ describe('process()', function() {
thumborMapping.process(event);
// Assert
const expectedResult = {
edits: {
edits: {
grayscale: true,
resize: {
width: 200,
height: 300
},
grayscale: true
height: 300,
fit: 'inside'
}
}
};
assert.deepEqual(thumborMapping.edits, expectedResult.edits);
Expand Down Expand Up @@ -628,7 +629,7 @@ describe('mapFilter()', function() {
thumborMapping.mapFilter(edit, filetype);
// Assert
const expectedResult = {
edits: {
edits: {
rotate: 0
}
};
Expand Down
28 changes: 19 additions & 9 deletions source/image-handler/thumbor-mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,31 @@ class ThumborMapping {
this.path = event.path;
const edits = this.path.split('/');
const filetype = (this.path.split('.'))[(this.path.split('.')).length - 1];

//Process the Dimenions
const dimPath = this.path.match(/\d+x\d+/g);
if (dimPath) {
const dims = dimPath[0].split('x');
// Set only if the dimensions provided are valid
if (isNaN(dims[0]) == false && isNaN(dims[1]) == false ){
this.edits.resize = {};
// Assign dimenions from the first match only to avoid parsing dimension from image file names
this.edits.resize.width = Number(dims[0]);
this.edits.resize.height = Number(dims[1]);

}
}

// Parse the image path
for (let i = 0; i < edits.length; i++) {
const edit = edits[i];
if (edit === ('fit-in')) {
this.edits.resize = {};
if (this.edits.resize === undefined) {
this.edits.resize = {};
}
this.edits.resize.fit = 'inside'
this.sizingMethod = edit;
}
else if (edit.includes('x')) {
this.edits.resize = {};
const dims = edit.split('x');
this.edits.resize.width = Number(dims[0]);
this.edits.resize.height = Number(dims[1]);
}
if (edit.includes('filters:')) {
} else if (edit.includes('filters:')) {
this.mapFilter(edit, filetype);
}
}
Expand Down

0 comments on commit 7dbf592

Please sign in to comment.