1

I am trying to get the Image height and width from a base64 string representation of the image and I am using GoJS.

What I eventually want to do is to add that image to a PDF using jspdf library.

Here is what I have done so far:

download() {
    let imgData = this.diagram.makeImageData({background: "white", returnType: "string"}); 
    let pdf = new jspdf('p', 'mm', 'a4'); // A4 size page of PDF  
      var position = 0;
      var imgWidth = imgData.width; //Reading width from Image Data
      var imgHeight = imgData.height; //Reading height from Image Data
      pdf.setFontSize(30);
      pdf.text("Diagram", 100, 20, 'center');
      pdf.addImage(imgData, 'PNG', 1, 25, position, imgWidth, imgHeight);  
      pdf.save("Diagram.pdf');
  }

In that imgData.width and imgData.height, the visual studio intellisense is showing "Property 'height' and 'width' does not exist on type 'string'.

How can I get the height and width property of the image?

1 Answer 1

1

Create an HTMLImageElement using that image data, and then get its naturalWidth and naturalHeight once it has been loaded:

var imgData = myDiagram.makeImageData(. . .);
var img = document.createElement("img");
img.addEventListener('load', function(e) {
  console.log(img.naturalWidth + " " + img.naturalHeight);
});
img.src = imgData;
Sign up to request clarification or add additional context in comments.

1 Comment

What if the image type is blob?

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.