Skip to content

Commit 52fd7b4

Browse files
committed
fix download image with applied emoji when OS dosn't support emojis
1 parent 037a4dc commit 52fd7b4

File tree

1 file changed

+58
-25
lines changed

1 file changed

+58
-25
lines changed

src/components/FindFace/FindFace.js

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ const FindFace = ({ imageUrl, onUserDataChange, user }) => {
9999
console.log(emoji);
100100

101101
return emoji === undefined ? true : textWidth > 0;
102-
// return textWidth > 0;
103102
};
104103

105104
const handleDownload = () => {
@@ -122,6 +121,8 @@ const FindFace = ({ imageUrl, onUserDataChange, user }) => {
122121

123122
ctx.drawImage(img, 0, 0, img.width, img.height);
124123

124+
const imagePromises = [];
125+
125126
faceAreas.forEach((area) => {
126127
const { width, height, fontSize } = getAreaSize(img, area);
127128

@@ -134,20 +135,16 @@ const FindFace = ({ imageUrl, onUserDataChange, user }) => {
134135
blurredCanvas.width = img.width;
135136
blurredCanvas.height = img.height;
136137

137-
ctx.drawImage(img, 0, 0, img.width, img.height);
138138
blurredCtx.filter = 'blur(20px)';
139139
blurredCtx.drawImage(img, 0, 0, img.width, img.height);
140140

141-
faceAreas.forEach((area) => {
142-
const faceImageData = blurredCtx.getImageData(
143-
area.leftCol,
144-
area.topRow,
145-
width,
146-
height
147-
);
148-
149-
ctx.putImageData(faceImageData, area.leftCol, area.topRow);
150-
});
141+
const faceImageData = blurredCtx.getImageData(
142+
area.leftCol,
143+
area.topRow,
144+
width,
145+
height
146+
);
147+
ctx.putImageData(faceImageData, area.leftCol, area.topRow);
151148
}
152149

153150
if (Object.keys(emojiMap).includes(filterType)) {
@@ -162,23 +159,59 @@ const FindFace = ({ imageUrl, onUserDataChange, user }) => {
162159
area.leftCol + width / 2,
163160
area.topRow + height / 2
164161
);
162+
} else {
163+
const imgPromise = new Promise((resolve, reject) => {
164+
const filterImg = new Image();
165+
filterImg.src = `/images/${filterType}.png`;
166+
filterImg.onload = () => {
167+
const aspectRatio = filterImg.width / filterImg.height;
168+
let scaledWidth = width;
169+
let scaledHeight = height;
170+
171+
if (scaledWidth / aspectRatio > scaledHeight) {
172+
scaledHeight = scaledWidth / aspectRatio;
173+
} else {
174+
scaledWidth = scaledHeight * aspectRatio;
175+
}
176+
177+
const offsetX = area.leftCol + (width - scaledWidth) / 2;
178+
const offsetY = area.topRow + (height - scaledHeight) / 2;
179+
180+
ctx.drawImage(
181+
filterImg,
182+
offsetX,
183+
offsetY,
184+
scaledWidth,
185+
scaledHeight
186+
);
187+
188+
resolve();
189+
};
190+
filterImg.onerror = (error) => {
191+
console.error('Error loading filter image:', error);
192+
reject(error);
193+
};
194+
});
195+
196+
imagePromises.push(imgPromise);
165197
}
166-
} else {
167-
const img = new Image();
168-
img.src = `/images/${filterType}.png`;
169-
img.onload = () => {
170-
ctx.drawImage(img, area.leftCol, area.topRow, width, height);
171-
};
172198
}
173199
});
174200

175-
canvas.toBlob((blob) => {
176-
const link = document.createElement('a');
177-
link.href = URL.createObjectURL(blob);
178-
link.download = 'filtered_image.png';
179-
link.click();
180-
setIsDownloading(false);
181-
}, 'image/png');
201+
Promise.all(imagePromises)
202+
.then(() => {
203+
canvas.toBlob((blob) => {
204+
const link = document.createElement('a');
205+
link.href = URL.createObjectURL(blob);
206+
link.download = 'filtered_image.png';
207+
link.click();
208+
setIsDownloading(false);
209+
}, 'image/png');
210+
})
211+
.catch((error) => {
212+
console.error('Error loading filter images:', error);
213+
setIsDownloading(false);
214+
});
182215
};
183216

184217
img.onerror = () => {

0 commit comments

Comments
 (0)