in source/use_cases/aws-serverless-image-handler/lib/lambda/image-handler/thumbor-mapping.js [94:252]
mapFilter(edit, filetype) {
const matched = edit.match(/:(.+)\((.*)\)/);
const key = matched[1];
let value = matched[2];
// Find the proper filter
if (key === ('autojpg')) {
this.edits.toFormat = 'jpeg';
}
else if (key === ('background_color')) {
if (!ColorName[value]) {
value = `#${value}`
}
this.edits.flatten = { background: Color(value).object() };
}
else if (key === ('blur')) {
const val = value.split(',');
this.edits.blur = (val.length > 1) ? Number(val[1]) : Number(val[0]) / 2;
}
else if (key === ('convolution')) {
const arr = value.split(',');
const strMatrix = (arr[0]).split(';');
let matrix = [];
strMatrix.forEach(function(str) {
matrix.push(Number(str));
});
const matrixWidth = arr[1];
let matrixHeight = 0;
let counter = 0;
for (let i = 0; i < matrix.length; i++) {
if (counter === (matrixWidth - 1)) {
matrixHeight++;
counter = 0;
} else {
counter++;
}
}
this.edits.convolve = {
width: Number(matrixWidth),
height: Number(matrixHeight),
kernel: matrix
}
}
else if (key === ('equalize')) {
this.edits.normalize = "true";
}
else if (key === ('fill')) {
if (this.edits.resize === undefined) {
this.edits.resize = {};
}
if (!ColorName[value]) {
value = `#${value}`
}
this.edits.resize.background = Color(value).object();
}
else if (key === ('format')) {
const formattedValue = value.replace(/[^0-9a-z]/gi, '').replace(/jpg/i, 'jpeg');
const acceptedValues = ['heic', 'heif', 'jpeg', 'png', 'raw', 'tiff', 'webp'];
if (acceptedValues.includes(formattedValue)) {
this.edits.toFormat = formattedValue;
}
}
else if (key === ('grayscale')) {
this.edits.grayscale = true;
}
else if (key === ('no_upscale')) {
if (this.edits.resize === undefined) {
this.edits.resize = {};
}
this.edits.resize.withoutEnlargement = true;
}
else if (key === ('proportion')) {
if (this.edits.resize === undefined) {
this.edits.resize = {};
}
const prop = Number(value);
this.edits.resize.width = Number(this.edits.resize.width * prop);
this.edits.resize.height = Number(this.edits.resize.height * prop);
}
else if (key === ('quality')) {
if (['jpg', 'jpeg'].includes(filetype)) {
this.edits.jpeg = { quality: Number(value) }
} else if (filetype === 'png') {
this.edits.png = { quality: Number(value) }
} else if (filetype === 'webp') {
this.edits.webp = { quality: Number(value) }
} else if (filetype === 'tiff') {
this.edits.tiff = { quality: Number(value) }
} else if (filetype === 'heif') {
this.edits.heif = { quality: Number(value) }
}
}
else if (key === ('rgb')) {
const percentages = value.split(',');
const values = [];
percentages.forEach(function (percentage) {
const parsedPercentage = Number(percentage);
const val = 255 * (parsedPercentage / 100);
values.push(val);
})
this.edits.tint = { r: values[0], g: values[1], b: values[2] };
}
else if (key === ('rotate')) {
this.edits.rotate = Number(value);
}
else if (key === ('sharpen')) {
const sh = value.split(',');
const sigma = 1 + Number(sh[1]) / 2;
this.edits.sharpen = sigma;
}
else if (key === ('stretch')) {
if (this.edits.resize === undefined) {
this.edits.resize = {};
}
if (this.sizingMethod === undefined || this.sizingMethod !== 'fit-in') {
this.edits.resize.fit = "fill";
}
}
else if (key === ('strip_exif')) {
this.edits.rotate = 0;
}
else if (key === ('strip_icc')) {
this.edits.rotate = 0;
}
else if (key === ('upscale')) {
if (this.edits.resize === undefined) {
this.edits.resize = {};
}
this.edits.resize.fit = "inside"
}
else if (key === ('watermark')) {
const options = value.replace(/\s+/g, '').split(',');
const bucket = options[0];
const key = options[1];
const xPos = options[2];
const yPos = options[3];
const alpha = options[4];
const wRatio = options[5];
const hRatio = options[6];
this.edits.overlayWith = {
bucket,
key,
alpha,
wRatio,
hRatio,
options: {}
}
const allowedPosPattern = /^(100|[1-9]?[0-9]|-(100|[1-9][0-9]?))p$/;
if (allowedPosPattern.test(xPos) || !isNaN(xPos)) {
this.edits.overlayWith.options['left'] = xPos;
}
if (allowedPosPattern.test(yPos) || !isNaN(yPos)) {
this.edits.overlayWith.options['top'] = yPos;
}
}
else {
return undefined;
}
}