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

spartacus1
Tera Contributor

Hello,



Anyone can help us on how to inject script to validate image before uploading.. I want to use it from this catalog item picture update portion..



user_image.png



Anybody can help?



Thank you


michal29
Mega Guru

Hello Raj,



Use the script:



var result = {


  height: 0,


  width: 0,


  size: 0,


  file_name: ''


  };



function getProperties(sys_id){


  var ga_att = new GlideRecord ("sys_attachment");


  ga_att.addQuery('table_sys_id', sys_id);


  ga_att.query();


  while (ga_att.next()){


  result.height = ga_att.image_height;


  result.width = ga_att.image_width;


  result.size = ga_att.size_bytes;


  result.file_name = ga_att.file_name;


  }


}



getProperties('<sys_id_of_catalog_item>')


// like that: getProperties('6924a5e20409a9881bad70394a55cb63')



Then result object holds all the properties.


Note: the result of this query is not visible in the Table, without the roles sys_attachment



EDIT:



if you have more than one attachment, try this one:



var attachments = [];


function getProperties(sys_id){



  var ga_att = new GlideRecord ("sys_attachment");


  ga_att.addQuery('table_sys_id', sys_id);


  ga_att.query();


  while (ga_att.next()){



var result = {


  height: 0,


  width: 0,


  size: 0,


  file_name: ''


  };



  result.height = ga_att.image_height;


  result.width = ga_att.image_width;


  result.size = ga_att.size_bytes;


  result.file_name = ga_att.file_name;


              attachments.push(result)


  }


}



getProperties('<sys_id_of_catalog_item>')


attachements


// This will return an array of object that are attachment properties



Regards,


Michal


Do you have to enable some feature on ServiceNow to populate the "image_height" and "image_width" values?   When I try to execute this script on the server for a GlideRecord object, it simply returns and empty string.   I also tried those properties on a GlideSysAttachment object, but they just came back as 0.



Thanks,


Dan Morella


spartacus1
Tera Contributor

Hi Michal,



Thanks for the script, but how we're going to use it? in client script? can guide us more. thanks a lot