ensure image is square before transit

This commit is contained in:
2025-03-24 19:31:11 -04:00
parent 2b59daa21f
commit 31f9cfdc1a

View File

@@ -1,6 +1,5 @@
import { APIUtils, NoBody } from "../apiutils.ts"; import { APIUtils, NoBody } from "../apiutils.ts";
import * as BaseImages from "../data/content/baseimages.ts"; import * as BaseImages from "../data/content/baseimages.ts";
import Logging from "@proxnet/undead-logging";
import express from "express"; import express from "express";
import * as Images from "./../data/content/images.ts"; import * as Images from "./../data/content/images.ts";
import { Image } from "https://deno.land/x/imagescript@1.3.0/mod.ts"; import { Image } from "https://deno.land/x/imagescript@1.3.0/mod.ts";
@@ -63,21 +62,6 @@ route.router.get(
else height = num; else height = num;
} }
if (cropSquare) {
if (image.width > image.height) {
image.crop(
Math.round(image.width / 2) - Math.round(image.height / 2),
0,
image.height,
image.height,
);
} else {image.crop(
0,
Math.round(image.height / 2) - Math.round(image.width / 2),
image.width,
image.width,
);}
}
if (width && height) { if (width && height) {
const targetWidth = width > image.width ? image.width : width; const targetWidth = width > image.width ? image.width : width;
const targetHeight = height > image.height ? image.height : height; const targetHeight = height > image.height ? image.height : height;
@@ -100,6 +84,21 @@ route.router.get(
} }
} else if (width) image.resize(width, Image.RESIZE_AUTO); } else if (width) image.resize(width, Image.RESIZE_AUTO);
else if (height) image.resize(Image.RESIZE_AUTO, height); else if (height) image.resize(Image.RESIZE_AUTO, height);
if (cropSquare) {
if (image.width > image.height) {
image.crop(
Math.round(image.width / 2) - Math.round(image.height / 2),
0,
image.height,
image.height,
);
} else {image.crop(
0,
Math.round(image.height / 2) - Math.round(image.width / 2),
image.width,
image.width,
);}
}
rs.type("png").send(Buffer.from(await image.encode())); rs.type("png").send(Buffer.from(await image.encode()));
}, },