feat: improve library preview image generation on publish (#4321)

Co-authored-by: Aakansha Doshi <aakansha1216@gmail.com>
This commit is contained in:
David Luzar 2021-11-26 11:46:13 +01:00 committed by GitHub
parent ca1f3aa094
commit b53d1f6f3e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 157 additions and 72 deletions

View file

@ -237,7 +237,11 @@ export const dataURLToFile = (dataURL: DataURL, filename = "") => {
export const resizeImageFile = async (
file: File,
maxWidthOrHeight: number,
opts: {
/** undefined indicates auto */
outputType?: typeof MIME_TYPES["jpg"];
maxWidthOrHeight: number;
},
): Promise<File> => {
// SVG files shouldn't a can't be resized
if (file.type === MIME_TYPES.svg) {
@ -257,16 +261,26 @@ export const resizeImageFile = async (
pica: pica({ features: ["js", "wasm"] }),
});
const fileType = file.type;
if (opts.outputType) {
const { outputType } = opts;
reduce._create_blob = function (env) {
return this.pica.toBlob(env.out_canvas, outputType, 0.8).then((blob) => {
env.out_blob = blob;
return env;
});
};
}
if (!isSupportedImageFile(file)) {
throw new Error(t("errors.unsupportedFileType"));
}
return new File(
[await reduce.toBlob(file, { max: maxWidthOrHeight })],
[await reduce.toBlob(file, { max: opts.maxWidthOrHeight })],
file.name,
{ type: fileType },
{
type: opts.outputType || file.type,
},
);
};