Skip to content

Commit

Permalink
Merge pull request #9471 from yashpalgoyal1304/set-width-image-minimizer
Browse files Browse the repository at this point in the history
Set image-minimizer to specify image width ...
  • Loading branch information
TobiGr authored Nov 28, 2022
2 parents 60671c9 + e9ef945 commit 2984649
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/image-minimizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module.exports = async ({github, context}) => {
return match;
}

let probeAspectRatio = 0;
let shouldModify = false;
try {
console.log(`Probing ${g2}`);
Expand All @@ -76,7 +77,8 @@ module.exports = async ({github, context}) => {
}
console.log(`Probing resulted in ${probeResult.width}x${probeResult.height}px`);

shouldModify = probeResult.height > IMG_MAX_HEIGHT_PX && (probeResult.width / probeResult.height) < MIN_ASPECT_RATIO;
probeAspectRatio = probeResult.width / probeResult.height;
shouldModify = probeResult.height > IMG_MAX_HEIGHT_PX && probeAspectRatio < MIN_ASPECT_RATIO;
} catch(e) {
console.log('Probing failed:', e);
// Immediately abort
Expand All @@ -86,7 +88,7 @@ module.exports = async ({github, context}) => {
if (shouldModify) {
wasMatchModified = true;
console.log(`Modifying match '${match}'`);
return `<img alt="${g1}" src="${g2}" height=${IMG_MAX_HEIGHT_PX} />`;
return `<img alt="${g1}" src="${g2}" width=${Math.min(600, (IMG_MAX_HEIGHT_PX * probeAspectRatio).toFixed(0))} />`;
}

console.log(`Match '${match}' is ok/will not be modified`);
Expand Down

0 comments on commit 2984649

Please sign in to comment.