Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

michal29
Mega Guru

Hello,



You can use my script actually anywhere...


My best bet would be a business rule working on update / insert that would run the script I provided.


Then you will get the attachments as an array of objects, or will get an empty array if there are no attachments.



Regards,


Michal


Thanks Michal,



I am using it in Product Catalog., can it be use there ya? updating the catalog item picture also is on the attachment?


BTW, do I need to change any from your script?


michal29
Mega Guru

Hello,



I guess you can test the script in the browser console if that suits your needs.



"updating the catalog item picture also is on the attachment?"


- I believe, yes



"BTW, do I need to change any from your script?"


- Yes, depends what you want to get.



1)


Say, you want to display the dimensions in some field, then add field to the catalog form and do


g_form.setValue(<YOUR_ADDED_FIELD>, attachments)



2)


Say, you want to validate the dimension if it's not too big, then after the script do,


for(var i =0; i< attachments.length; i++){


  if (attachments[i].height   > 500|| attachments[i].width > 500){
    alert("The attachment is too big, please remove it");
  }


}



To remove the attachment you could query similar to the script I provided, and use gr.delete();



Regards,


Michal


Thank you Michal and we appreciate your patient to us 😉



1)


Say, you want to display the dimensions in some field, then add field to the catalog form and do


g_form.setValue(<YOUR_ADDED_FIELD>, attachments)


**question: what is 'attachments' ur referring to? is that the value of picture or the image filename? cos I cannot pull the picture name from the filed 'picture' in 'cmdb_hardware_product_model' table



Can you give us sample code for 'g_form.setValue(<YOUR_ADDED_FIELD>, attachments)' ?