123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- export const imageRotate = async (
- url: string,
- direction: "row" | "column" = "row"
- ): Promise<void | Blob> => {
- const img = new Image();
- img.src = url;
- await new Promise((resolve) => (img.onload = resolve));
- let width = img.width;
- let height = img.height;
- if (width > height) {
- if (direction === "row") {
- return;
- }
- width = img.height;
- height = img.width;
- } else {
- if (direction === "column") {
- return;
- }
- width = img.height;
- height = img.width;
- }
- const canvas = document.createElement("canvas");
- canvas.width = width;
- canvas.height = height;
- const ctx = canvas.getContext("2d")!;
- const center =
- 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]);
- ctx.drawImage(img, 0, 0);
- return await new Promise<Blob>((resolve) =>
- canvas.toBlob(resolve as any, "image/png", 1)
- );
- };
- export const loadImage = async (blob: Blob) => {
- const img = new Image();
- img.src = URL.createObjectURL(blob);
- return new Promise<HTMLImageElement>((resolve) => (img.onload = () => resolve(img)));
- };
- export const fixImageSize = async (
- blob: Blob,
- max: number,
- min: number,
- scale = true
- ) => {
- const img = await loadImage(blob);
- 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;
- } else if (height > max) {
- width = (width / height) * max;
- width = max;
- }
- let size = width > height ? width : height;
- size = size > min ? size : min;
- console.log(size, width, height);
- const $canvas = document.createElement("canvas");
- $canvas.width = size;
- $canvas.height = size;
- const ctx = $canvas.getContext("2d")!;
- ctx.rect(0, 0, size, size);
- ctx.fillStyle = "#fff";
- ctx.fill();
- ctx.drawImage(img, (size - width) / 2, (size - height) / 2, width, height);
- const newBlob = await new Promise<Blob | null>((resolve) =>
- $canvas.toBlob(resolve, "png")
- );
- return newBlob;
- };
- export const coverImageSize = async (
- blob: Blob,
- coverWidth: number,
- coverHeight: number,
- scale = true
- ) => {
- const img = await loadImage(blob);
- let width = img.width,
- useWidth;
- let height = img.height,
- useHeight;
- const proportion = coverWidth / coverHeight;
- const cProportion = width / height;
- if (cProportion > proportion) {
- useWidth = width;
- useHeight = width / proportion;
- } else if (cProportion < proportion) {
- // h偏大
- useWidth = height * proportion;
- useHeight = height;
- }
- const $canvas = document.createElement("canvas");
- $canvas.width = useWidth;
- $canvas.height = useHeight;
- const ctx = $canvas.getContext("2d")!;
- ctx.rect(0, 0, useWidth, useHeight);
- ctx.fillStyle = "#fff";
- ctx.fill();
- ctx.drawImage(img, (useWidth - width) / 2, (useHeight - height) / 2, width, height);
- const newBlob = await new Promise<Blob | null>((resolve) =>
- $canvas.toBlob(resolve, "png")
- );
- return {
- blob: newBlob!,
- width: useWidth,
- height: useHeight,
- };
- };
|