mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-10-01 23:56:39 +08:00
禁用图片压缩
This commit is contained in:
parent
d4c43acd9f
commit
7185fa76a0
82
app/utils.ts
82
app/utils.ts
@ -86,43 +86,53 @@ export async function downloadAs(text: string, filename: string) {
|
||||
export function compressImage(file: File, maxSize: number): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = (readerEvent: any) => {
|
||||
const image = new Image();
|
||||
image.onload = () => {
|
||||
let canvas = document.createElement("canvas");
|
||||
let ctx = canvas.getContext("2d");
|
||||
let width = image.width;
|
||||
let height = image.height;
|
||||
let quality = 0.9;
|
||||
let dataUrl;
|
||||
|
||||
do {
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
ctx?.clearRect(0, 0, canvas.width, canvas.height);
|
||||
ctx?.drawImage(image, 0, 0, width, height);
|
||||
dataUrl = canvas.toDataURL("image/jpeg", quality);
|
||||
|
||||
if (dataUrl.length < maxSize) break;
|
||||
|
||||
if (quality > 0.5) {
|
||||
// Prioritize quality reduction
|
||||
quality -= 0.1;
|
||||
} else {
|
||||
// Then reduce the size
|
||||
width *= 0.9;
|
||||
height *= 0.9;
|
||||
}
|
||||
} while (dataUrl.length > maxSize);
|
||||
|
||||
resolve(dataUrl);
|
||||
};
|
||||
image.onerror = reject;
|
||||
image.src = readerEvent.target.result;
|
||||
reader.onload = () => {
|
||||
// 直接解析为 Data URL
|
||||
resolve(reader.result as string);
|
||||
};
|
||||
reader.onerror = reject;
|
||||
reader.readAsDataURL(file);
|
||||
});
|
||||
|
||||
// return new Promise((resolve, reject) => {
|
||||
// const reader = new FileReader();
|
||||
// reader.onload = (readerEvent: any) => {
|
||||
// const image = new Image();
|
||||
// image.onload = () => {
|
||||
// let canvas = document.createElement("canvas");
|
||||
// let ctx = canvas.getContext("2d");
|
||||
// let width = image.width;
|
||||
// let height = image.height;
|
||||
// let quality = 0.9;
|
||||
// let dataUrl;
|
||||
//
|
||||
// do {
|
||||
// canvas.width = width;
|
||||
// canvas.height = height;
|
||||
// ctx?.clearRect(0, 0, canvas.width, canvas.height);
|
||||
// ctx?.drawImage(image, 0, 0, width, height);
|
||||
// dataUrl = canvas.toDataURL("image/jpeg", quality);
|
||||
//
|
||||
// if (dataUrl.length < maxSize) break;
|
||||
//
|
||||
// if (quality > 0.5) {
|
||||
// // Prioritize quality reduction
|
||||
// quality -= 0.1;
|
||||
// } else {
|
||||
// // Then reduce the size
|
||||
// width *= 0.9;
|
||||
// height *= 0.9;
|
||||
// }
|
||||
// } while (dataUrl.length > maxSize);
|
||||
//
|
||||
// resolve(dataUrl);
|
||||
// };
|
||||
// image.onerror = reject;
|
||||
// image.src = readerEvent.target.result;
|
||||
// };
|
||||
// reader.onerror = reject;
|
||||
// reader.readAsDataURL(file);
|
||||
// });
|
||||
}
|
||||
|
||||
export function readFromFile() {
|
||||
@ -301,7 +311,11 @@ export function isVisionModel(model: string) {
|
||||
const isGpt4Turbo =
|
||||
model.includes("gpt-4-turbo") && !model.includes("preview");
|
||||
|
||||
const isGpt4o = model === "gpt-4o";
|
||||
|
||||
return (
|
||||
visionKeywords.some((keyword) => model.includes(keyword)) || isGpt4Turbo
|
||||
visionKeywords.some((keyword) => model.includes(keyword)) ||
|
||||
isGpt4Turbo ||
|
||||
isGpt4o
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user