1
<div class="row" data-ng-repeat="row in imageDataUrls" >
    <div class="col-sm-3" data-ng-repeat="imageDataUrl in row" >
        <img alt="img" class="img-responsive"data-ng-src="{{imageDataUrl.url}}" />
    </div>
</div>

I am showing the image using data URLs, but how can I get the current height and width in pixel to resize it proportionally in angular js?

I have some algorithm to resize it independently

3
  • you don't need angularjs to do that: stackoverflow.com/questions/3380252/… Commented Mar 2, 2017 at 12:43
  • If you just set width or height the image should be resized proportional Commented Mar 2, 2017 at 12:44
  • I have some algorithm to resize it independently. I want to trigger that function Commented Mar 2, 2017 at 12:47

4 Answers 4

1

I'm not sure what data imageDataUrl has in it but you can get the image dimensions in JavaScript easily:

var img = new Image();
img.onload = function() {
  console.log(this.width + 'x' + this.height);
}
img.src = 'http://lorempixel.com/400/200/';
Sign up to request clarification or add additional context in comments.

2 Comments

but I'm using FileReader();
1

as pointed by mvermand comment, you can just add CSS to your class or directly height/width attributes.

<div class="row" data-ng-repeat="row in imageDataUrls" >
    <div class="col-sm-3" data-ng-repeat="imageDataUrl in row" >
        <img alt="img" class="img-responsive" src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/SNice.svg/2000px-SNice.svg.png" width="300"/>
    </div>
</div>

If using only height or width, the other attribute is calculated to respect the image proportions.

Now, regarding the size you want, you can also use percents - like width="100%" to use the full width.

Cheers

Comments

0

If your goal is to resize proportionally, why not resize using CSS percentages?

CSS:

.smaller {
  width: 80%;
}

Your Code:

<div class="row" data-ng-repeat="row in imageDataUrls" >
    <div class="col-sm-3" data-ng-repeat="imageDataUrl in row" >
        <img alt="img" class="img-responsive smaller" data-ng-src="{{imageDataUrl.url}}" />
    </div>
</div>

Comments

0

you can get/set the height and width properties with angular.element("selector").height or width.

2 Comments

I want to trigger a function on ng-repeat with the current element
add your function in the ng-init attribute and give the data object as parameter. if you really need the element itself (not only source) compile it in your controller with angular $compile. and so on.

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.