Search code examples
pythongoogle-app-enginegoogle-cloud-platformwebapp2google-cdn

Size Parameter doesn't work on Google CDN for images


I am using webApp2 and Google App Engine for a simple app and images and blobstore api to serve uploaded images using Google CDN. I am using the following code to generate the Serving Url for images -

from google.appengine.api import images
from google.appengine.ext import blobstore

def _get_urls_for(self, file_name):
    user = users.get_current_user()
    if user is None:
        return

    bucket_name = app_identity.get_default_gcs_bucket_name()
    path = os.path.join('/', bucket_name, user.user_id(), file_name)
    real_path = '/gs' + path
    key = blobstore.create_gs_key(real_path)
    url = images.get_serving_url(key, size=0)
    thumbnail_url = images.get_serving_url(key, size=150, crop=True)
    return url, thumbnail_url

It gives me the url as following (as example) -

http://localhost:8080/_ah/img/encoded_gs_file:YXBwX2RlZmF1bHRfYnVja2V0LzE4NTgwNDc2NDIyMDEzOTEyNDExOC9pbWc4NzYuanBn=s32

and

http://localhost:8080/_ah/img/encoded_gs_file:YXBwX2RlZmF1bHRfYnVja2V0LzE4NTgwNDc2NDIyMDEzOTEyNDExOC9pbWc4NzYuanBn=s150-c

Both the urls defer by just the last size parameter (=s150-c) which should crop the image in the thumbnail_url to 150 px as it's largest dimension. I can confirm that by referring to the official documentation here. However the parameter doesn't affect the size of image at all. I am still getting full size image, even after adding the parameter. I have already tried to remove -c and tried with various sizes.
I have tried few arguments from this list as well, which unfortunately doesn't affect the image at all.

Any idea what could be the reason of this behaviour?


Solution

  • After tracing the log, I found out that it happened due to PIL not being installed locally. If the transformation needs to be done on local server, then PIL needs to be installed. I chose to install Pillow by using pip install Pillow -t lib/ and then added the following to app.yaml

    libraries:
    - name: PIL
      version: "1.1.7"
    

    Then added an appengine_config.py file in root of my project where lib folder resides and added the following code to it.

    # appengine_config.py
    from google.appengine.ext import vendor
    
    # Add any libraries install in the "lib" folder.
    vendor.add('lib')
    

    And it started to work magically.