kelly Nguyen
kelly Nguyen

Reputation: 41

GAE - get Image width and height on server side

I'm working with an application like an image gallery. This allow user select file from his computer and upload the image. After upload the image, that image will be display in a gallery. I save the image in blobstore, and get it by blobkey. I wan to get a thumbnail image (that was resized from thre original image) On server side, I try to use image api to resize te original one based on it's blobkey, like this

public String getImageThumbnail(String imageKey) {
    BlobKey blobKey = new BlobKey(imageKey);
    Image oldImage = ImagesServiceFactory.makeImageFromBlob(blobKey);
    /*
    * can not get width anf height of "oldImage"
    * then I try to get imageDate of old Image to create a new image like this
    */
    byte[] oldImageData = oldImage.getImageData();

   Image newImage1 = ImagesServiceFactory.makeImage(oldImageData);
/*
* however
* oldImage.getImageData(); return null/""
* and cause the Exception when call this ImagesServiceFactory.makeImage(oldImageData)
*/

}

Are there anyone had work with image api on server side, please let's me know how to get width and height of an image that create from a blobkey or an byte[]

Upvotes: 3

Views: 1304

Answers (2)

kelly Nguyen
kelly Nguyen

Reputation: 41

Thanks all for reading I've solved my problem using this

    ImagesService imagesService = ImagesServiceFactory.getImagesService();
    BlobKey blobKey =  new BlobKey(imageKey);
    BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey);
    byte[] bytes = blobstoreService.fetchData(blobKey, 0, blobInfo.getSize());
    Image oldImage = ImagesServiceFactory.makeImage(bytes);
    int w = oldImage.getWidth();
    int h = oldImage.getHeight();

Upvotes: 1

Igor Artamonov
Igor Artamonov

Reputation: 35951

Image object have height/weight details:

int width = oldImage.getWidth();
int height = oldImage.getHeight();

Upvotes: 0

Related Questions