mirror of
https://github.com/basketikun/infinite-canvas.git
synced 2026-08-02 06:51:14 +08:00
feat(version): bump version to 0.2.0 and update image ratio parsing functions
This commit is contained in:
@@ -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;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user