fix: rebase product lock scale composite on source
This commit is contained in:
parent
67310e9ae5
commit
e67aab87c5
@ -5866,6 +5866,9 @@ export async function POST(request: NextRequest) {
|
||||
scaleAdjustPatchReference?.productLockOverlayUrl as string,
|
||||
protectMaskImageUrl:
|
||||
scaleAdjustPatchReference?.productLockProtectMaskUrl || undefined,
|
||||
repairMaskImageUrl:
|
||||
scaleAdjustPatchReference?.compositeClipMaskUrl || undefined,
|
||||
sourceImageUrl,
|
||||
sourceHeight,
|
||||
sourceWidth,
|
||||
}
|
||||
|
||||
@ -74,6 +74,8 @@ export interface CanvasScaleAdjustPatchCompositeConfig {
|
||||
export interface CanvasScaleAdjustProductLockCompositeConfig {
|
||||
overlayImageUrl: string;
|
||||
protectMaskImageUrl?: string;
|
||||
repairMaskImageUrl?: string;
|
||||
sourceImageUrl?: string;
|
||||
sourceHeight: number;
|
||||
sourceWidth: number;
|
||||
}
|
||||
@ -566,6 +568,8 @@ function normalizeScaleAdjustProductLockCompositeConfig(
|
||||
const overlayImageUrl = String(value.overlayImageUrl || "").trim();
|
||||
if (!/^https?:\/\//i.test(overlayImageUrl)) return null;
|
||||
const protectMaskImageUrl = String(value.protectMaskImageUrl || "").trim();
|
||||
const repairMaskImageUrl = String(value.repairMaskImageUrl || "").trim();
|
||||
const sourceImageUrl = String(value.sourceImageUrl || "").trim();
|
||||
const sourceWidth = Math.round(Number(value.sourceWidth || 0));
|
||||
const sourceHeight = Math.round(Number(value.sourceHeight || 0));
|
||||
if (sourceWidth <= 0 || sourceHeight <= 0) return null;
|
||||
@ -575,6 +579,12 @@ function normalizeScaleAdjustProductLockCompositeConfig(
|
||||
...(protectMaskImageUrl && /^https?:\/\//i.test(protectMaskImageUrl)
|
||||
? { protectMaskImageUrl }
|
||||
: {}),
|
||||
...(repairMaskImageUrl && /^https?:\/\//i.test(repairMaskImageUrl)
|
||||
? { repairMaskImageUrl }
|
||||
: {}),
|
||||
...(sourceImageUrl && /^https?:\/\//i.test(sourceImageUrl)
|
||||
? { sourceImageUrl }
|
||||
: {}),
|
||||
sourceHeight,
|
||||
sourceWidth,
|
||||
};
|
||||
@ -3283,6 +3293,18 @@ async function applyScaleAdjustProductLockCompositeToImages(params: {
|
||||
const protectMaskBuffer = composite.protectMaskImageUrl
|
||||
? await fetchImageBufferFromUrl(composite.protectMaskImageUrl)
|
||||
: null;
|
||||
const repairMaskBuffer = composite.repairMaskImageUrl
|
||||
? await fetchImageBufferFromUrl(composite.repairMaskImageUrl)
|
||||
: null;
|
||||
const sourceBaseBuffer = composite.sourceImageUrl
|
||||
? await sharp(await fetchImageBufferFromUrl(composite.sourceImageUrl), {
|
||||
failOn: "none",
|
||||
})
|
||||
.rotate()
|
||||
.ensureAlpha()
|
||||
.png({ compressionLevel: 6 })
|
||||
.toBuffer()
|
||||
: null;
|
||||
const overlayMetadata = await sharp(overlayBuffer, { failOn: "none" }).metadata();
|
||||
const overlayWidth = Math.round(Number(overlayMetadata.width || 0));
|
||||
const overlayHeight = Math.round(Number(overlayMetadata.height || 0));
|
||||
@ -3324,10 +3346,42 @@ async function applyScaleAdjustProductLockCompositeToImages(params: {
|
||||
width: generatedWidth,
|
||||
})
|
||||
: resizedOverlayBaseBuffer;
|
||||
const mergedPngBuffer = await sharp(normalizedGeneratedBuffer, {
|
||||
const baseForProductLock = sourceBaseBuffer && repairMaskBuffer
|
||||
? await sharp(sourceBaseBuffer, { failOn: "none" })
|
||||
.resize(generatedWidth, generatedHeight, {
|
||||
fit: "fill",
|
||||
kernel: sharp.kernel.lanczos3,
|
||||
})
|
||||
.ensureAlpha()
|
||||
.png({ compressionLevel: 6 })
|
||||
.toBuffer()
|
||||
: normalizedGeneratedBuffer;
|
||||
const generatedRepairOverlay =
|
||||
sourceBaseBuffer && repairMaskBuffer
|
||||
? await applyRawAlphaToImage({
|
||||
alphaMaskRaw: await sharp(repairMaskBuffer, { failOn: "none" })
|
||||
.resize(generatedWidth, generatedHeight, { fit: "fill" })
|
||||
.removeAlpha()
|
||||
.extractChannel(0)
|
||||
.blur(Math.max(4, Math.round(Math.min(generatedWidth, generatedHeight) * 0.006)))
|
||||
.raw()
|
||||
.toBuffer(),
|
||||
height: generatedHeight,
|
||||
imageBuffer: normalizedGeneratedBuffer,
|
||||
width: generatedWidth,
|
||||
})
|
||||
: null;
|
||||
const mergedPngBuffer = await sharp(baseForProductLock, {
|
||||
failOn: "none",
|
||||
})
|
||||
.composite([{ input: resizedOverlayBuffer, left: 0, top: 0 }])
|
||||
.composite(
|
||||
generatedRepairOverlay
|
||||
? [
|
||||
{ input: generatedRepairOverlay, left: 0, top: 0 },
|
||||
{ input: resizedOverlayBuffer, left: 0, top: 0 },
|
||||
]
|
||||
: [{ input: resizedOverlayBuffer, left: 0, top: 0 }]
|
||||
)
|
||||
.png({ compressionLevel: 6 })
|
||||
.toBuffer();
|
||||
const mergedBuffer = await sharp(mergedPngBuffer, { failOn: "none" })
|
||||
@ -3364,6 +3418,8 @@ async function applyScaleAdjustProductLockCompositeToImages(params: {
|
||||
overlayHeight,
|
||||
overlayWidth,
|
||||
protectMaskApplied: Boolean(protectMaskBuffer),
|
||||
repairMaskApplied: Boolean(repairMaskBuffer && sourceBaseBuffer),
|
||||
sourceBaseApplied: Boolean(sourceBaseBuffer),
|
||||
sourceHeight: composite.sourceHeight,
|
||||
sourceWidth: composite.sourceWidth,
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user