0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2024-12-22 05:33:02 -05:00
penpot-exporter-figma-plugin/ui-src/parser/parseImage.ts
Jordi Sala Morales 3094f05e98
Optimize images before generating zip file (#141)
* Optimize images before generating zip file

* add changelog

* refactor

* fix lint
2024-06-05 12:36:49 +02:00

53 lines
1.4 KiB
TypeScript

import { ImageColor } from '@ui/lib/types/utils/imageColor';
import { detectMimeType } from '@ui/utils';
const IMAGE_QUALITY = 0.8;
export const parseImage = async (bytes: Uint8Array): Promise<ImageColor> => {
const image = await extractFromBytes(bytes);
return {
width: image.width,
height: image.height,
dataUri: image.dataURL,
keepAspectRatio: true,
id: '00000000-0000-0000-0000-000000000000'
};
};
async function extractFromBytes(bytes: Uint8Array) {
const mymeType = detectMimeType(bytes);
const url = URL.createObjectURL(new Blob([bytes]));
const image = await new Promise<HTMLImageElement>((resolve, reject) => {
const img = new Image();
img.onload = () => resolve(img);
img.onerror = () => reject();
img.src = url;
});
const canvas = new OffscreenCanvas(image.width, image.height);
const context = canvas.getContext('2d');
if (!context) {
throw new Error('Could not create canvas context');
}
context.drawImage(image, 0, 0);
const dataURL = await canvas
.convertToBlob({ type: mymeType, quality: IMAGE_QUALITY })
.then(blob => {
return new Promise<string>(resolve => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result as string);
reader.readAsDataURL(blob);
});
});
return {
dataURL,
width: image.width,
height: image.height
};
}