Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get image size of image loaded by lazy load

I am using lazy load plugin (jquery.lazyload.js) and I try to get the image size that it loads. So in the display part I have:

 echo '<img #img id="img-' . $i . '" name="' . $filename . '" class="lazy" data-original="'.$image.'" />';

Java Script:

<script type="text/javascript">     
   displaysize(img) {
      console.log(img.clientWidth, img.clientHeight);
   }        

</script>


<script src="js/rectangle3.class.js"></script>

HTML:

<div id="imgsize">
  <p id='imgsize-debug'></p>
  <button id='map-download-btn' onclick='displaysize();'>Image size:</button>
  <div id='imagesize'></div>
</div>

The image display is ok but when I add displaysize(img) function to the script and button, the page keep loading. I will need to get image size for calculation in my java script file,

document.getElementById("rectRecord-" + this.rectPointer).innerHTML =
        "Rectangle " + (this.rectPointer + 1) + " ("
        + this.startX + ", "
        + this.startY + ", "
        + this.endX + ", "
        + this.endY + ")"

need to change to:

 document.getElementById("rectRecord-" + this.rectPointer).innerHTML =
        "Rectangle " + (this.rectPointer + 1) + " ("
        + (this.startX)/width + ", "
        + (this.startY)/height + ", "
        + (this.endX-this.startX)/width+" , "
        + (this.endY-this.startY)/height +""
like image 730
user1314404 Avatar asked Oct 17 '22 11:10

user1314404


2 Answers

As I said in the comments, you need to pass the image to the function in order to fetch the width/height. In this example you can click the image to get the size.

In order to use a button you would need to use a selector to target the right image you want the dimensions of.

function displaysize(img) {
  console.log(img.clientWidth, img.clientHeight);
}

$(function() {
    $("img.lazy").lazyload();
    
    $(document).on('click', '.lazy', function() {
      displaysize($(this)[0]);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery_lazyload/1.9.7/jquery.lazyload.js"></script>

<img class="lazy" data-original="http://appelsiini.net/img/bmw_m1_hood.jpg" />
like image 76
DarkBee Avatar answered Oct 20 '22 22:10

DarkBee


Where is the display size button relative to your lazy loaded image? Does each lazy load image have a display size button or is there only one lazy loaded image?

Your problem is that your displaysize method which uses a parameter called img but you do not pass any parameter when you call the function on click.

So you need to pass something to that display size method which will point it to the correct image.

Of your image is like this relative to your button:

 <img id="lazyLoadedImage" />
 <div id="imgsize">
   <p id='imgsize-debug'></p>
   <button id='map-download-btn'    onclick='displaysize("lazyLoadedImage");'>Image size:</button>
   <div id='imagesize'></div>
 </div>

 displaysize(imgID) {

          console.log(document.getElementById(imgID).width + ","+ document.getElementById(imgID).height);
    }        

If you do not have a static set up like this depending on the questions i asked at the beginning here you will have to adjust this code. However whatever the case you will be able to do this by passing an actual reference of the image you would like to inspect to your display image size method.

like image 45
Harry Avatar answered Oct 20 '22 23:10

Harry