5

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 +""
user1314404
  • 1,253
  • 3
  • 22
  • 51

4 Answers4

1

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" />
DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • I tried that but when I click button it is not showing the coordinate. You can check my piece of code here: – user1314404 Jun 15 '17 at 14:44
  • Yes... But of which image you want the dimensions? You are showing multiple images on the page. That's why you need a selector to pinpoint the correct one or do you want the coors of all the images? – DarkBee Jun 15 '17 at 17:42
  • correct ! this is a slider so it will load one image at a time. If you click next or previous it load another image. So I need to capture the size of that image when it loads. (I found out that when loading, it resize the image with no certain rule, so I need to check the loading size each time it loads). – user1314404 Jun 16 '17 at 02:36
  • Okay, you have a live demo of the above snippet? – DarkBee Jun 16 '17 at 05:32
  • yes, but I don't want to show link here, is there any way I can send you ? – user1314404 Jun 16 '17 at 06:36
  • sent, please check ! Tk u. – user1314404 Jun 16 '17 at 13:45
1

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.

Harry
  • 3,930
  • 2
  • 14
  • 31
1

According to lazy loading plugin site you need to set image dimensions before loading the image:

PRO TIP! You must set image dimensions either as width and height attributes or in CSS. Otherwise plugin might not work properly.

I see you are using PHP, so you can try to set width and height as advised in this answer.

You can also add an id to your images to make the javascript call from the button easier.

Gabriel Molina
  • 539
  • 3
  • 14
0

Finnaly I managed to find the width and height of the image since it is parent of the rectangular I created inside. So in the javascript file I will use:

var options = {
            width: $('#' + rectangles[rectPointer].rectangleDiv.id).parent().width() + 1,
            height: $('#' + rectangles[rectPointer].rectangleDiv.id).parent().height() - 3
        };

Then width and height get from:

  (options.width ||rectangles[rectPointer].startX)   + " "

options.height ||rectangles[rectPointer].startY

then for my calculation:

Rectangle.prototype.updatePosition = function (data, options) {
    this.startX = data.newStartX;
    this.startY = data.newStartY;
    this.endY   = parseFloat(this.startY) + parseFloat(this.rectangleDiv.style.height);
    this.endX   = parseFloat(this.startX) + parseFloat(this.rectangleDiv.style.width);

    console.log("END: " 
        + parseFloat(this.startY) + parseFloat(this.rectangleDiv.style.height) + " - " + parseFloat(this.startX) + parseFloat(this.rectangleDiv.style.width));

    document.getElementById("rectRecord-" + this.rectPointer).innerHTML =
        "Rectangle " + (this.rectPointer + 1) + " ("
        + (options.width || this.startX) + ", "
        + (options.height || this.startY) + ", "
        + (this.startX)/(options.width || this.startX) + ", "
        + (this.startY)/(options.height || this.startY) + ")"
;

Thanks all for your help ! I have credited up points for all but couldn't accept as answer since it doesn't really be used to solve my issue.

user1314404
  • 1,253
  • 3
  • 22
  • 51