8

I'm using javascript function to input image.

This is my HTML:

<input type="file" name="filUpload" id="filUpload" onclick="" onchange="showimagepreview(this)">
<br />
<div id="before">
<img id="imgprvw" alt="" class="img-thumbnail" style="width: 250px; height: 250px;" />

And this is my JavaScript:

function showimagepreview(input) { //image preview after select image
  if (input.files && input.files[0]) {
    var filerdr = new FileReader();
    filerdr.onload = function(e) {
      url = e.target.result;
      image = url;
      $('#imgprvw').attr('src', url);
      //console.log(url);

      //$('#imgprvw').attr('src', e.target.result);
      //url = e.target.result;
      //$("#imgpath").val(url);
    }
    filerdr.readAsDataURL(input.files[0]);
  }
}

This code convert image to bite array, I need to resize my image using that bite array. Is there any possible methods to do it.

I will pass that binary array to web service. I the image size too high, it takes much time, that's why I need to convert.

6
  • If you actually want to resize the image (and not just change the size on the site), maybe check out resizing via canvas: stackoverflow.com/questions/19262141/… But generally I would do that on the backend. Commented Nov 25, 2014 at 9:21
  • i will pass that binary array to web service. if the image size too high, it takes much time, that's why i need co convert. Commented Nov 25, 2014 at 9:25
  • Yes, then resize it via canvas. You can use document.getElementById("mycanvas").toDataURL('image/png'); to get the PNG as a data-url afterwards. Commented Nov 25, 2014 at 9:25
  • Possible duplicate of Resize image with javascript canvas (smoothly) Commented Jul 10, 2017 at 9:37
  • stackoverflow.com/questions/19262141/… Commented Jan 1, 2019 at 9:51

1 Answer 1

10

Resize it on the canvas like this:

function showimagepreview(input) { //image preview after select image
  if (input.files && input.files[0]) {
    var filerdr = new FileReader();

    filerdr.onload = function(e) {
      var img = new Image();

      img.onload = function() {
        var canvas = document.createElement('canvas');
        var ctx = canvas.getContext('2d');
        canvas.width = 250;
        canvas.height = canvas.width * (img.height / img.width);
        ctx.drawImage(img, 0, 0, canvas.width, canvas.height);

        // SEND THIS DATA TO WHEREVER YOU NEED IT
        var data = canvas.toDataURL('image/png');

        $('#imgprvw').attr('src', img.src);
        //$('#imgprvw').attr('src', data);//converted image in variable 'data'
      }
      img.src = e.target.result;
    }
    filerdr.readAsDataURL(input.files[0]);
  }
}

I haven't tested this yet, but it should work.

Resize image with javascript canvas (smoothly)

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

6 Comments

You tried to linked the question Resize image with javascript canvas (smoothly), but it was not visible the that way. As a note: this question very similar to the linked one, so it should probably be better marked as duplicate.
It's similar, but it didn't include file upload and adding the loaded image to a canvas. Thanks for the code clean-up though. I didn't find an easy way to indent.
I understand your point and I might be wrong about it: But creating answers over and over again, that are just slightly different, is not that helpful to keep SO clear. The question is about resizing and not "How to draw data that was read by FileReader as image into a canvas" or something like this. While the OP might not know how to do that it should be a different question IMHO.
data in img.src is not converted data. but there is some values in var data. but i can't identify what value is this.
t.niese, you do have a point. Can you mark it as duplicate? Saranga, the value in data is base64 encoded. You should be able to use it as per your edit. Does it work?
|

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.