1

I am using react-signature-canvas library to capture user signature in my app. It returns data in base64 format. My requirement is to resize the captured signature into 96*96 resolution before sending it to a backend and for that I am using react-image-file-resizer library. But, props of react-image-file-resizer requires a path of image and I don't have a path but the base64 data.

I am new to react. Please help me!

0

1 Answer 1

0

I read the documentation for react-image-file-resized. It does not look like you could do that unless you are using node (because if you are, you can actually temporarily save it and read then delete).

My suggestion is to resize the Base64 image itself. Here are some resources that you might want to take a look into to do that:

especially this code

const resizeImage = (base64Str, maxWidth = 400, maxHeight = 350) => {
  return new Promise((resolve) => {
    let img = new Image()
    img.src = base64Str
    img.onload = () => {
      let canvas = document.createElement('canvas')
      const MAX_WIDTH = maxWidth
      const MAX_HEIGHT = maxHeight
      let width = img.width
      let height = img.height

      if (width > height) {
        if (width > MAX_WIDTH) {
          height *= MAX_WIDTH / width
          width = MAX_WIDTH
        }
      } else {
        if (height > MAX_HEIGHT) {
          width *= MAX_HEIGHT / height
          height = MAX_HEIGHT
        }
      }
      canvas.width = width
      canvas.height = height
      let ctx = canvas.getContext('2d')
      ctx.drawImage(img, 0, 0, width, height)
      resolve(canvas.toDataURL())
    }
  })
}

Sign up to request clarification or add additional context in comments.

1 Comment

I used suggested code but getting error like Uncaught TypeError: Cannot read property 'source' of undefined at new Image (index.web.tsx:1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.