Java image transformations
Last updated: Sep-02-2024
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 transformations 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 optimal user experience.
For example, you can resize and crop, add overlay images, blur or pixelate faces, apply a large variety of special effects and filters, and apply settings to optimize your images and to deliver them responsively.
Cloudinary's Java SDK simplifies the generation of transformation URLs for easy embedding of assets in your Java application.
Deliver and transform images
You can deliver your images using methods that generate image tags or via direct URL-building directives.
The image tag
You can add images to your Java Enterprise Edition view using Cloudinary's image
tag, or using the imageTag
method of the core Java library. Both methods generate the full image resource URL based on the given transformation parameters and wraps it with an HTML image tag with the appropriate attributes:
For example, displaying the uploaded image with the sample public ID, while providing an alternate text:
If cloud_name
is configured to be demo
, this tag is equivalent to:
You can use the image
tag or imageTag
method to show transformed versions of your uploaded images by adding transformation instructions. For example, displaying the 'sample' image resized to fill a 100x150 area:
This would generate the following HTML:
Additionally, you can add other, non-transformation parameters to the image
tag such as the asset version, configuration parameters and HTML5 image tag attributes. Using the core Java library, pass any HTML5 image tag attributes directly to the map in the imageTag
method, and pass other non-transformation parameters to methods of the url
class.
- The
version
parameter is added to the delivery URL as explained in Asset versions. - Configuration parameters that you specify here override any that you have set globally.
-
HTML5 image tag attributes (e.g., alt or class) are added to the resulting
<img>
tag.
is compiled to:
In general, when using an SDK, you will probably take advantage of the SDK parameter names for improved readability and maintenance of your code. However, you can also optionally pass a raw_transformation parameter, whose value is a literal URL transformation definition. Note that the string you pass as the raw transformation value will be appended as is (with no processing or validation) to the end of any other transformation parameters passed in the same component of the transformation chain.
For example:
Chaining transformations
Cloudinary supports powerful transformations. You can even combine multiple transformations together as part of a single transformation request, e.g. crop an image and add a border. In certain cases you may want to perform additional transformations on the result of the previous transformation request. To do that, you can use chained transformations.
To apply chained transformations in a transformation URL, you include multiple transformation components separated by '/'. Each transformation component is applied to the result of the previous one. When using the image
tag the first transformation's parameters are specified inline with the tag's attributes and additional transformations are specified using a transformation
tag inside a <jsp:attribute name="transformation">
tag. When using the client library directly, the transformation
object has a method named chain
which allows chaining transformations. The following example first fills the image within a 250px square, then rounds the result to a circle, and finally delivers the image in the optimal transparent format:
![3 chained transformations applied to an image 3 chained transformations applied to an image](https://res.cloudinary.com/demo/image/upload/ar_1.0,c_fill,w_250/r_max/f_auto/livingroom-yellow-chair.png)
The following example applies 4 chained transformations: fill to a 250*400px portrait, then rotate the result by 20 degrees, then add a brown outline to the rotated image, and optimize the resulting image to deliver with the best compression that gives good visual quality and in the optimal transparent format:
![4 chained transformations applied to an image 4 chained transformations applied to an image](https://res.cloudinary.com/demo/image/upload/c_fill,h_400,w_250/a_20/e_outline,co_brown/q_auto/f_auto/h_350,dpr_2/kitchen-island.png)
For more information on image transformations, see Apply common image transformations.
Direct URL building
The image
tag or the imageTag
method generate an HTML image tag. In certain conditions, you might want to generate a transformation URL directly, without the containing image tag. You can do that by either using the url
tag or the generate
method. Here are few examples:
Apply common image transformations
This section provides an overview and examples of the following commonly used image transformation features, along with links to more detailed documentation on these features:
- Resizing and cropping
- Converting to another image format
- Applying image effects and filters
- Adding text and image overlays
- Image optimizations
- Responsive image settings
Keep in mind that this section is only intended to introduce you to the basics of using image transformations with Java.
For comprehensive explanations of how to implement a wide variety of transformations, see Image transformations. For a full list of all supported image transformations and their usage, see the Transformation URL API Reference.
Resizing and cropping
There are a variety of different ways to resize and/or crop your images, and to control the area of the image that is preserved during a crop.
The following 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:
You can also use automatic gravity to determine what to keep in the crop automatically.
For details on all resizing and cropping options, see resizing and cropping images.
Converting to another image format
You can deliver any image uploaded to Cloudinary in essentially any image format. There are three main 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
fetch_format
parameter. - Use the
auto
fetch_format to instruct Cloudinary to deliver the image in the most optimized format for each browser that requests it.
For example:
- Deliver a .jpg file in .gif format:
- Let Cloudinary select the optimal format for each browser. For example, in Chrome, this image may deliver in .avif or .webp format (depending on your product environment setup):
The above code generates a URL with the
f_auto
parameter:
For more details, see:
- Delivering images in different formats
- Automatic format selection (f_auto)
- Tips and considerations for using f_auto
Applying image effects and filters
You can select from a large selection of image effects, enhancements, and filters to apply to your images. The available effects include a variety of color balance and level effects, tinting, blurring, pixelating, sharpening, automatic improvement effects, artistic filters, image and text overlays, distortion and shape changing effects, outlines, backgrounds, shadows, and more.
For example, the code below applies a cartoonify effect, rounding corners effect, and background color effect (and then scales the image down to a height of 300 pixels).
![An image with several transformation effects An image with several transformation effects](https://res.cloudinary.com/demo/image/upload/e_cartoonify/r_max/e_outline:100,co_lightblue/b_lightblue/h_300/actor.jpg)
For more details on the available image effects and filters, see Visual image effects and enhancements.
Adding text and image overlays
You can add images and text as overlays on your main image. You can apply the same types of transformations on your overlay images as you can with any image and you can use gravity settings or x and y coordinates to control the location of the overlays. You can also apply a variety of transformations on text, such as color, font, size, rotation, and more.
For example, the code below overlays a couple's photo on a mug image. The overlay photo is cropped using face detection with adjusted color saturation and a vignette effect applied. The word love is added in a brown, fancy font and placed to fit the design. Additionally, the final image is cropped and the corners are rounded.
![An image with many transformations and overlays applied An image with many transformations and overlays applied](https://res.cloudinary.com/demo/image/upload/w_400,h_250,c_fill,g_south/l_nice_couple,w_1.3,h_1.3,g_faces,c_crop,fl_region_relative/e_saturation:50/e_vignette/fl_layer_apply,w_100,r_max,g_center,y_15,x_-20/l_text:Cookie_23_bold:Love,co_rgb:744700/fl_layer_apply,x_-23,y_50/c_crop,w_300,h_250,x_30/r_60/coffee_cup.png)
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 application. These include optimizations to image quality, format, and size, among others.
For example, you can use the auto
value for the fetch_format
and quality
attributes to automatically deliver the image in the format and quality that minimize file size while meeting the required quality level. In addition, resize the image to make it even lighter. Below, these parameters are applied, resulting in a 25.66 KB AVIF file (in Chrome) instead of a 1.34 MB JPG with no visible change in quality.
![Efficient file size optimization using auto format and auto quality features Efficient file size optimization using auto format and auto quality features](https://res.cloudinary.com/demo/image/upload/w_500/f_auto,q_auto/pond_reflect.jpg)
For an in-depth review of the many ways you can optimize your images, see Image optimization.
Responsive image settings
Responsive web design is a method of designing websites to provide an optimal viewing experience to users, irrespective of the device, viewport size, orientation, or resolution used to view it. Ensuring that optimal experience means you should avoid sending high resolution images that get resized client side, with significant bandwidth waste for users of small displays. Instead, you should always deliver the right size image for each device and screen size.
For example, you can ensure that each user receives images at the size and device pixel ratio (dpr) that fit their device using the auto
value for the dpr
and width
attributes. The auto
value is replaced with actual values on the client side based on the screen properties and viewport width:
Cloudinary offers several options for simplifying the complexity of delivering responsive images. For a detailed guide on how to implement these options, see Responsive images.