Skip to content

Commit

Permalink
🌅 Trim option for PDFs images (#688)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 authored Oct 20, 2023
1 parent 207b2f0 commit 5ede905
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-owls-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-cli': patch
---

Trim PDFs when converting to PNG, ensure WEBP conversion does not overwrite
12 changes: 9 additions & 3 deletions packages/myst-cli/src/transforms/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,15 @@ type ConversionFn = (
/**
* Factory function for all simple imagemagick conversions
*/
function imagemagickConvert(to: ImageExtensions, from: ImageExtensions) {
function imagemagickConvert(
to: ImageExtensions,
from: ImageExtensions,
options?: { trim?: boolean },
) {
return async (session: ISession, source: string, writeFolder: string, opts: ConversionOpts) => {
const { imagemagickAvailable } = opts;
if (imagemagickAvailable) {
return imagemagick.convert(to, from, session, source, writeFolder);
return imagemagick.convert(to, from, session, source, writeFolder, options);
}
return null;
};
Expand Down Expand Up @@ -329,7 +333,9 @@ const conversionFnLookup: Record<string, Record<string, ConversionFn>> = {
[ImageExtensions.png]: svgToPng,
},
[ImageExtensions.pdf]: {
[ImageExtensions.png]: imagemagickConvert(ImageExtensions.pdf, ImageExtensions.png),
[ImageExtensions.png]: imagemagickConvert(ImageExtensions.pdf, ImageExtensions.png, {
trim: true,
}),
},
[ImageExtensions.gif]: {
[ImageExtensions.png]: gifToPng,
Expand Down
7 changes: 5 additions & 2 deletions packages/myst-cli/src/utils/imagemagick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export async function convert(
session: ISession,
input: string,
writeFolder: string,
options?: { trim?: boolean },
) {
if (!fs.existsSync(input)) return null;
const { name, ext } = path.parse(input);
Expand All @@ -91,7 +92,9 @@ export async function convert(
session.log.debug(`Cached file found for converted ${inputFormatUpper}: ${input}`);
return filename;
} else {
const executable = `convert -density 600 -colorspace RGB ${input} ${output}`;
const executable = `convert -density 600 -colorspace RGB ${input}${
options?.trim ? ' -trim' : ''
} ${output}`;
session.log.debug(`Executing: ${executable}`);
const exec = makeExecutable(executable, createImagemagikLogger(session));
try {
Expand Down Expand Up @@ -181,7 +184,7 @@ export async function convertImageToWebp(
const convertGif = makeExecutable(`gif2webp -q ${quality} "${image}" -o "${webp}"`, debugLogger);
// Density has to be BEFORE the PDF
const convertPdfPng = makeExecutable(
`convert -density 600 -colorspace RGB ${image} ${png}`,
`convert -density 600 -colorspace RGB ${image} -trim ${png}`,
debugLogger,
);
const convertPdfWebP = makeExecutable(`cwebp -q ${quality} "${png}" -o "${webp}"`, debugLogger);
Expand Down

0 comments on commit 5ede905

Please sign in to comment.