Workaround for get_serving_url doesn't support width / height 'size parameter?

Currently get_serving_url does not support resizing / resizing images independently, instead only allowing square dimensions using the size parameter.

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

Can anyone suggest an effective workaround for this? It would seem like using images.resize () is the way to go, but it doesn't require re-storing the transformed image in the block store to get the blob_key for get_serving_url?

Hopefully there is a solution that is effective enough not to override the speed advantage of blobstore!

Thanks everyone.

+3


source to share


2 answers


what you need to do is calculate the aspect ratio of the original image to resize it to your desired width and height, after which you should have a calculated size value to go 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 the side is what you use for your serving url

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

+4


source


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

Yay!

From the post: "This is actually implemented, but I haven't seen documented anywhere:



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

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

+4


source







All Articles