-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e1b677
commit ea7a1cb
Showing
18 changed files
with
3,946 additions
and
3,007 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
24.0.6 | ||
24.0.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
draw.io file sizing script | ||
|
||
Takes any draw.io file or mx model as input and resizes embedded PNG and JPEG images according to passed in parameters. | ||
|
||
Installing | ||
|
||
Run 'npm install' in this folder. Ensure you have node locally. | ||
|
||
Running | ||
|
||
To resize all images to 200 width: | ||
|
||
node drawImageResize.js --file=path/to/your/file.drawio --width=200 | ||
|
||
To resize all images to 40% their current width: | ||
|
||
node drawImageResize.js --file=path/to/your/file.drawio --percentage=40 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
const fs = require('fs'); | ||
const sharp = require('sharp'); | ||
const yargs = require('yargs/yargs'); | ||
const { hideBin } = require('yargs/helpers'); | ||
const sizeOf = require('image-size'); | ||
|
||
const argv = yargs(hideBin(process.argv)).options({ | ||
file: { type: 'string', demandOption: true, describe: 'The path to the .drawio file' }, | ||
percentage: { type: 'number', demandOption: false, describe: 'The percentage to resize the images to' }, | ||
width: { type: 'number', demandOption: false, describe: 'The width to resize the images to' } | ||
}).argv; | ||
|
||
const resizeImage = async (base64Image, percentage, width) => | ||
{ | ||
console.log(`Resizing image...`); | ||
// Adjust the regex to match the new end character ";" instead of "," | ||
const matches = base64Image.match(/^data:image\/(jpeg|png),(.*);$/); | ||
if (!matches) return null; | ||
|
||
const imageBuffer = Buffer.from(matches[2], 'base64'); | ||
const dimensions = sizeOf(imageBuffer); | ||
|
||
console.log(`width = ${width}`); | ||
console.log(`percentage = ${percentage}`); | ||
|
||
if (percentage && !width) | ||
{ | ||
// Width isn't passed in, use percentage | ||
width = Math.floor(dimensions.width * (percentage / 100)); | ||
console.log(`dimensions.width = ${dimensions.width}`); | ||
} | ||
console.log(`width = ${width}`); | ||
return sharp(imageBuffer) | ||
.resize({ width: width, withoutEnlargement: true }) | ||
.toBuffer() | ||
.then(resizedBuffer => | ||
{ | ||
console.log(`Image resized to ${percentage}% of its original size.`); | ||
// Ensure to append ";" at the end after re-encoding to base64 | ||
return `data:image/${matches[1]},` + resizedBuffer.toString('base64') + ';'; | ||
}); | ||
}; | ||
|
||
const processDrawioFile = async (filePath, percentage, width) => | ||
{ | ||
console.log(`Starting processing of ${filePath}`); | ||
|
||
if (!(percentage || width)) | ||
{ | ||
console.log(`You must pass in one of percentage or width`); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
let data = fs.readFileSync(filePath, { encoding: 'utf-8' }); | ||
// Adjust the regex pattern to expect ";" as the closing character of the base64 data | ||
const base64Pattern = /data:image\/(?:jpeg|png),[^;]+;/g; | ||
const images = [...data.matchAll(base64Pattern)].map(match => match[0]); | ||
|
||
console.log(`Found ${images.length} images to process.`); | ||
|
||
for (let i = 0; i < images.length; i++) | ||
{ | ||
console.log(`Processing image ${i + 1} of ${images.length}...`); | ||
const newBase64 = await resizeImage(images[i], percentage, width); | ||
|
||
if (newBase64) | ||
{ | ||
data = data.replace(images[i], newBase64); | ||
} | ||
} | ||
|
||
fs.writeFileSync(filePath, data, { encoding: 'utf-8' }); | ||
console.log(`All images processed. Updated file saved.`); | ||
} | ||
catch (error) | ||
{ | ||
console.error(`Error processing file: ${error.message}`); | ||
} | ||
}; | ||
|
||
processDrawioFile(argv.file, argv.percentage, argv.width); | ||
|
Oops, something went wrong.