From 925ab9e3c2f3c43abef3ee2b3639bd633677c880 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Tue, 15 Feb 2022 14:40:51 -0800 Subject: [PATCH] Restrict image export size. --- .../src/components/export/BinaryImageExporter.ts | 16 +++++++++++----- .../components/export/ImageExporterFactory.ts | 7 +++---- .../action-dispatcher/export-dialog/index.tsx | 2 +- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/mindplot/src/components/export/BinaryImageExporter.ts b/packages/mindplot/src/components/export/BinaryImageExporter.ts index 7aaf0980..ae1bd704 100644 --- a/packages/mindplot/src/components/export/BinaryImageExporter.ts +++ b/packages/mindplot/src/components/export/BinaryImageExporter.ts @@ -15,16 +15,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { Mindmap } from '../..'; import Exporter from './Exporter'; import SVGExporter from './SVGExporter'; /** * Based on https://mybyways.com/blog/convert-svg-to-png-using-your-browser */ class BinaryImageExporter extends Exporter { - private svgElement: Element; + private static maxAllowedSize = 10000; - private mindmap: Mindmap; + private svgElement: Element; private width: number; @@ -32,10 +31,9 @@ class BinaryImageExporter extends Exporter { private adjustToFit: boolean; - constructor(mindmap: Mindmap, svgElement: Element, width: number, height: number, imgFormat: 'image/png' | 'image/jpeg', adjustToFit = true) { + constructor(svgElement: Element, width: number, height: number, imgFormat: 'image/png' | 'image/jpeg', adjustToFit = true) { super(imgFormat.split('/')[0], imgFormat); this.svgElement = svgElement; - this.mindmap = mindmap; this.adjustToFit = adjustToFit; this.width = width; this.height = height; @@ -66,6 +64,14 @@ class BinaryImageExporter extends Exporter { width = (this.width * dpr); height = (this.height * dpr); } + + // Prevents an image too big. Failures seen during export in couple of browsers + if (Math.max(width, height) > BinaryImageExporter.maxAllowedSize) { + const scale = Math.max(width, height) / BinaryImageExporter.maxAllowedSize; + width /= scale; + height /= scale; + } + canvas.setAttribute('width', width.toFixed(0)); canvas.setAttribute('height', height.toFixed(0)); diff --git a/packages/mindplot/src/components/export/ImageExporterFactory.ts b/packages/mindplot/src/components/export/ImageExporterFactory.ts index 697228c2..54111437 100644 --- a/packages/mindplot/src/components/export/ImageExporterFactory.ts +++ b/packages/mindplot/src/components/export/ImageExporterFactory.ts @@ -15,14 +15,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { Mindmap } from '../..'; import BinaryImageExporter from './BinaryImageExporter'; import Exporter from './Exporter'; import SVGExporter from './SVGExporter'; type imageType = 'svg' | 'png' | 'jpg'; class ImageExpoterFactory { - static create(type: imageType, mindmap: Mindmap, svgElement: Element, width: number, height: number, adjustToFit = true): Exporter { + static create(type: imageType, svgElement: Element, width: number, height: number, adjustToFit = true): Exporter { let result: Exporter; switch (type) { case 'svg': { @@ -30,11 +29,11 @@ class ImageExpoterFactory { break; } case 'png': { - result = new BinaryImageExporter(mindmap, svgElement, width, height, 'image/png', adjustToFit); + result = new BinaryImageExporter(svgElement, width, height, 'image/png', adjustToFit); break; } case 'jpg': { - result = new BinaryImageExporter(mindmap, svgElement, width, height, 'image/jpeg', adjustToFit); + result = new BinaryImageExporter(svgElement, width, height, 'image/jpeg', adjustToFit); break; } default: diff --git a/packages/webapp/src/components/maps-page/action-dispatcher/export-dialog/index.tsx b/packages/webapp/src/components/maps-page/action-dispatcher/export-dialog/index.tsx index 45a6090a..93695c98 100644 --- a/packages/webapp/src/components/maps-page/action-dispatcher/export-dialog/index.tsx +++ b/packages/webapp/src/components/maps-page/action-dispatcher/export-dialog/index.tsx @@ -105,7 +105,7 @@ const ExportDialog = ({ case 'png': case 'jpg': case 'svg': { - exporter = ImageExporterFactory.create(formatType, mindmap, svgElement, size.width, size.height, zoomToFit); + exporter = ImageExporterFactory.create(formatType, svgElement, size.width, size.height, zoomToFit); break; } case 'wxml':