Skip to content

Build a WhatsApp Clone with Automatic Image Optimization

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 transform your assets using a set of transformation parameters. All image transformations 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 :

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")
Code language: CSS (css)

This example uses the fill cropping method to generate and deliver an image that completely fills the requested 250×250 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")
Code language: CSS (css)

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 transformations; you can even apply transformations based on certain conditions.

With Cloudinary’s conditional transformations, images are transformed 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 and or 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 transform 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")
Code language: JavaScript (javascript)

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 fetchFormat and 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")
Code language: CSS (css)

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 fetchFormat parameter. 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 :
MediaManager.get().url().generate("selected_image.gif")
Code language: CSS (css)

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")
Code language: CSS (css)

All we had to do was set the desired format to be a gif.

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:

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, transformation, 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

Back to top

Featured Post