Hi all,

Some months ago I deployed a Plone 4 site for a car club at
http://www.vdubmexico.com.

We've recently had some issues with varnish and Guru Meditation randomly
thrown at logged in users and also to some anonymous users. So, besides to
increase the various timeout parameters of varnish, I tought that I should
also start to look at my code, as I know I did some tricks that might have a
negative impact on performance.


For example, on the theme product. I added some custom views for
IATImage that clips the image (using PIL) in some ways the default Plone
resized images didn't fit very well for us. This is one of the special
views:

on browser/configure.zcml

  <browser:page
      for="Products.ATContentTypes.interfaces.IATImage"
      name="vdub_image_preview"
      class=".views.vdubImagePreview"
      permission="zope2.View"
      layer=".interfaces.IThemeSpecific"
      />

and in browser/views.py

class vdubImagePreview(BrowserView):
    scale_x = 244
    scale_y = 115

    def _make_headers(self,nw_imag,imag_format,imag_filename):
        out_file = StringIO()
        nw_imag.save(out_file,format=imag_format)
        response = self.request.response
        response.setHeader('Content-type',
                            'image/%s'%(imag_format).lower())
        response.setHeader('Content-Disposition',
                            'inline;filename=%s'%imag_filename)
        response.setHeader('Content-Lenght',
                            len (out_file.getvalue()))
        return out_file.getvalue()

    def _calculate_ratio(self,img_size):
        """
        Calcular la razon de escalamiento cuando la imagen es mas ancha que
alta
        """
        xx,yy = img_size
        return float(self.scale_x)/xx

    def _calculate_ratio_y(self,img_size):
        """Calcular la razon de escalamiento cuando la imagen es mas ancha
que alta
        """
        #que pex?? esto es igual a lo anterior!
        xx,yy = img_size
        return float(self.scale_x)/xx

    @memoize
    def __call__(self):
        image = self.context.getImage()
        pil_img = PIL.Image.open(StringIO(str(image.data)))

        #Different ratio!
        xx,yy = pil_img.size
        if yy > xx:
            #La imagen es mas ancha que alta
            ratio = self._calculate_ratio_y(pil_img.size)
        else:
            ratio = self._calculate_ratio(pil_img.size)

        #rescale to new ratio
        nw_image = pil_img.resize((int(xx*ratio),int(yy*ratio)),
                                  PIL.Image.ANTIALIAS)

        #Crop from the upper left corner
        nw_image = nw_image.crop((0,0,self.scale_x,self.scale_y))

        return self._make_headers(nw_image,pil_img.format, image.filename)



I'm using this custom views in the footer, so practically every user that
visits the portal hits the plone instance, even though I've deployed a basic
Apache->Varnish->Plone setup.

I'm probably doing some stupid things... So, is there a better way to
implement this functionality?

---
Noe
_______________________________________________
Product-Developers mailing list
[email protected]
http://lists.plone.org/mailman/listinfo/product-developers

Reply via email to