Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cropping uploaded pic in avatar update #10213

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
"react": "18.3.1",
"react-day-picker": "^8.10.1",
"react-dom": "18.3.1",
"react-easy-crop": "^5.2.0",
"react-google-recaptcha": "^3.1.0",
"react-hook-form": "^7.53.2",
"react-i18next": "^15.2.0",
Expand Down
34 changes: 34 additions & 0 deletions src/Utils/getCroppedImg.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const createImage = (url: string): Promise<HTMLImageElement> =>
new Promise((resolve, reject) => {
const image = new Image();
image.addEventListener("load", () => resolve(image));
image.addEventListener("error", (error) => reject(error));
image.setAttribute("crossOrigin", "anonymous"); // needed to avoid cross-origin issues on some browsers
image.src = url;

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML Medium

DOM text
is reinterpreted as HTML without escaping meta-characters.
});

export async function getCroppedImg(imageSrc: string, croppedAreaPixels: any) {
const image = await createImage(imageSrc);
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");

if (!ctx) {
return null;
}

const { x, y, width, height } = croppedAreaPixels;

canvas.width = width;
canvas.height = height;

ctx.drawImage(image, x, y, width, height, 0, 0, width, height);

return new Promise<string>((resolve) => {
canvas.toBlob((blob) => {
if (!blob) {
return resolve("");
}
resolve(URL.createObjectURL(blob));
}, "image/png");
});
}
Loading
Loading