Image APIãå©ç¨ãã
http://code.google.com/appengine/docs/images/usingimages.html
ã¯ããã«
Image APIã使ãã¨ãªãµã¤ãºãå転ãè²ãã³ã³ãã©ã¹ãã®èª¿ç¯ãªã©æ¨æºçãªå¤æãè¡ããã¨ãã§ããããããã®å¤æã¯é常ã¯ã¦ã¼ã¶ã¼ãã¢ãããã¼ãããç»åãåçã«å¯¾ãã¦è¡ããããã¨ã ããããã®ããã¥ã¡ã³ãã§ã¯ç»åãåçã«ã¢ãããã¼ããããå¤æãããä¿åãããããæé ã«ã¤ãã¦èª¬æãããGetting Started Guideã«ããä¾é¡Guestbookãå©ç¨ãã¦ãæ¨æ¶ã¨å ±ã«ã¢ãã¿ã¼ãã¢ãããã¼ãã§ããããã«æ¹é ãã¦ã¿ããã
ç»åããããã£ä½æ
åãã«ããã¹ããã¨ã¯ãã²ã¹ãããã¯ãµã³ãã«ã®ã¢ãã«ãä¿®æ£ãã¦ãã¢ãããã¼ããããç»åãBlobã§ä¿åã§ããããã«ãããã¨ã ã
class Greeting(db.Model): author = db.UserProperty() content = db.StringProperty(multiline=True) avatar = db.BlobProperty() date = db.DateTimeProperty(auto_now_add=True)
ã¦ã¼ã¶ã¼ç»åãã¢ãããã¼ã
次ã«ããã®ã¯ããã©ã¼ã ãä¿®æ£ãã¦ã¦ã¼ã¶ã¼ããã¡ã¤ã«ãé¸æãã¦ã¢ãããã¼ãããããã®ãã£ã¼ã«ãã追å ãããã¨ã ãã¾ãããã©ã¼ã ã¿ã°ã«enctypeå±æ§ã追å ãã¦ããã«ããã¼ããã©ã¼ã ãã¼ã¿ã§ãããã¨ãå¿ããã«æå®ãã¦ãããã¨ã
self.response.out.write(""" <form action="/sign" enctype="multipart/form-data" method="post"> <div><label>Message:</label></div> <div><textarea name="content" rows="3" cols="60"></textarea></div> <div><label>Avatar:</label></div> <div><input type="file" name="img"/><</div> <div><input type="submit" value="Sign Guestbook"></div> </form> </body> </html>""")
ããã§ãã£ã¼ã«ããäºã¤æã¤ç°¡åãªãã©ã¼ã ãã§ããã
ããã¾ã§æ¥ããGuestbookãã³ãã©ãå¤æ´ãã¦ããã©ã¼ã ããç»åãã¼ã¿ãåãåãããã¼ã¿ã¹ãã¢ã«Blobã¨ãã¦ä¿åã§ããã
class Guestbook(webapp.RequestHandler): def post(self): greeting = Greeting() if users.get_current_user(): greeting.author = users.get_current_user() greeting.content = self.request.get("content") avatar = self.request.get("img") greeting.avatar = db.Blob(avatar) greeting.put() self.redirect('/')
ç»åã®å¤æ
Images APIã使ãã°ããã¤ãã®ç»åå¤æãå©ç¨ã§ããã
I'm Feeling Lucky
ãã®ãI'm Feeling Luckyãå¤æã¯æããæããã強調ããè²ã調ç¯ãã¦ã³ã³ãã©ã¹ããæé©ãªã¬ãã«ã«ãã¦ãããã
Guestbookã¢ããªã±ã¼ã·ã§ã³ã§ã¯32x32ã®ã¢ãã¿ã¼ãä½ããããã¾ãåãã«google.appengine.api.imagesã¢ã¸ã¥ã¼ã«ãã¤ã³ãã¼ãããããããresizeé¢æ°ãå¼ãã§ç»åãã¼ã¿ã渡ãã¦ããå¿ è¦ãããã
from google.appengine.api import images class Guestbook(webapp.RequestHandler): def post(self): greeting = Greeting() if users.get_current_user(): greeting.author = users.get_current_user() greeting.content = self.request.get("content") avatar = images.resize(self.request.get("img"), 32, 32) greeting.avatar = db.Blob(avatar) greeting.put() self.redirect('/')
åçç»å表示(servingã£ã¦ã©ã訳ãã°ãããã ããã»ã»ã»)
æå¾ã«/imgãã¹ã«ã¢ã¯ã»ã¹ããã£ãã¨ãã«åçã«ãããã®ç»åã表示ããImageãã³ãã©ãä½æããªããã°ãããªããã¾ããããã®åçãªç»åãå¼ã³åºãããã«HTMLãå¤æ´ããã
class Image (webapp.RequestHandler): def get(self): greeting = db.get(self.request.get("img_id")) if greeting.avatar: self.response.headers['Content-Type'] = "image/png" self.response.out.write(greeting.avatar) else: self.error(404)
Imageãã³ãã©ã§ã¯img_idããªã¯ã¨ã¹ãããåå¾ãããã¤ã¾ãGuestbookã®HTMLãä¿®æ£ãã¦ãImageãã³ãã©ã«greetingã®keyã渡ãå¿ è¦ãããã
self.response.out.write("<div><img src='img?img_id=%s'></img>" % greeting.key()) self.response.out.write(' %s</div>' % cgi.escape(greeting.content))
ããã§Guestbookã¢ããªã±ã¼ã·ã§ã³ã®ä¿®æ£ã¯å®äºã :
import cgi import datetime import wsgiref.handlers import logging from google.appengine.ext import db from google.appengine.api import users from google.appengine.ext import webapp from google.appengine.api import images logging.getLogger().setLevel(logging.DEBUG) class Greeting(db.Model): author = db.UserProperty() content = db.StringProperty(multiline=True) avatar = db.BlobProperty() date = db.DateTimeProperty(auto_now_add=True) class MainPage(webapp.RequestHandler): def get(self): self.response.out.write('<html><body>') query_str = "SELECT * FROM Greeting ORDER BY date DESC LIMIT 10" greetings = db.GqlQuery (query_str) for greeting in greetings: if greeting.author: self.response.out.write('<b>%s</b> wrote:' % greeting.author.nickname()) else: self.response.out.write('An anonymous person wrote:') self.response.out.write("<div><img src='img?img_id=%s'></img>" % greeting.key()) self.response.out.write(' %s</div>' % cgi.escape(greeting.content)) self.response.out.write(""" <form action="/sign" enctype="multipart/form-data" method="post"> <div><label>Message:</label></div> <div><textarea name="content" rows="3" cols="60"></textarea></div> <div><label>Avatar:</label></div> <div><input type="file" name="img"/></div> <div><input type="submit" value="Sign Guestbook"></div> </form> </body> </html>""") class Image (webapp.RequestHandler): def get(self): greeting = db.get(self.request.get("img_id")) if greeting.avatar: self.response.headers['Content-Type'] = "image/png" self.response.out.write(greeting.avatar) else: self.response.out.write("No image") class Guestbook(webapp.RequestHandler): def post(self): greeting = Greeting() if users.get_current_user(): greeting.author = users.get_current_user() greeting.content = self.request.get("content") avatar = images.resize(self.request.get("img"), 32, 32) greeting.avatar = db.Blob(avatar) greeting.put() self.redirect('/') application = webapp.WSGIApplication([ ('/', MainPage), ('/img', Image), ('/sign', Guestbook) ], debug=True) def main(): wsgiref.handlers.CGIHandler().run(application) if __name__ == '__main__': main()