MCPcopy Create free account
hub / github.com/OpenSignLabs/OpenSign / convertJpegToPng

Function convertJpegToPng

apps/OpenSign/src/constant/Utils.js:4774–4825  ·  view source on GitHub ↗
(base64Image, filename)

Source from the content-addressed store, hash-verified

4772 * @returns {Promise<File>} - A promise that resolves with a new PNG File.
4773 */
4774export function convertJpegToPng(base64Image, filename) {
4775 if (base64Image) {
4776 const arr = base64Image.split(",");
4777 const mimeMatch = arr[0].match(/:(.*?);/);
4778 if (!mimeMatch) throw new Error("Invalid dataURL");
4779 const mime = mimeMatch[1];
4780 const type = mime.split("/")[1];
4781 if (type === "png") {
4782 return base64Image;
4783 } else {
4784 const bstr = atob(arr[1]);
4785 let n = bstr.length;
4786 const u8arr = new Uint8Array(n);
4787 while (n--) u8arr[n] = bstr.charCodeAt(n);
4788 const inputFile = new File([u8arr], `${filename}.${type}`, {
4789 type: mime
4790 });
4791 return new Promise((resolve, reject) => {
4792 // ensure it’s JPEG/JPG (you can remove this check if not needed)
4793 if (!/image\/jpe?g/.test(inputFile.type)) {
4794 return reject(new Error("Input must be a JPEG or JPG image"));
4795 }
4796
4797 // Read the file as a data URL
4798 const reader = new FileReader();
4799 reader.onerror = () => reject(new Error("Failed to read the file"));
4800 reader.onload = () => {
4801 const img = new Image();
4802 img.onerror = () => reject(new Error("Failed to load image"));
4803 img.onload = () => {
4804 // draw image onto a canvas
4805 const canvas = document.createElement("canvas");
4806 canvas.width = img.width;
4807 canvas.height = img.height;
4808 const ctx = canvas.getContext("2d");
4809 ctx.drawImage(img, 0, 0);
4810
4811 // directly get PNG as base64 data URL
4812 try {
4813 const pngDataUrl = canvas.toDataURL("image/png");
4814 resolve(pngDataUrl);
4815 } catch (err) {
4816 reject(new Error("Failed to convert canvas to PNG"));
4817 }
4818 };
4819 img.src = reader.result;
4820 };
4821 reader.readAsDataURL(inputFile);
4822 });
4823 }
4824 }
4825}
4826//function is used to get assigned signer's email
4827export const getSignerEmail = (data, signers) => {
4828 const getEmail =

Callers 3

handleSaveImageFunction · 0.90
handleSaveSignatureFunction · 0.90
addDefaultSignatureFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected