How to get the size (in pixels) of a jpeg image I get with UrlFetchApp.fetch(photoLink)?

前端 未结 2 1285
甜味超标
甜味超标 2021-01-16 07:29

In a script that sends email in HTML format I add an image that is stored in a public shared folder. I get the blob using UrlFetchApp.fetch(photoLink) but the i

相关标签:
2条回答
  • 2021-01-16 07:50

    There is no easy way within Apps Script to figure out what an image size would be. There are some other projects that might be able to analyze the bitmap data and give you dimensions.

    The last time I had to solve this problem. I just wrote a simple App Engine app to do the image math for me -

    import webapp2
    from google.appengine.api import urlfetch
    from google.appengine.api import images
    from django.utils import simplejson
    
    class MainHandler(webapp2.RequestHandler):
      def get(self):
        url = self.request.get('url')
        imgResp = urlfetch.fetch(url) #eg. querystring - url=http://xyz.com/img.jpg 
        if imgResp.status_code == 200:
          img = images.Image(imgResp.content);
          jsonResp = {"url":url, "h":img.height, "w":img.width, "format":img.format}
          self.response.headers['Content-Type'] = 'application/json'
          self.response.out.write(simplejson.dumps(jsonResp))
    
    app = webapp2.WSGIApplication([('/imageinfo', MainHandler)], debug=True)
    

    And then I call it from Apps Script like this -

    function checkImageSizes() {
      var imageUrls = ['http://developers.google.com/apps-script/images/carousel0.png','http://www.w3.org/MarkUp/Test/xhtml-print/20050519/tests/jpeg420exif.jpg'];
      for(var i in imageUrls){
        var resp = JSON.parse(UrlFetchApp.fetch('http://arunimageinfo.appspot.com/imageinfo?url='+imageUrls[i]).getContentText());
        Logger.log('Image at %s is %s x %s',resp.url,resp.w,resp.h);
      }  
    }
    

    You are welcome to use my App Engine instance if your volume is a couple of times a week :)

    0 讨论(0)
  • 2021-01-16 07:56

    I doubt you can do this in apps script. Certainly not natively but you might be able to find or adapt a jpg library that looks at the binary blob header and extracts the image size.

    0 讨论(0)
提交回复
热议问题