mirror of
https://github.com/JimLiu/baoyu-skills.git
synced 2026-08-07 17:33:03 +08:00
fix(baoyu-image-gen): use standard size presets for DashScope aspect ratio mapping
Replace free-form dimension calculation with closest-match from predefined standard sizes to avoid invalid resolution errors from the API. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -22,27 +22,53 @@ function parseAspectRatio(ar: string): { width: number; height: number } | null
|
|||||||
return { width: w, height: h };
|
return { width: w, height: h };
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSizeFromAspectRatio(ar: string | null, quality: CliArgs["quality"]): string {
|
const STANDARD_SIZES: [number, number][] = [
|
||||||
const baseSize = quality === "2k" ? 1440 : 1024;
|
[1024, 1024],
|
||||||
|
[1280, 720],
|
||||||
|
[720, 1280],
|
||||||
|
[1024, 768],
|
||||||
|
[768, 1024],
|
||||||
|
[1536, 1024],
|
||||||
|
[1024, 1536],
|
||||||
|
[1536, 864],
|
||||||
|
[864, 1536],
|
||||||
|
];
|
||||||
|
|
||||||
if (!ar) return `${baseSize}*${baseSize}`;
|
const STANDARD_SIZES_2K: [number, number][] = [
|
||||||
|
[1536, 1536],
|
||||||
|
[2048, 1152],
|
||||||
|
[1152, 2048],
|
||||||
|
[1536, 1024],
|
||||||
|
[1024, 1536],
|
||||||
|
[1536, 864],
|
||||||
|
[864, 1536],
|
||||||
|
[2048, 2048],
|
||||||
|
];
|
||||||
|
|
||||||
|
function getSizeFromAspectRatio(ar: string | null, quality: CliArgs["quality"]): string {
|
||||||
|
const is2k = quality === "2k";
|
||||||
|
const defaultSize = is2k ? "1536*1536" : "1024*1024";
|
||||||
|
|
||||||
|
if (!ar) return defaultSize;
|
||||||
|
|
||||||
const parsed = parseAspectRatio(ar);
|
const parsed = parseAspectRatio(ar);
|
||||||
if (!parsed) return `${baseSize}*${baseSize}`;
|
if (!parsed) return defaultSize;
|
||||||
|
|
||||||
const ratio = parsed.width / parsed.height;
|
const targetRatio = parsed.width / parsed.height;
|
||||||
|
const sizes = is2k ? STANDARD_SIZES_2K : STANDARD_SIZES;
|
||||||
|
|
||||||
if (Math.abs(ratio - 1) < 0.1) {
|
let best = defaultSize;
|
||||||
return `${baseSize}*${baseSize}`;
|
let bestDiff = Infinity;
|
||||||
|
|
||||||
|
for (const [w, h] of sizes) {
|
||||||
|
const diff = Math.abs(w / h - targetRatio);
|
||||||
|
if (diff < bestDiff) {
|
||||||
|
bestDiff = diff;
|
||||||
|
best = `${w}*${h}`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ratio > 1) {
|
return best;
|
||||||
const w = Math.round(baseSize * ratio);
|
|
||||||
return `${w}*${baseSize}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const h = Math.round(baseSize / ratio);
|
|
||||||
return `${baseSize}*${h}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeSize(size: string): string {
|
function normalizeSize(size: string): string {
|
||||||
|
|||||||
Reference in New Issue
Block a user