How to calculate Image width and height?

Rajesh M1
Giga Guru

Hello,

I have a requirement in which we have to validate image width and height of the image file attached to the catalog request.

There is similar functionality in Images(db_image) table OOB. if we upload any image it directly calculates image width and height but I am unable to trace the code which is performing this functionality.

Please suggest an approach or possible solution to perform this activity.

Note: Our instance is in Fuji

Best Regards,

Rajesh M.

19 REPLIES 19

arnabwa
Giga Guru

Hi Raj,



Try this On Load client script below. It works perfectly fine for me :



function onLoad() {


    //Type appropriate comment here, and begin script below


  var maxheight = 1000;


  var maxwidth = 1000;


  var valid = true;


  window.myisvalidimage = function(){


  return valid;


};


  $j('#sc_attachment_button').on('click', function( e ) {


  setTimeout(function (){



  $j('#attachFile').on('change', function( evt ) {


if (typeof (this.files) != "undefined") {


   




var reader = new FileReader();


                              //Read the contents of Image File.


                              reader.readAsDataURL(this.files[0]);


                              reader.onload = function (e) {


                                      //Initiate the JavaScript Image object.


                                      var image = new Image();


                                      //Set the Base64 string return from FileReader as source.


                                      image.src = e.target.result;


                                      image.onload = function () {


                                              //Determine the Height and Width.


                                              var height = this.height;


                                              var width = this.width;


                                              if (height > maxheight || width > maxwidth) {


  valid = false;


                                                      alert("Height and Width must not exceed 100px.Please Delete and Re-upload !");


                                                      return false;


                                              }


                                              alert("Uploaded image has valid Height and Width.");


                                              return true;


                                      };


                              };


                      } else {


                              alert("This browser does not support HTML5.");


                              return false;


                      }


});



  },3000);


 


  });


}



Make the necessary variable changes and you are good to go !!!



Thanks,


Arnab


Hi Arnab,



Which necessary variable to be change for example are you referring to?



Thanks


spartacus1
Tera Contributor

Hi Arnab,



Thank you for the script, but were going to use it under 'Product Catalog' sample during adding item in Hardware model then clicking the 'add picture' or 'Update' picture... can be done? please help



I want control the image details during onLoad..


user_image2.png



Thank you in advance


Hi,



client side solution will no give you consistent result on different browsers. i would suggest server side approach. make one on submit client script and query attachment table for images and fetch image height and width and do your validation.



i hope this helps.



(please mark helpful/correct/like based on impact)


Hi Rushit,



you mean do a 'UI Actions' ? can guide us more?



Thanks