In the previous post, we showed how to upload images to a Cloudinary server. In this part, we will play with some of the features we see on the WhatsApp technology. After you or your users have uploaded image assets to Cloudinary, you can deliver them via dynamic URLs. You can include instructions in your dynamic URLs that tell Cloudinary to manipulate your assets using a set of transformation parameters. All image manipulations and image optimizations are performed automatically in the cloud and your transformed assets are automatically optimized before they are routed through a fast CDN to the end user for an optimal user experience. For example, you can resize and crop, add overlays, blur or pixelate faces, apply a variety of special effects and filters, and apply settings to optimize your images and to deliver them responsively.
Here are a few examples of the commonly used image transformation features, along with links to their more detailed documentation :
- Resizing and cropping
- Converting to another image format
- Applying image effects and filters
- Adding text and image overlays
- Image optimizations
We won’t be going in detail about all the listed features, as there are still a lot of them to be explored. However we’ve attached links to each of them should you decide to read further on your own. Now, with our
MediaManager already initialized, we’ll quickly resize and crop our uploaded image in the
onCreate() method. Here’s how :
MediaManager.get().url().transformation(new Transformation().width(250).height(250).gr avity("faces").crop("fill")).generate("selected_image.jpg")
This example uses the
fill cropping method to generate and deliver an image that completely fills the requested 250x250 size while retaining the original aspect ratio. It uses face detection gravity to ensure that all the faces in the image are retained and centered when the image is cropped
WhatsApp technology doesn’t provide options like applying effects and filters just like Instagram. We can add this feature to our clone using Cloudinary:
MediaManager.get().url().transformation(new Transformation() .effect("cartoonify").chain() .radius("max").chain() .effect("outline:100").color("lightblue").chain() .background("lightblue").chain() .height(300).crop("scale")).generate("selected_image.jpg")
The code above applies a cartoonify effect, rounding corners effect, and background color effect (and then scales the image down to a height of 300 pixels).
There is a lot more you can do with Cloudinary image manipulations; you can even apply transformations based on certain conditions.
With Cloudinary’s conditional transformations, images are manipulated on-the-fly using dynamic delivery URLs for any uploaded image. A condition and its associated transformations are added to the URLs using the
if parameter which accepts a string value detailing the condition to evaluate. You can apply a transformation based on the image's width, height, aspect ratio, the number of faces in the image (if any) or the number of frames (for animated images) or pages (for PDFs) present. For example, we can evaluate whether our uploaded image's width is greater than 500 pixels with
if_w_gt_500. Also, multiple conditions can be evaluated by concatenating the conditions with an
or operator, and a different transformation can be applied in the case that the condition is evaluated as negative by using the
if_else parameter. Now let’s manipulate our uploaded image such that
if it contains a face, we zoom into the face otherwise, we fit the entire image into the defined space.
Here’s how :
Head back inside the
onCreate() method in the MainActivity Class and define the transformations we just described using the
MediaManager as thus :
// define transformation from MediaManager MediaManager.get().url().transformation(new Transformation() //if a face is detected .if("fc_gte_1").chain() //zoom into the face with the defined parameters .width(200).height(200).gravity("face").crop("thumb").chain() //if a face is not detected, fit the image into the defined params .if("else").width(200).height(200).crop("fit").chain() //end transformation .if("end").chain() .radius(40).border("4px_solid_black")).generate("selected_image.jpg")
By default, Cloudinary automatically performs certain optimizations on all transformed images. There are also a number of additional features that enable you to further optimize the images you use in your Android application. These include optimizations to image quality, format and size, among others.
For example, you can use the
auto value for the
quality attributes to automatically deliver the image in the format and quality that minimize file size while meeting the required quality level. Below, these two parameters are applied, resulting in a 50 percent file size reduction (1.4MB vs. 784KB) with no visible change in quality.
MediaManager.get().url().transformation(new Transformation().quality("auto").fetchForm at("auto")).generate("selected_image.webp")
You can deliver any image uploaded to Cloudinary in essentially any image format. There are two major ways to convert and deliver in another format:
- Specify the image's public ID with the desired extension.
- Explicitly set the desired format using the
fetchFormatparameter. Specifying the image's public ID with the desired extension is primarily the easiest way to convert it to another format. let’s take for example that we want to change the format of our uploaded image to a
gif, we’ll simply specify it in the image delivery URL as thus :
Yeah, it’s that simple. We can also achieve the same result with the
fetchFormat parameter. this is a bit trickier, but equally as simple.
MediaManager.get().url().transformation(new Transformation().width(350).crop("scale")) .format("gif").generate("selected_image.jpg")
All we had to do was set the desired format to be a
Cloudinary enables you to easily transform your images on-the-fly to any required format, style and dimension, and also optimizes images to have the minimal file size for an improved user experience and for saving bandwidth. You can do this by implementing dynamic image transformation and delivery URLs for accessing the images. You can change the required transformations at any time and all transformed images will be created on-demand (lazily) and delivered to your users through a fast CDN with optimized caching.
Cloudinary's image management service supports the following image transformation and delivery capabilities:
- Deliver images using dynamic Cloudinary URLs
- Embed images in web pages
- Resize image dimensions and crop to match your graphic design
- Detect faces in images for automatic cropping, resizing, overlay placement and using visual effects
- Convert between image formats
- Adjust image quality
- Modify the image shape and style
- Apply effects to images
- Add image and text overlays
- Chain transformations together
- Name custom transformations
- Optimize images
- Deliver responsive images
- Fetch images from remote locations
- Control access to your images
- Use advanced URL delivery options
- Create images from PDF files
- Manage animated GIFs
- Add flags to control transformation behavior
- Apply transformations only if a specified condition is met
- Use arithmetic expressions to adjust the value of numeric parameters or user-defined variables
- Define and use user-defined variables in your transformations These features were also listed with their respective links to provide you the flexibility to read further on your own.
Cloudinary is a cloud-based service that provides an end-to-end image and video management solution. The Android SDK provides simple, yet comprehensive file upload, administration, manipulation, optimization, and delivery capabilities. These can be implemented using code that integrates seamlessly with your existing Android application. You can leverage these awesome capabilities and deliver amazing media solutions to your subscribers and have them thank you later. DEMO