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

How to resize Image <img> using css #105

Open
danielsantoso opened this issue Aug 21, 2019 · 4 comments
Open

How to resize Image <img> using css #105

danielsantoso opened this issue Aug 21, 2019 · 4 comments

Comments

@danielsantoso
Copy link

I want to resize img using css width:200px, but nothing happens. can you give an example to resize image using this library?

@mrTurkay
Copy link

same issue,

any solutions?

@chiaraDipiMeta
Copy link

same issue. I've also tried with "pt" and percentages, but nothing changes. Any solution?

@jkarr
Copy link

jkarr commented Feb 13, 2020

I was just able to get image sizing and scaling to work properly today. Here is how I handle images (pngs and svgs are supported in our system). I used a promise to ensure that all images were base64 for attempting the export.

  1. in your html, put a canvas and an image.
    <canvas id="some-identifier"></canvas> <img style="width:300px; max-height: 100%;" src="your image" alt="some-identifier" />

  2. When you click your button to export to word:
    `$("#exportQuestion").on("click", function () {
    var promises = [];
    if ($("#questionCopyDiv canvas").length > 0) {
    $("#questionCopyDiv").removeClass("hidden");

                 // convert images to base64 and convert svg to png
                 $("#questionCopyDiv img").each( function (i) {
                     var $image = $(this);
                     var image = this;
    
                     var promise = new Promise(function (resolve, reject) {
                         var xhr = new XMLHttpRequest();
                         xhr.open('get', $image.attr("src"));
                         xhr.responseType = 'blob';
                         xhr.onload = function () {
                             var fileReader = new FileReader();
                             fileReader.onload = function () {
                                 var canvas = document.getElementById($image.attr("alt") + "-canvas");
                                 var context = canvas.getContext('2d');
                                 var width = image.width;
                                 var height = image.height;
                                 // this is the key, resize the canvas to the appropriate size
                                 canvas.witdh = width; 
                                 canvas.height = height;
                                 // then resize the image to it's original size
                                 $image.css("width", "");
                                 $image.css("height", "");
                                 // draw the image, starting at 0, 0, to the size of the original image.
                                // paint to the canvas at 0, 0 and scale to the desired width and heith
                                 context.drawImage(image, 0, 0, image.width, image.height, 0, 0, width, height);
                                // replace the image src with the case data url and 
                                //          remove the canvas from the page 
                                $image.attr("src", canvas.toDataURL("image/png"));
                                 resolve($(canvas).remove());
                             }
    
                             fileReader.readAsDataURL(xhr.response);
                         };
    
                         xhr.send();
                     });
    
                     promises.push(promise);
                 });
             }
    
             if (promises.length > 0) {
                 Promise.all(promises).then(function(val) {
                     exportToWord();
                 });
             } else {
                 exportToWord();
             }
         });
     });`
    

@coolswitch
Copy link

<img width="300" height="200" ...
this works to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants