开发者

GoogleAppEngine: how to prevent image resize failure for big images

开发者 https://www.devze.com 2023-04-08 14:36 出处:网络
I\'ve got to manage some images taken from the client. Up to now, my code is the following: Image oldImage = null;

I've got to manage some images taken from the client.

Up to now, my code is the following:

    Image oldImage = null;
    try {
        oldImage = ImagesServiceFactory.makeImage(imageBuf);
    } catch (Exception e) {
        log.severe("Cannot make the image due to " + e);
        return;
    }
    int newWidth = ??; //how can I guess this value to reach a size < 1MB??
    int newHeight= ??; //how can I guess this value to reach a size < 1MB??
    Transform resize = ImagesServiceFactory.makeResize(newWidth, newHeight);
    Image newImage = imagesService.applyTransform(resize, oldImage); //Exception if > 1MB

Such images may exceed the size limit of GAE (1 MB), and I'd like to resize or crop them in order to reach a size of little less than 1 MB (say, 950kB).

How can I achieve this goal?

I cannot directly use the resize method, as I don't know the exact width & height which the image should be resized to.

Moreover, depending on the encoding of the picture (png, jpeg, etc.) the size of the i开发者_开发技巧mage may vary also using fixed height and width.

I really have no efficient ideas on how to face this issue: the only way I can see a solution is to continue trying with lower and lower dimensions until I get a good-sized image, but this is obviously not acceptable in terms of performances...

Please, help me!!

Thank you very much

Bye

cghersi


If you store the images in the blobstore, you can resize much larger images than you can passing the image through your appserver, by using the getServingUrl() function. Since this does not pass its data through the API, it's not limited by the API response size limits.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号