如何获得我用UrlFetchApp.fetch(photoLink)获得的jpeg图像的大小(以像素为单位)? [英] How to get the size (in pixels) of a jpeg image I get with UrlFetchApp.fetch(photoLink)?

查看:272
本文介绍了如何获得我用UrlFetchApp.fetch(photoLink)获得的jpeg图像的大小(以像素为单位)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以HTML格式发送电子邮件的脚本中,我添加了一个存储在公共共享文件夹中的图像。
我使用 UrlFetchApp.fetch(photoLink)获得blob,但图像不一定是正确的大小,所以在html代码中我使用width和height属性现在使用固定值,见下面的代码),但我希望它会自动调整正确的比例。



为了实现这一点,我需要知道如何获得原始大小的图像(高度和宽度),但我只是不知道如何得到它没有插入图像的中间文件(这将工作,但我觉得这种方法有点怪异和不必要的复杂...而且,我不感觉每次更改图像文件时都会出现无用的文档)。



以下是创建电子邮件的代码的相关部分:

  function sendMail(test,rowData,genTitle,day,title,stHour,endHour){
var photoLink = sh.getRange( 'H1')的getValue();
var image = UrlFetchApp.fetch(photoLink);
// *************找到图片的像素大小以得到它的比例
var msgTemplate ='< A HREF =http:// www。 domain.be/reservations-global/\">< IMG SRC ='+ photoLink +'BORDER = 0 ALT =logoHEIGHT = 200 WIDTH = 300>< / A>< BR>< BR> '+
'''''''''''''''''''''''';'''''''''''''' < th>< th>< TH>< th>< th>< th>< th>< th>< th>< th> Cordialement,< BR>< BR>';
var mailTitle ='Confirmation deréservation - '+ getTextFromHtml(genTitle);
var descr =''; (var d = 0; d Logger.log(Number(rowData [(d + 5)]));
var content ='< tr bgcolor =#ffffbbwidth =100%>< td>< NUMBER> < / td>< td> (数字(rowData [(d + 5)])> 1){var pl ='places'} else {var pl ='< / tr>'
if地点'};
content = content.replace('< NUMBER>',rowData [(d + 5)] + pl);
content = content.replace('< DESCRIPTION>',title [d] +'de'+ stHour [d] +'heuresà'+ endHour [d] +'heures'); (数字(rowData [(d + 5)])> 0){
descr + = content;
if
}
}
msgTemplate = msgTemplate.replace('< NOM>',rowData [1]).exc('< EVENTS>',descr).replace('<的tableHeader>,genTitle);
var textVersion = getTextFromHtml(msgTemplate.replace(/< br> / gi,'\\\
')。replace(/< td> / gi,'\\\
'));
// Logger.log(textVersion)
if(test){
MailApp.sendEmail(Session.getEffectiveUser()。getEmail(),mailTitle,textVersion,{'htmlBody':msgTemplate, replyTo:retour});
}
else
{
MailApp.sendEmail(rowData [2],mailTitle,textVersion,{'htmlBody':msgTemplate,replyTo:retour});
}
}


解决方案

在Apps脚本中找到一个简单的方法来确定图像的大小。还有一些其他项目可能能够分析位图数据并给出尺寸。



上一次我必须解决这个问题。我只写了一个简单的App Engine应用程序来为我做图像数学运算 -

 从google.appengine导入webapp2 
.api从google.appengine.api导入urlfetch
从django.utils导入图像
import simplejson
$ b $ 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-类型'] ='application / json'
self.response.out.write(simplejson.dumps(jsonResp))

app = webapp2.WSGIApplication([('/ imageinfo',MainHandler )],debug = True)

然后我从Apps Script这样调用它 -

 函数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('url')。 getContentText());
Logger.log('%s处的图像是%s x%s',resp.url,resp.w,resp.h);




$ b

欢迎您使用我的App Engine实例,如果您的音量是一周几次:)

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 image has not necessarily the right size so in the html code I use width and height attributes (for now with fixed values, see code below) but I'd like it to be automatically resized with the right ratio.

To achieve that I need to know how to get the original size of the image (height and width) but I just don't know how to get it without inserting the image in an intermediate document (which would work but I find this approach a bit weird and unnecessarily complicated... moreover I don't feel like having a useless doc appearing each time I change the image file).

Here is the relevant part of the code that creates the email message :

function sendMail(test,rowData,genTitle,day,title,stHour,endHour){
  var photoLink = sh.getRange('H1').getValue();
  var image = UrlFetchApp.fetch(photoLink);
  //************* find the pixel size of the image to get its ratio
  var msgTemplate = '<A HREF="http://www.domain.be/reservations-global/"><IMG SRC="'+photoLink+'" BORDER=0 ALT="logo" HEIGHT=200 WIDTH=300></A><BR><BR>'+
    'Résumé de vos réservations au nom de <NOM><BR><BR><BR><table style="background-color:lightblue;border-collapse:collapse;" border = 1 cellpadding = 5><th></th><th><TABLEHEADER></th><EVENTS></table><BR><CONCLUSION><BR>Cordialement,<BR><BR>';
  var mailTitle = 'Confirmation de réservation - '+getTextFromHtml(genTitle);
  var descr = '';
  for(var d = 0;d<day.length;++d){
    Logger.log(Number(rowData[(d+5)]));
    var content = '<tr bgcolor="#ffffbb" width="100%"><td><NUMBER> </td><td > <DESCRIPTION></td></tr>'
    if(Number(rowData[(d+5)])>1){var pl = ' places'}else{var pl = ' place'};
    content = content.replace('<NUMBER>',rowData[(d+5)]+pl);
    content = content.replace('<DESCRIPTION>',title[d]+' de '+stHour[d]+' heures à '+endHour[d]+' heures');
    if(Number(rowData[(d+5)])>0){
      descr += content;
    }
  }
  msgTemplate = msgTemplate.replace('<NOM>',rowData[1]).replace('<EVENTS>',descr).replace('<TABLEHEADER>',genTitle); 
  var textVersion = getTextFromHtml(msgTemplate.replace(/<br>/gi,'\n').replace(/<td>/gi,'\n'));
//  Logger.log(textVersion)
  if(test){
    MailApp.sendEmail(Session.getEffectiveUser().getEmail(),mailTitle, textVersion,{'htmlBody':msgTemplate,"replyTo" : retour});
  }
  else
  {
    MailApp.sendEmail(rowData[2],mailTitle, textVersion,{'htmlBody':msgTemplate,"replyTo" : retour});
  }
}

解决方案

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 :)

这篇关于如何获得我用UrlFetchApp.fetch(photoLink)获得的jpeg图像的大小(以像素为单位)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆