在数据存储区中作为Blob存储在html页面中的图像 [英] Image which is stored as a blob in Datastore in a html page

查看:116
本文介绍了在数据存储区中作为Blob存储在html页面中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在python中制作一个基于Google App Engine的Web应用程序。其中的一部分涉及在IMDb上展示Movie的海报。我有链接到海报。例如。
http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1._SX94_SY140_.jpg



然后我试着把它放在一个HTML页面中:

 < a href =https:/ /ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3.jpg\"> 
< img src =https://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3.jpg>
< / a>

请参阅此处的示例:



不显示图片并点击图片,页面打开:


拒绝转介

您无权访问 https://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3.jpg



参考#24.84d81002.1393249598.981ca7

通过的问题,我知道IMDb不允许推荐到任何其他网站。解决方案是自己获取图像,保存并自行提供。
为此,我遵循此链接



我制作了一个数据存储对象,用于存储电影的各种信息以及使用urlfetch api提取的海报。代码的相关部分是

  from google.appengine.ext import db $ b $ from google.appengine.api import urlfetch 

类Movie(db.Model):
title = db.StringProperty()
poster = db.BlobProperty(default = None)

coverurl =https://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3.jpg
movie = Movie()
posterimg = urlfetch.fetch(coverurl).content
movie .cover = db.Blob(posterimg)
movie.put()

现在,呈现html页面,我从数据存储中获得了电影对象:

  m = Movie.gql(WHERE title =:1 ,title).get()
img = m.posterimg

呈现为

  imghtml ='< img src ='+ img +'> < / a>'

预计发生错误,并且收到:


UnicodeDecodeError:'ascii'编解码器无法解码位置0中的字节0xff:序号不在范围内(128)



这是什么方法?
如何在数据存储中将一个存储为Blob的图像包含在html图像中?

解决方案

upload image使用应用程序引擎帮助程序将blobstore,在相同的处理程序中获取serving_url,使用您的图像对象保存serving_url,在您的html模板中引用图片url



例如

  from google.appengine.ext.webapp import blobstore_handlers 
from google.appengine.api.images import get_serving_url

class Handler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file')
blob_info = upload_files [0]
)img = MyImageObject()
img.image_name = blob_info.filename
img.image_url = get_serving_url(blob_info.key())
img.put()



然后在模板中

 < ; img src ={{img.image_url}}> 

为了获得图像的原始大小,您需要追加= s0到url的结尾,append = sXX要调整大小,请参阅此处的网址修改部分 https://developers.google.com / appengine / docs / python / images / functions

I am trying to make a Web Application based on Google App Engine in python. A part of it deals with displaying the poster of Movie hosted at IMDb. I have the link to the poster. For ex. http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1._SX94_SY140_.jpg

Then I tried to put it in an HTML page as:

<a href="https://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3.jpg">
<img src="https://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3.jpg">
</a>

See the example here :

The image isn;t shown and on clicking it, a page opens saying:

Referral Denied

You don't have permission to access "https://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3.jpg" on this server.

Reference #24.84d81002.1393249598.981ca7

On going through this question, I came to know that IMDb does not allow referrals to any other sites. The solution was to get the image yourself, save it and then serve it yourself. For doing that, I followed this link.

I made a datastore object storing various informations of the movie as well as the poster which was fetched using urlfetch api. relevant parts of the code are

from google.appengine.ext import db
from google.appengine.api import urlfetch

class Movie(db.Model):
    title = db.StringProperty()
    poster = db.BlobProperty(default=None)

coverurl = "https://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3.jpg"
movie = Movie()
posterimg = urlfetch.fetch(coverurl).content
movie.cover = db.Blob(posterimg)
movie.put()

Now, for rendering the html page, I got the movie object from datastore as

m = Movie.gql("WHERE title = :1", title).get()
img = m.posterimg

and the html page was rendered as

imghtml = ' <img src="' + img + '"> </a>'

An error was expected and was recieved as:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

What is the way out of this? How can I include an Image which is stored as a blob in Datastore in a html image?

解决方案

upload image to blobstore using the app engine helpers, get the serving_url in the same handler, save the serving_url with your image object, refer to the image url in your html template

e.g.

from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.api.images import get_serving_url

class Handler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files = self.get_uploads('file')
        blob_info = upload_files[0]
        img = MyImageObject()
        img.image_name = blob_info.filename
        img.image_url = get_serving_url(blob_info.key())
        img.put()

then in the template

<img src="{{img.image_url}}">

to get original size of image you will need to append =s0 to end of url, append =sXX to resize, see "URL Modifications" section here https://developers.google.com/appengine/docs/python/images/functions

这篇关于在数据存储区中作为Blob存储在html页面中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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