Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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[]

like image 881
kelly Nguyen Avatar asked Nov 19 '25 17:11

kelly Nguyen


1 Answers

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();
like image 127
kelly Nguyen Avatar answered Nov 24 '25 20:11

kelly Nguyen