2

I have an iframe which contains an image. The image source is linked to an image in an S3 bucket. I need to encode this image in Base64 and save the body of the iframe in the database.

How can I do this?

Braiam
  • 1
  • 11
  • 47
  • 78
Евгений
  • 187
  • 3
  • 13
  • 1
    Possible duplicate of [Get image data in JavaScript?](http://stackoverflow.com/questions/934012/get-image-data-in-javascript) – Olivier Boissé Aug 19 '16 at 15:37
  • What have you tried, other than asking others to do your work for you, and why did it not work? –  Aug 19 '16 at 18:10

2 Answers2

0

Try this How to access the <img /> in document from iframe?

$('#Iframe1').contents().find('img').css({'width':'100%','height':'90%'});

and then use this function from here Get image data in JavaScript?

function getBase64Image(img) {
    // Create an empty canvas element
    var canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;

    // Copy the image contents to the canvas
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0);

    // Get the data-URL formatted image
    // Firefox supports PNG and JPEG. You could check img.src to
    // guess the original format, but be aware the using "image/jpg"
    // will re-encode the image.
    var dataURL = canvas.toDataURL("image/png");

    return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}
Community
  • 1
  • 1
Daniel Kobe
  • 9,376
  • 15
  • 62
  • 109
  • Thank you very much for answer, but when i'm try to do this : var image = $('#Iframe1').contents().find('img'); console.log("Base64" + current.getBase64Image(image)); I have error "The provided value is not a type of HTMLImageElement" – Евгений Aug 21 '16 at 11:38
  • log out the value of image, tell me what it is – Daniel Kobe Aug 21 '16 at 23:13
0

if you store s3 bucket object in binary/blob, e.g you have done on server something like:

...

//event.body - base64 received from client or REST
       
let decodedImage = Buffer.from(event.body.replace(/^data:(image|application)\/\w+;base64,/, ""), "base64")

        var params = {
          Bucket: bucket,
          Key: key, // 
          Body: decodedImage,
          ContentEncoding: 'base64',
          ACL: 'public-read',
          ContentType: event.headers["Content-Type"],
        }


        // Uploading files to the bucket :
...
        s3.putObject(params, function (err, data) {

to received it and convert it back to base64 can be done on frontend with code (React hook in example):

const blobToBase64 = (blob) => {
  const reader = new FileReader()
  reader.readAsDataURL(blob)
  return new Promise((rs, rj) => {
    reader.onloadend = () => {
      rs(reader.result)
    }
    reader.onerror = rj
  })
}

function useImage({s3Link}) {
  const [src, setSrc] = React.useState(null)

  React.useEffect(() => {
    async function query({link}) {
      //link is link to s3 bucket URL link e.g
      // const link = s3.getSignedUrl('getObject', {
      //   Bucket: bucketnamw,
      //   Key: key,
      //   Expires: 30,
      // })

      const r = await fetch(link)
      const blob = await r.blob()
      const base64 = await blobToBase64(blob)
      console.log(`base64!`, base64)
      setSrc(base64)
    }

    if (s3Link) {
      query({link: s3Link})
    }
  }, [s3Link, setSrc])

  return [{src}]
}

...

const [hookImage] = useImage({s3Link})

view:

 {hookImage.src ? <img src={hookImage.src} /> : null}
Anja Ishmukhametova
  • 1,535
  • 16
  • 14