图片损坏-Google App引擎 [英] Broken Images - Google App Engine

查看:92
本文介绍了图片损坏-Google App引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我意识到这是一个相当普遍的问题,并且我查看了许多其他类似的StackOverflow问题,但没有一个答案可以解决问题.

Firstly, I realize this is a fairly common question on here, and I have looked at numerous other similar StackOverflow questions, and none of the answers have solved the problem.

基本上,一旦用户提交图像->它们就会发布为空白图像-当我单击图像的src时,我将进入空白页面.我的代码有什么问题?

Basically, once users submit images -> they are posted as blank images - and when I click the src of the images I am lead to a blank page. What's wrong with my code?

这是来自各种文件的代码片段的混搭

Here's a mashup of bits and pieces of my code from various files

<div class="card-image"><img src="/image?img_id={{card.key()}}"></img>

class Card(db.Model):
    image = db.BlobProperty(required = True)

class Image(MainHandler):
    def get(self):
        card = db.get(self.request.get('img_id'))
        if card.image:
            self.response.headers['Content-Type'] = 'image/png'
            self.response.out.write(card.image)
        else:
            self.response.out.write('No image')
 class Gallery(MainHandler):
       image = db.Blob(images.resize(self.request.get('image'), 32, 32))
       #later in the code, a Card is constructed.

推荐答案

您的代码对我来说很好.您确定模型中包含图像数据吗?

Your code looks fine to me. Are you sure your model contains the image data?

第二.您可以使用一种更好,更快和更便宜的方式来提供图像. Google可以使用App Engine的高性能图片服务系统,几乎免费地为您提供图片,并且具有运行时大小调整功能.要使用此功能,您必须使用blobstore并使用get_serving_url.

Second. There is a better, faster and cheaper way you can serve your images. Google can serve the images for you, almost for FREE and with runtime sizing, using App Engine's High-Performance Image Serving System. To use this, you have to use the blobstore and use get_serving_url.

以下是投放网址的示例:

Here is an example of a serving url:

https://lh6.ggpht.com/1HjICy6ju1e2GIg83L0qdliUBmPHUgKV8FP3QGK8Qf2pHVBfwkpO_V38ifAPm-9m20q_3ueZzdRCYQNyDE3pmA695iaLunjE=s0

更新

如果您是blobstore的新手,我的建议是开始从blobstore上载和提供图像.请参阅文档中的代码示例.

If you are new to the blobstore, my advise is to start uploading and serving images from the blobstore. See the code examples in the doc.

完成此操作后,您可以优化和使用get_serving_url.您只需要获取一次该服务网址并将其保存在数据存储区中,就可以在html img标签中使用它.

After this works, you can optimize and use get_serving_url. You only have to get this serving url once and save in the datastore, so you can use in in your html img tag.

这是一个代码示例,用于获取blobstore blob的服务网址,其中blob引用保存在数据存储区中:

Here is a code example to get a serving url for a blobstore blob, where the blob reference is saved in the datastore:

class Dynamic(db.Model):                                                                        # key : name
    name = db.StringProperty() 
    blob_ref = blobstore.BlobReferenceProperty()
    serving_url = db.LinkProperty()

dyn= Dynamic.get_by_key_name(key_name)
try :                                                                               # get url with size = 0, do not save it
    dyn.serving_url = images.get_serving_url(dyn.blob_ref, size=None, secure_url=True)
except DeadlineExceededError : 
    try :             # sometimes this request fails, retry. This always works fine
        dyn.serving_url = images.get_serving_url(dyn.blob_ref, size=None, secure_url=True)
    except DeadlineExceededError :
        logging.error('Image API get_serving_url deadline error after retry' %(dyn.key().name()))                        
                return None
    dyn.put()

这篇关于图片损坏-Google App引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆