Cloudinary Blog

Build a WhatsApp Clone with Automatic Image Optimization

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 :

Resizing and Cropping :

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 :

Copy to clipboard
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

Applying Effects and Filters

WhatsApp technology doesn’t provide options like applying effects and filters just like Instagram. We can add this feature to our clone using Cloudinary:

Copy to clipboard
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 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 :

Copy to clipboard
// 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")

Image Optimizations

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.

Copy to clipboard
MediaManager.get().url().transformation(new Transformation().quality("auto").fetchForm
at("auto")).generate("selected_image.webp")

Convert image to another format

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 :
Copy to clipboard
MediaManager.get().url().generate("selected_image.gif")

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.

Copy to clipboard
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 gif.

Cloudinary Transformation and Delivery capabilities

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:

Conclusion

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

Recent Blog Posts

Our $2B Valuation

By
Blackstone Growth Invests in Cloudinary

When we started our journey in 2012, we were looking to improve our lives as developers by making it easier for us to handle the arduous tasks of handling images and videos in our code. That initial line of developer code has evolved into a full suite of media experience solutions driven by a mission that gradually revealed itself over the course of the past 10 years: help companies unleash the full potential of their media to create the most engaging visual experiences.

Read more
Direct-to-Consumer E-Commerce Requires Compelling Visual Experiences

When brands like you adopt a direct–to-consumer (DTC) e-commerce approach with no involvement of retailers or marketplaces, you gain direct and timely insight into evolving shopping behaviors. Accordingly, you can accommodate shoppers’ preferences by continually adjusting your product offering and interspersing the shopping journey with moments of excitement and intrigue. Opportunities abound for you to cultivate engaging customer relationships.

Read more
Automatically Translating Videos for an International Audience

No matter your business focus—public service, B2B integration, recruitment—multimedia, in particular video, is remarkably effective in communicating with the audience. Before, making video accessible to diverse viewers involved tasks galore, such as eliciting the service of production studios to manually dub, transcribe, and add subtitles. Those operations were costly and slow, especially for globally destined content.

Read more
Cloudinary Helps Minted Manage Its Image-Generation Pipeline at Scale

Shoppers return time and again to Minted’s global online community of independent artists and designers because they know they can count on unique, statement-making products of the highest quality there. Concurrently, the visual imagery on Minted.com must do justice to the designs into which the creators have poured their hearts and souls. For Minted’s VP of Engineering David Lien, “Because we are a premium brand, we need to ensure that every single one of our product images matches the selected configuration exactly. For example, if you pick an 18x24 art print on blue canvas, we will show that exact combination on the hero images in the PDF.”

Read more
Highlights on ImageCon 2021 and a Preview of ImageCon 2022

New year, same trend! Visual media will continue to play a monumental role in driving online conversions. To keep up with visual-experience trends and best practices, Cloudinary holds an annual conference called ImageCon, a one-of-a-kind event that helps attendees create the most engaging visual experiences possible.

Read more