【问题标题】:Google App Engine: Uploading and Transforming User Profile Photo via BlobstoreGoogle App Engine:通过 Blobstore 上传和转换用户个人资料照片
【发布时间】:2014-03-14 09:02:06
【问题描述】:

我试图让每个用户上传一张个人资料照片,并将它们转换为 100x100 像素。

我创建了一个 ProfilePhoto 模型:

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

上传处理程序:

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() 方法,但此时我对如何使用它感到很困惑。

【问题讨论】:

  • 重新缩放它们?或者,如果您只需要 get_serving_url 需要的维度和参数,则对其进行编辑。
  • 是的,先生,@JimmyKane。我想重新调整它们。鉴于我的设置,您将如何使用get_serving_url()? (我已经编辑了问题)

标签: python image google-app-engine blobstore


【解决方案1】:

在你的模板里放

<img src="{{ profile_photo.serving_url }}=s100">

【讨论】:

    猜你喜欢
    • 2014-08-26
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 2017-05-04
    相关资源
    最近更新 更多