Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 630
Cerzi Avatar asked May 24 '26 04:05

Cerzi


2 Answers

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"

like image 180
glo Avatar answered May 25 '26 18:05

glo


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

like image 29
aschmid00 Avatar answered May 25 '26 16:05

aschmid00