0

I am handling image files (mostly JPG, occasionally PNG, TIFF or GIF, never other format, if it makes a difference), and I need to make sure they are within a specified pixel size range (min/max width, min/max height). Files are all in G-Drive folders.

I have found a lot of questions regarding determining the pixel count on screen or in an HTML environment, but I haven't been able to find a way to determine the pixel dimensions of a file.

I want to write the height and width to a Google Sheet, where I can check compliance with the parameters. The solution needs to be in JavaScript.

Here is the code I have so far

  • what have you tried? what code are you using that operates on image files? – Isaac Mar 21 '18 at 00:22
  • You don't necessarily have to *display* the image in order to do the height/width determination. You'll always have to load the images though. Are you using this in google sheet code, or as a standalone thing? – Jorg Mar 21 '18 at 00:25
  • This question as it stands is too broad. Do you need help accessing the file from Google drive, or just getting the dimensions of the file that you've already accessed... posting code would also help. – Larry Turtis Mar 21 '18 at 00:37
  • `var img = document.createElement('img'); img.onload = function(){ console.log('width:'+this.width+'; height:'+this.height); }; img.src = 'someImg.png';`. Note that `onload` is Asynchronous. – StackSlave Mar 21 '18 at 00:40
  • Yes, your question is too broad, but for most image file formats, you don't need to load the full file, a simple range request might do, but you need to know the file format before hand. For JPEG ones, you don't need to decode it all through an ``, just grab the EXIF data. [Here you can find a code](https://stackoverflow.com/questions/42540903/sort-by-image-resolution-in-gallery/42649964#42649964) that does it from user selected Files for various formats. You'd just have to handle the format detection & range-request fetching yourself. – Kaiido Mar 21 '18 at 03:16
  • Thank you for the ideas.... – RealSweatyYeti Mar 21 '18 at 16:07
  • Thank you for the ideas.... though I'm not sure how to be more specific when I don't know where to look for the answer Jorg, larry turtis - I need help accessing the file, then writing the pixel dimensions to Google Sheet. PHPglue - I get an error saying "document" is undefined... it seems this code only works in HTML5, from what I've read... how do I define "document" in garden variety javascript? kaiido - thank you for the link - I never imagined it would be so complicated to do what I would have thought was a common task. – RealSweatyYeti Mar 21 '18 at 16:16
  • The code I've got so far is: function folderContents(id) { var folder = DriveApp.getFolderById('[idXYZ]'); var ss = SpreadsheetApp.getActiveSpreadsheet(); var entrySheet = ss.getSheetByName("Files IN"); var contents = folder.getFiles(); var file; var name; var date; var size; var format; var width; var height; var data; entrySheet.clear(); //remove this line for going live entrySheet.appendRow(["Name", "Date Created", "File Size","File Format","Width","Height"]);//remove this line for going live – RealSweatyYeti Mar 21 '18 at 16:17
  • while(contents.hasNext()) { file = contents.next(); name = file.getName(); date = file.getDateCreated(); size = file.getSize(); format = name.substring(name.lastIndexOf('.')+1); width = 100; //THIS IS WHERE I NEED HELP height = 200; //THIS IS WHERE I NEED HELP data = [name, date, size, format,width,height]; entrySheet.appendRow(data); } Everything works perfectly.... I just need to figure out what to do to get "width" and "height". – RealSweatyYeti Mar 21 '18 at 16:17
  • Last two comments are in case people want to have the code editable... there is an image added to my original post showing it properly formatted – RealSweatyYeti Mar 21 '18 at 16:25

0 Answers0