Cerzi
Cerzi

Reputation: 830

Workaround for get_serving_url not supporting width/height 'size' param?

the get_serving_url currently does not support resizing image width/height independently, instead only allowing square resizes using the 'size' param.

http://code.google.com/p/googleappengine/issues/detail?id=4200

Can anyone suggest an efficient workaround for this? It would seem like using images.resize() is the way to go, but would that not require re-storing the transformed image in the blobstore to get a blob_key for get_serving_url?

Hopefully there is a solution that is efficient enough to not undo the speed benefit of using the blobstore!

Thanks all.

Upvotes: 3

Views: 457

Answers (2)

glo
glo

Reputation: 799

https://code.google.com/p/googleappengine/issues/detail?id=4200#c18

Yay!

From the post: "It's actually implemented but I didn't see it documented anywhere:

  • length of longest dimension: =s180
  • width: =w200
  • height: =h150
  • width and height: =w200-h150
  • cropping with size: =s200-c
  • cropping with width/height: =w200-h150-c
  • rotate: =r90
  • combine rotation: =w200-h150-r90-c

Here is an example: http://lh4.ggpht.com/TgjDT-cPRr6bjrpSVQeILk93o4Ouzjo1ygMB6KpmnCHhyH5vJKKRrqxCD2bC3T09CRIP6h5QFsV_l0hnhio5bN7z=h200-w300-r180-c"

Upvotes: 5

aschmid00
aschmid00

Reputation: 7158

what you need to do is calculating the proportions of the original image to resize it to your desired width and height, after that you should have the calculated size value to pass to the serving url and get what you want.

    resolution = '1200x900'
    blob_width, blob_height = resolution.split('x')

    blob_width  = float(blob_width)
    blob_height = float(blob_height)

    width  = float(width)
    height = float(height)
    blob_prop  = blob_width / blob_height

    req_box_prop = width / height

    if req_box_prop == blob_prop:
        scale_factor = blob_width / width
        serving_img_height = blob_width / scale_factor
        serving_img_width  = blob_height / scale_factor

    if req_box_prop < blob_prop:
        serving_img_width  = width
        serving_img_height = width / blob_prop

    else:
        serving_img_width  = height * blob_prop
        serving_img_height = height


    serving_img_width  = int(round(serving_img_width, 0)) 
    serving_img_height = int(round(serving_img_height, 0))

    # use serving urls
    side = max(serving_img_width, serving_img_height)

and side is what you use for your serving url

'http://yourservingurl=s%s'%side

Upvotes: 4

Related Questions