Skip to content

Commit

Permalink
fix: export images with webhook icon
Browse files Browse the repository at this point in the history
deps: drop canvg

Closes #4127
  • Loading branch information
barmac committed Sep 30, 2024
1 parent 6a5fbaa commit a0d8096
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 108 deletions.
1 change: 0 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"camunda-cmmn-moddle": "^1.0.0",
"camunda-dmn-js": "^2.9.1",
"camunda-dmn-moddle": "^1.3.0",
"canvg": "^4.0.2",
"classnames": "^2.5.1",
"cmmn-js": "^0.20.0",
"cmmn-js-properties-panel": "^0.9.0",
Expand Down
29 changes: 23 additions & 6 deletions client/src/app/util/__tests__/generateImageSpec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions client/src/app/util/__tests__/webhook.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 38 additions & 32 deletions client/src/app/util/generateImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,63 @@
* except in compliance with the MIT License.
*/

import { Canvg } from 'canvg';

// list of defined encodings
const ENCODINGS = [
'image/png',
'image/jpeg'
];

const INITIAL_SCALE = 3;
const FINAL_SCALE = 1;
const SCALE_STEP = 1;

const DATA_URL_REGEX = /^data:((?:\w+\/(?:(?!;).)+)?)((?:;[\w\W]*?[^;])*),(.+)$/;


export default async function generateImage(type, svg) {
const encoding = 'image/' + type;

if (ENCODINGS.indexOf(encoding) === -1) {
throw new Error('<' + type + '> is an unknown type for converting svg to image');
}

const initialSVG = svg;

let dataURL = '';

for (let scale = INITIAL_SCALE; scale >= FINAL_SCALE; scale -= SCALE_STEP) {
const dataUrl = asDataURL(svg);
const image = await loadImage(dataUrl);
const {
height,
width
} = getSize(svg);

let canvas = document.createElement('canvas');
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;

svg = initialSVG.replace(/width="([^"]+)" height="([^"]+)"/, function(_, widthStr, heightStr) {
return `width="${parseInt(widthStr, 10) * scale}" height="${parseInt(heightStr, 10) * scale}"`;
});
const context = canvas.getContext('2d');
context.drawImage(image, 0, 0, width, height);

const context = canvas.getContext('2d');
// make the background white for every format
context.globalCompositeOperation = 'destination-over';
context.fillStyle = 'white';
context.fillRect(0, 0, canvas.width, canvas.height);

const canvg = Canvg.fromString(context, svg);
await canvg.render();

// make the background white for every format
context.globalCompositeOperation = 'destination-over';
context.fillStyle = 'white';
return canvas.toDataURL(encoding, 1.0);
}

context.fillRect(0, 0, canvas.width, canvas.height);
function asDataURL(svg) {
return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`;
}

dataURL = canvas.toDataURL(encoding);
async function loadImage(url) {
const img = new Image();
img.src = url;
return new Promise((resolve, reject) => {
img.onload = () => resolve(img);
img.onerror = () => {
const error = new Error('Error happened generating image.');

reject(error);
};
img.src = url;
});
}

if (DATA_URL_REGEX.test(dataURL)) {
return dataURL;
}
}
function getSize(svg) {
const match = /width="(?<width>[^"]+)" height="(?<height>[^"]+)"/.exec(svg);
const width = parseInt(match.groups.width, 10),
height = parseInt(match.groups.height, 10);

throw new Error('Error happened generating image. Diagram size is too big.');
return { width, height };
}
74 changes: 5 additions & 69 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a0d8096

Please sign in to comment.