0

Hey I am trying to change the height and width of image which I read from the input and trying to display it back, this the code:

function readanddisplayImage(input){
  if(input.files && input.files[0]){         
     var filereader = new FileReader(); 
     filereader.readAsDataURL(input.files[0]);         
     filereader.onload = function(reader){
            var image = new Image();
            image.src = reader.target.result;     
            image.width = 150;
            image.height = 150;       
            //It works fine till this line but in the next line image is displayed without the size changed. 
            $("#profilePicture").css("background-image", "url('" + image.src + "')");             
            alert(image.width +"  "+ image.height);
     }
  }    
}

Can someone please tell me where I am going wrong?? ThankYou.

3
  • try to replace image.width with image.style.width and replace image.height with image.style.height Commented Sep 24, 2016 at 6:48
  • stackoverflow.com/questions/1297449/… Commented Sep 24, 2016 at 6:55
  • @MdMusfekurRahman - that doesn't effect the image when used as a css background either Commented Sep 24, 2016 at 6:56

2 Answers 2

3

changing the image width and height like that wont effect the image when used as a CSS background ... try changing the backgound image dimensions using CSS

 filereader.onload = function(reader){
        $("#profilePicture").css("background-image", "url('" + reader.target.result + "')").css("background-size", "150px 150px");

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

1 Comment

Thanks a lot really hekpfull
0

A background image's size is managed by the CSS property background-size - for example, set it to 150px 150px for the desired effect.

Comments

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.