【发布时间】:2015-05-27 11:35:38
【问题描述】:
我正在尝试调整图像大小以生成缩略图、卡片等。但是,在调整图像大小时,它似乎保持纵横比,这不起作用,因为原始图像通常是矩形而缩略图,卡片等具有方形图像/与原始图像不同的纵横比。另外,如果纵横比相同,那我没有意识到这与仅在 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 google-app-engine image-resizing