|
@@ -28,9 +28,7 @@ export const imageRotate = async (
|
|
|
const ctx = canvas.getContext("2d")!;
|
|
|
|
|
|
const center =
|
|
|
- direction === "row"
|
|
|
- ? [img.width / 2, height / 2]
|
|
|
- : [img.height / 2, width / 2];
|
|
|
+ direction === "row" ? [img.width / 2, height / 2] : [img.height / 2, width / 2];
|
|
|
ctx.translate(center[0], center[1]);
|
|
|
ctx.rotate(((direction === "row" ? -1 : 1) * Math.PI) / 2);
|
|
|
ctx.translate(-center[0], -center[1]);
|
|
@@ -41,13 +39,29 @@ export const imageRotate = async (
|
|
|
);
|
|
|
};
|
|
|
|
|
|
-export const fixImageSize = async (blob: Blob, max: number, min: number) => {
|
|
|
+export const fixImageSize = async (
|
|
|
+ blob: Blob,
|
|
|
+ max: number,
|
|
|
+ min: number,
|
|
|
+ scale = true
|
|
|
+) => {
|
|
|
const img = new Image();
|
|
|
img.src = URL.createObjectURL(blob);
|
|
|
await new Promise((resolve) => (img.onload = resolve));
|
|
|
|
|
|
let width = img.width;
|
|
|
let height = img.height;
|
|
|
+ if (!scale) {
|
|
|
+ const diff = max - min;
|
|
|
+ if (width > max) {
|
|
|
+ max = width;
|
|
|
+ min = max - diff;
|
|
|
+ } else if (height > max) {
|
|
|
+ max = height;
|
|
|
+ min = max - diff;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
if (width > max) {
|
|
|
height = (height / width) * max;
|
|
|
width = max;
|
|
@@ -57,6 +71,7 @@ export const fixImageSize = async (blob: Blob, max: number, min: number) => {
|
|
|
}
|
|
|
let size = width > height ? width : height;
|
|
|
size = size > min ? size : min;
|
|
|
+
|
|
|
const $canvas = document.createElement("canvas");
|
|
|
$canvas.width = size;
|
|
|
$canvas.height = size;
|