Reputation: 1107
I'm trying to resize an image to generate thumbnails, cards etc. However, while the image is being resized, it seems to be maintaining aspect ratio, which doesn't work because the original images are often rectangular while the thumbnails, cards etc. have square images/ different aspect ratio from original image. Also, if the aspect ratio is the same, then I didn't realize how this is different from just setting the width of the img in CSS?
I have the following code on the App Engine backend to handle this:
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
and the result can be seen by changing the url params at: 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
I'm trying to get it to a 320x320 square or a different aspect ratio. Thanks!
Upvotes: 0
Views: 49
Reputation: 872
The Images service doesn't provide a way to resize without maintaining the aspect ratio. To do that you would need to use another library and re-save the file.
The benefit of resizing the image on the server side rather than on the client using CSS is that you reduce the served file size, which improves loading speed. GAE Images service also gets you a free CDN for resized images, which means it gets cached, duplicated to different geographic locations, and served even faster, and it doesn't cost you anything in addition to the cost of storing the original image.
Upvotes: 1