String jsonRet = null;
if (width == null) { // first time
BlobInfo info = new BlobInfoFactory().loadBlobInfo(blobKey);
ImagesService imagesService = ImagesServiceFactory
.getImagesService();
// Try to fetch image data
byte[] bytes;
try {
bytes = blobstoreService.fetchData(blobKey, 0, info.getSize());
}
catch ( Exception e){
// If not possible, first resize to acceptable size
Image bigImage = ImagesServiceFactory.makeImageFromBlob(blobKey);
Transform initialResize = ImagesServiceFactory.makeResize(1024, 4000);
Image resizedImage = imagesService.applyTransform(initialResize, bigImage);
bytes = resizedImage.getImageData();
}
// We now have an acceptable image for further processing
Image oldImage = ImagesServiceFactory.makeImage(bytes);
Integer intWidth = oldImage.getWidth();
Integer intHeight = oldImage.getHeight();
// step 1: create new image with white background and center image
// in middle; return;
// create new image with white background and center image in
// middle; return;
// trying composite
if (intWidth < targetWidth && intHeight < targetHeight) {
status = "1";
blobKey = getBlobKeyImage(
blobKey,
(setImageMiddle(intWidth, intHeight, oldImage,
imagesService)).getImageData());
// preparing returnJSON
String[] json = { blobKey.getKeyString(), status };
jsonRet = new Gson().toJson(json);
}
// step 2: rescale image to targetWidth
else if (intWidth > targetWidth) {
// if ( true) {
Transform resize = ImagesServiceFactory.makeResize(targetWidth,
4000);
Image newImage = imagesService.applyTransform(resize, oldImage);
// getting the new height
intHeight = newImage.getHeight();
intWidth = newImage.getWidth();
if (intHeight > targetHeight) { // step3: cropping the image
status = "3";