App Engine Imagine API 上的大小调整 api 如何工作?

How does the resizing api on the App Engine Imagine API work?

我正在尝试调整图像大小以生成缩略图、卡片等。但是,在调整图像大小时,它似乎保持纵横比,但这不起作用,因为原始图像通常是矩形的,而缩略图、卡片等具有方形图像/与原始图像不同的纵横比。另外,如果宽高比相同,那么我没有意识到这与在 CSS?

中设置 img 的宽度有何不同

我在 App Engine 后端有以下代码来处理这个问题:

class ImageHandler(webapp2.RequestHandler):
    def get(self):
        img_url = self.request.get("url")
        try:
            result = urllib2.urlopen(img_url)
            image = result.read()
            width = int(self.request.get("width")) or 320
            height = int(self.request.get("height")) or 320
            image = images.resize(image, width, height)
            self.response.headers['Content-Type'] = 'image/png'
            self.response.out.write(image)
            #pdb.set_trace()
            #print "Result: " + str(result.read())
        except urllib2.URLError, e:
            print e

并且可以通过更改 url 参数来查看结果: http://qwiznation.appspot.com/img?width=400&height=200&url=http://www.washingtonpost.com/blogs/wonkblog/files/2012/12/Obama_Fiscal_Cliff-0e5c7-588-copy.jpg

我正在尝试将其设置为 320x320 正方形或不同的纵横比。谢谢!

图片服务不提供在不保持纵横比的情况下调整大小的方法。为此,您需要使用另一个库并重新保存文件。

在服务器端而不是在客户端使用 CSS 调整图像大小的好处是您可以减小提供的文件大小,从而提高加载速度。 GAE 图像服务还为您提供了一个免费的 CDN,用于调整大小的图像,这意味着它会被缓存、复制到不同的地理位置,并且服务速度更快,而且除了存储原始图像的成本之外,它不会花费您任何费用。