Skip to content

Commit

Permalink
WIP to allow setting a background color
Browse files Browse the repository at this point in the history
  • Loading branch information
tmathern committed Jan 5, 2024
1 parent e216046 commit dbfb305
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/postprocessing/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ const EXTRA_INSTRUCTIONS = new Set([
"interlace",
"dpi",
"convertToDpi",
"watermark"
"watermark",
"pdfbgcolor"
]);

const SRGB_COLOR_PROFILE_FILE = process.env.SRGB_COLOR_PROFILE_FILE;
Expand Down Expand Up @@ -191,7 +192,6 @@ function isTargetFormat(metadata, instructions) {
if (format) {
return metadata.FileType === format;
}

return false;
}

Expand Down Expand Up @@ -473,7 +473,7 @@ async function applyOperations(img, intermediateRendition, rendition, directorie
img = convertSvg(img, instructions);
} else {
// raster
img = handleTransparency(img, instructions.fmt);
img = handleTransparency(img, instructions);
}

// handle exif orientation
Expand Down Expand Up @@ -623,12 +623,10 @@ async function write(img, outfile) {
* @param {Object} directories working directories of the current activation
*/
async function imagePostProcess(intermediateRendition, rendition, directories) {

const img = await read(intermediateRendition.path);
img.setFormat(rendition.instructions.fmt);

await applyOperations(img, intermediateRendition, rendition, directories);

await applyOutputProperties(img, rendition.instructions);

await write(img, rendition.path);
Expand All @@ -639,23 +637,29 @@ function convertSvg(img, instructions) {
// Only using width because ImageMagick will use the smallest value whether it be width or height
const width = instructions.width || SVG_DEFAULT_WIDTH;

img = handleTransparency(img, instructions.fmt);
img = handleTransparency(img, instructions);

// some svgs have no size (only percentage width/height), so we scale them to our target size
img.in("-size", `${width}`); // 2020-11-10 img.rawSize() will not be applied at the correct time and the SVG will be upscaled resulting in a fuzzy final rendition
return img;
}

// gm("img.png").set(attribute, value)
function handleTransparency(img, fmt) {
if (FORMAT_SUPPORTS_TRANSPARENCY[fmt]) {
function handleTransparency(img, instructions) {
if(instructions.pdfbgcolor) {

Check failure on line 648 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.16.1)

Expected indentation of 4 spaces but found 2

Check failure on line 648 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.17)

Expected indentation of 4 spaces but found 2
// replacing transparency with a color defined in instructions
// (disregard if the format supports keeping transparency or not)
img.background(instructions.pdfbgcolor);

Check failure on line 651 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.16.1)

Expected indentation of 8 spaces but found 4

Check failure on line 651 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.17)

Expected indentation of 8 spaces but found 4
img.flatten();

Check failure on line 652 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.16.1)

Expected indentation of 8 spaces but found 4

Check failure on line 652 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.17)

Expected indentation of 8 spaces but found 4
} else {

Check failure on line 653 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.16.1)

Expected indentation of 4 spaces but found 2

Check failure on line 653 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.17)

Expected indentation of 4 spaces but found 2
if (FORMAT_SUPPORTS_TRANSPARENCY[instructions.fmt]) {

Check failure on line 654 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.16.1)

Expected indentation of 8 spaces but found 4

Check failure on line 654 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.17)

Expected indentation of 8 spaces but found 4
img.background(DEFAULT_TRANSPARENCCY_BACKGROUND); // Keep transparency

Check failure on line 655 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.16.1)

Expected indentation of 12 spaces but found 8

Check failure on line 655 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.17)

Expected indentation of 12 spaces but found 8
} else {

Check failure on line 656 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.16.1)

Expected indentation of 8 spaces but found 4

Check failure on line 656 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.17)

Expected indentation of 8 spaces but found 4
img.background(DEFAULT_BACKGROUND_COLOR); // Change tranparency background to default background color
img.background(DEFAULT_BACKGROUND_COLOR); // Change tranparency background to default background color

Check failure on line 657 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.16.1)

Expected indentation of 12 spaces but found 8

Check failure on line 657 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.17)

Expected indentation of 12 spaces but found 8
img.flatten(); // forces the image on its background to "commit" the background

Check failure on line 658 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.16.1)

Expected indentation of 12 spaces but found 8

Check failure on line 658 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.17)

Expected indentation of 12 spaces but found 8
}

Check failure on line 659 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.16.1)

Expected indentation of 8 spaces but found 4

Check failure on line 659 in lib/postprocessing/image.js

View workflow job for this annotation

GitHub Actions / build (14.17)

Expected indentation of 8 spaces but found 4

return img;
}
}

module.exports = {
Expand Down

0 comments on commit dbfb305

Please sign in to comment.