为什么在这个例子中图像高度变得如此之大? [英] Why does image height get so large in this example?

查看:136
本文介绍了为什么在这个例子中图像高度变得如此之大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在计算图片高度serverside为动态图片提供服务,但每隔一段时间HTML中的高度就会变得太大:

< div class =item_imagestyle =height:1116px>



为什么当照片显示为1116像素具有 get_serving_url 的服务器实际上不是1116px,所以我可能在我的算法中犯了一些错误:

  class NewAdHandler(BaseHandler):
$ b $ get_ad(self,key):
data = memcache.get(key)
如果数据不是None :
返回数据
else:
data = Ad.get_by_id(long(key))
memcache.add(key,data,6000)
返回数据
$ b $ def get_image(self,ad):
data = memcache.get(str(ad.key()))
如果数据不是无:
返回数据
else:
data = ad.matched_images.get()
memcache.add(s tr(ad.key()),data,6000)
返回数据

def get_images(self,ad):
data = memcache.get(str(ad.key ())+'images')
如果数据不是无:
返回数据
else:
data = ad.matched_images
memcache.add(str(ad .key())+'images',data,6000)
返回数据

def get(self,id,html):
logging.debug('logged in% s',self.logged_in)
region = None
city = None
ad = self.get_ad(id)
if not ad or not ad.published:
self.error(404)
return
image = self.get_image(ad)
thumb_url = None
height = None#image.get_height()
#logging。 debug('height%d',height)
maxheight = 80

if image:
img = images.Image(blob_key = str(image.primary_image.key()) )
img.im _feeling_lucky()
img.execute_transforms(output_encoding = images.JPEG)
height = img.height
if height< 640:maxheight =高度


logging.debug('height%d',height)

image_url = None
country =''
if self.request.host.find('koolbusiness.com')> -1:country ='印度'
elif self.request.host.find('montao.com')> -1:country ='Brasil'
elif self.request.host.find('hipheap.com')> -1:country ='USA'
if image:
if image.primary_image:
try:
image_url = \
images.get_serving_url(str(image。
size = 640)
thumb_url = \
images.get_serving_url(str(image.primary_image.key()),
size = 120)
除了例外,e:
image_url ='/ images /'+ str(image.key().id())\
+'_small.jpg'
else :
image_url ='/ images /'+ str(image.key().id())\
+'_small.jpg'
imv = []
for我在self.get_images(ad):
img = images.Image(blob_key = str(i.primary_image.key()))

img.im_feeling_lucky()
img .execute_transforms(邻utput_encoding = images.JPEG)
#maxheight = img.height
#if img.height< 640:maxheight = 640
if img.height> maxheight:maxheight = img.height
if i.primary_image:
try:
i1 = \
images.get_serving_url(str(i.primary_image.key()))
imv.append(i1)
除了例外,e:
i1 ='/ images /'+ str(image.key().id())\
+' _small.jpg'
imv.append(i1)
price = ad.price
if ad.geopt and not ad.city:
logging.info('geopt:'+ str(ad.geopt))
url ='http://maps.googleapis.com/maps/api/geocode/json'\
+'?latlng = {},{}& sensor = false'.format(ad.geopt.lat,
ad.geopt.lon)
result = urlfetch.fetch(url)
jsondata = json.loads(result.content)

为jsondata ['results']中的结果:
为re中的组件sult ['address_components']:
如果'administrative_area_level_1'\
在组件['types']中:
region = component ['long_name']。replace('County'
'')
如果'locality'在组件['types']中:
city = component ['long_name']

如果ad.city!= city:
ad.city = city
ad.put()

if ad.region!= region:
ad.region = region
ad.put( )

如果ad.price:#并且不包含分隔符
try:
price = \
i18n.I18n(self.request).format_decimal( int(ad.price))
除了例外,e:
price = ad.price
else:
city = ad。 city
region = ad.region#if ad.region eller get region

if region == None:
region =''

if region ==无:
region =''
regionentity = montaomodel.Region.all()。filter('name =',$ b $ region).get()
cityentity = montaomodel .City.all()。filter('name =',city).get()
logging.debug('is logged in:%s',self.logged_in,)
self.render_jinja(
'view_ad',
image_url = image_url,loggedin = self.logged_in,user = self.current_user,
country = country,
region = ad.region,
regionentity = regionentity,
cityentity = cityentity,
city = city,
imv = imv,request = self.request,
len = len(imv),height = maxheight,
ad = ad,thumb_url = thumb_url,
price = price,VERSI ON = VERSION,
#user_url =(users.create_logout_url(self.request.uri)if users.get_current_user()else None),
admin = users.is_current_user_admin(),
linebreak_txt = (ad.text.replace('\ n','< br>'
)if ad.text else None),
image = image,
logged_in = self.logged_in,
form = AddAdCityForm(),
form_url = self.request.url,

模板中的代码是

  {%if image_url%} 

<! - 主图像 - > < div class =item_imagestyle =height:{{height}} px>

< div class =item_arrow_leftstyle =height:428px> < i class =sprite_vi_arrow_leftstyle =margin-top:200px>< / i> < / DIV> < div class =image_container> < img src ={{image_url}}id =main_image_0alt ={{ad.title}}title =点击查看下一张图片> {%endfor%}

你能告诉我我能做些什么吗?此问题的实时视图此处

解决方案

为什么要指定height:1116px ... height:auto应该照顾你需要的东西吗?



感谢
AB


I'm calculating image height serverside to serve dynamic images but every once in a while the height in HTML becomes way too large:

<div class="item_image" style="height: 1116px">

Why is it so much as 1116px when the pics that a server with get_serving_url actually are not 1116px so I'm probably making some mistake in my algorithm:

class NewAdHandler(BaseHandler):

    def get_ad(self, key):
        data = memcache.get(key)
        if data is not None:
            return data
        else:
            data = Ad.get_by_id(long(key))
            memcache.add(key, data, 6000)
            return data

    def get_image(self, ad):
        data = memcache.get(str(ad.key()))
        if data is not None:
            return data
        else:
            data = ad.matched_images.get()
            memcache.add(str(ad.key()), data, 6000)
            return data

    def get_images(self, ad):
        data = memcache.get(str(ad.key()) + 'images')
        if data is not None:
            return data
        else:
            data = ad.matched_images
            memcache.add(str(ad.key()) + 'images', data, 6000)
            return data

    def get(self, id, html):
        logging.debug('logged in %s', self.logged_in)
        region = None
        city = None
        ad = self.get_ad(id)
        if not ad or not ad.published:
            self.error(404)
            return
        image = self.get_image(ad)
        thumb_url = None
        height = None#image.get_height()
        #logging.debug('height %d', height)
        maxheight = 80

        if image:
            img = images.Image(blob_key=str(image.primary_image.key()))
            img.im_feeling_lucky()
            img.execute_transforms(output_encoding=images.JPEG)
            height = img.height
            if height < 640: maxheight = height


        logging.debug('height %d', height)

        image_url = None
        country = ''
        if self.request.host.find('koolbusiness.com') > -1: country = 'India'
        elif self.request.host.find('montao.com') > -1: country = 'Brasil'
        elif self.request.host.find('hipheap.com') > -1: country = 'USA'
        if image:
            if image.primary_image:
                try:
                    image_url = \
                        images.get_serving_url(str(image.primary_image.key()),
                            size=640)
                    thumb_url = \
                        images.get_serving_url(str(image.primary_image.key()),
                            size=120)
                except Exception, e:
                    image_url = '/images/' + str(image.key().id()) \
                        + '_small.jpg'
            else:
                image_url = '/images/' + str(image.key().id()) \
                    + '_small.jpg'
        imv = []
        for i in self.get_images(ad):
            img = images.Image(blob_key=str(i.primary_image.key()))

            img.im_feeling_lucky()
            img.execute_transforms(output_encoding=images.JPEG)
            #maxheight = img.height
            #if img.height < 640: maxheight = 640
            if img.height > maxheight: maxheight = img.height
            if i.primary_image:
                try:
                    i1 = \
                        images.get_serving_url(str(i.primary_image.key()))
                    imv.append(i1)
                except Exception, e:
                    i1 = '/images/' + str(image.key().id()) \
                        + '_small.jpg'
                    imv.append(i1)
        price = ad.price
        if ad.geopt and not ad.city:
            logging.info('geopt:' + str(ad.geopt))
            url = 'http://maps.googleapis.com/maps/api/geocode/json' \
                + '?latlng={},{}&sensor=false'.format(ad.geopt.lat,
                    ad.geopt.lon)
            result = urlfetch.fetch(url)
            jsondata = json.loads(result.content)

            for result in jsondata['results']:
                for component in result['address_components']:
                    if 'administrative_area_level_1' \
                        in component['types']:
                        region = component['long_name'].replace('County'
                                , '')
                    if 'locality' in component['types']:
                        city = component['long_name']

            if ad.city != city:
                ad.city = city
                ad.put()

            if ad.region != region:
                ad.region = region
                ad.put()

            if ad.price:  # and doesn't contain separators
                try:
                    price = \
                        i18n.I18n(self.request).format_decimal(int(ad.price))
                except Exception, e:
                    price = ad.price
        else:
            city = ad.city
            region = ad.region  # if ad.region eller get region

        if region == None:
            region = ''

        if region == None:
            region = ''
        regionentity = montaomodel.Region.all().filter('name =',
                region).get()
        cityentity = montaomodel.City.all().filter('name =', city).get()
        logging.debug('is logged in: %s', self.logged_in,)
        self.render_jinja(
            'view_ad',
            image_url=image_url,loggedin=self.logged_in,                 user= self.current_user,
           country = country,
            region=ad.region,
            regionentity=regionentity,
            cityentity=cityentity,
            city=city,
            imv=imv,request=self.request,   
            len=len(imv),height=maxheight,
            ad=ad,thumb_url=thumb_url,
            price=price,VERSION=VERSION,
            #user_url=(users.create_logout_url(self.request.uri) if users.get_current_user() else None),
            admin=users.is_current_user_admin(),
            linebreak_txt=(ad.text.replace('\n', '<br>'
                           ) if ad.text else None),
            image=image,
            logged_in = self.logged_in,
            form=AddAdCityForm(),
            form_url=self.request.url,
            )

The code in the template is

 {% if image_url %} 

        <!-- Main Images -->    <div class="item_image" style="height: {{height}}px">

            <div class="item_arrow_left" style="height: 428px">             <i class="sprite_vi_arrow_left" style="margin-top: 200px"></i>      </div>      <div class="image_container">       <img src="{{image_url}}" id="main_image_0" alt="{{ad.title}}" title="Click for next image">             {% for im in imv %}     <img src="{{im}}" class="hidden" id="main_image_{{loop.index}}" alt="{{ad.title}}" class="thumb_image_single" title="Click for next image">
            {% endfor %}

Can you tell me what I can do about the problem? A live view of the problem is here.

解决方案

Why you want to specify height:1116px...height:auto should take care of what you need right?

Thanks AB

这篇关于为什么在这个例子中图像高度变得如此之大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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