feat(version): bump version to 0.2.0 and update image ratio parsing functions

This commit is contained in:
HouYunFei
2026-07-14 17:37:51 +08:00
parent a5824f509e
commit b16263eb43
4 changed files with 16 additions and 16 deletions
+9 -4
View File
@@ -146,16 +146,21 @@ function resolveSize(quality: string | undefined, ratio: string): string {
return `${width}x${height}`;
}
function parseImageRatio(value: string) {
function parseRatioValue(value: string) {
const parts = value.split(":");
if (parts.length !== 2) throw new Error("图像尺寸格式不支持,请使用 auto、9:16 或 1024x1024");
const w = Number(parts[0]);
const h = Number(parts[1]);
if (!Number.isFinite(w) || !Number.isFinite(h) || w <= 0 || h <= 0) throw new Error("图像比例必须是正数,例如 9:16");
if (Math.max(w, h) / Math.min(w, h) > IMAGE_MAX_RATIO) throw new Error("图像宽高比不能超过 3:1,请调整尺寸");
return { width: w, height: h };
}
function parseImageRatio(value: string) {
const ratio = parseRatioValue(value);
if (Math.max(ratio.width, ratio.height) / Math.min(ratio.width, ratio.height) > IMAGE_MAX_RATIO) throw new Error("图像宽高比不能超过 3:1,请调整尺寸");
return ratio;
}
function parseImageDimensions(value: string) {
const match = value.match(/^(\d+)x(\d+)$/i);
if (!match) return null;
@@ -197,8 +202,8 @@ function closestGeminiAspectRatio(value: string) {
const ratio = parseImageRatio(value);
const target = ratio.width / ratio.height;
return GEMINI_SUPPORTED_RATIOS.reduce((best, item) => {
const current = parseImageRatio(item);
const bestRatio = parseImageRatio(best);
const current = parseRatioValue(item);
const bestRatio = parseRatioValue(best);
return Math.abs(current.width / current.height - target) < Math.abs(bestRatio.width / bestRatio.height - target) ? item : best;
});
}