Transforming An Image Using The ImagesService

Here’s a short code example demonstrating how to transform images using the images service. This code example resizes an image to dimensions of 100×100, figures out the MIME type of the image, and prepares to write the image data into a write channel (good for writing to Cloud Storage or another storage service).

The image_content object represents a byte[] array containing image data. This code snippet then creates two objects: image_mime (the MIME type for the resized image) and buffer (a ByteBuffer containing the resized image’s data, ready for writing to a write channel).

// Create the image from the byte array
Image image = ImagesServiceFactory.makeImage(image_content);
// Resize the image to 100x100
Transform resize = ImagesServiceFactory.makeResize(100, 100);
ImagesService images = ImagesServiceFactory.getImagesService();
image = images.applyTransform(resize, image);
//Figure out the format of the image, and build a mime type.
Image.Format format = image.getFormat();
String image_mime = "image/" + format.toString().toLowerCase();
ByteBuffer buffer = ByteBuffer.wrap(image.getImageData());