2014-02-14 58 views
0

我試圖讓每個用戶上傳一張個人資料照片,並將它們轉換爲100x100像素。谷歌App Engine:通過Blobstore上傳和轉換用戶個人資料照片

我創建了一個模型ProfilePhoto:

class ProfilePhoto(ndb.Model): 
    user_key = ndb.KeyProperty() 
    blob_key = ndb.BlobKeyProperty() 
    serving_url = ndb.StringProperty() 
    created = ndb.DateTimeProperty(auto_now_add = True) 

的UploadHandler:

class ProfilePhotoForm(BaseHandler): 

    def get(self, user_id): 
     user_id = int(user_id) 
     if is_author: 
      author_key = ndb.Key('Account', user_id) 
      profile_photo = ProfilePhoto.query(ProfilePhoto.user_key == author_key).fetch() 
      if profile_photo: 
       photo_blob_key = profile_photo[0].blob_key 
      else: 
       photo_blob_key = None 

      upload_url = blobstore.create_upload_url('/%s/upload' % user_id) 

      user = User.get_by_id(int(user_id)) 
      self.render(
       'profile-photo-form.html', 
       user = user, 
       upload_url = upload_url, 
       photo_blob_key = photo_blob_key, 
       profile_photo = profile_photo) 
     else: 
      self.write('You are not author.') 

    def post(self, user_id): 
     user_id = int(user_id) 
     user = User.get_by_id(user_id) 
     user.profile_photo = self.request.POST.get('profile_photo').file.read() 
     user.put() 
     self.redirect('/u/%s' % user_id) 

然後UploadHandler和ServeHandler看起來是這樣的:

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler): 

    def post(self, user_id): 
     user_key = ndb.Key('User', user_id) 
     photo = self.get_uploads('profile_photo') 
     if photo: 
      existing_photos = ProfilePhoto.query(ProfilePhoto.user_key == user_key).fetch() 
      if existing_photos: 
       # Deleting previous user photos if they exist 
       for e in existing_photos: 
        blobstore.delete(e.blob_key) 
        e.key.delete() 

      photo_info = photo[0] 

      serving_url = images.get_serving_url(photo_info.key()) 

      profile_photo = ProfilePhoto(
       user_key = ndb.Key('User', user_id), 
       blob_key = photo_info.key(), 
       serving_url = serving_url) 

      profile_photo.put() 

      #self.redirect('/profile-photo/%s' % photo_info.key()) 
      self.redirect('/u/%s' % user_id) 
     else: 
      self.redirect('/u/%s' % user_id) 


class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler): 

    def get(self, blob_key): 
     blob_key = str(urllib.unquote(blob_key)) 
     blob_info = blobstore.BlobInfo.get(blob_key) 
     self.send_blob(blob_info) 

最後,形式看起來像這樣:

<form action="{{upload_url}}" method="post" enctype="multipart/form-data"> 


      <label>Profile Photo<br> <input type="file" name="profile_photo"></label> 

      {% if profile_photo %} 
       <h4 class="sub-title">Current Photo</h4> 
       <img src="/profile-photo/{{photo_blob_key}}" /> 
      {% endif %} 

    <input type="Submit" value="Save"> 
</form> 

形象在我的模板次輸出是這樣的:

<img src="/profile-photo/{{photo_blob_key}}" /> 

所以,現在我已經收集是在我的模板變換圖像並將其輸出的最佳方法是使用get_serving_url()的方法,但在這一點我很困惑如何使用它。

+0

重新調整呢?或者,如果您只需要維度get_serving_url需要的參數並對其進行編輯,則可以編輯它們。 –

+0

是的,@JimmyKane。我想重新調整它們。鑑於我的設置,你將如何使用'get_serving_url()'? (我編輯了這個問題) – puoyaahhh

回答

1

在模板把

<img src="{{ profile_photo.serving_url }}=s100"> 
相關問題